Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can to set Listener for each items? #43

Open
mehdinewdesign opened this issue May 7, 2016 · 7 comments
Open

How can to set Listener for each items? #43

mehdinewdesign opened this issue May 7, 2016 · 7 comments

Comments

@mehdinewdesign
Copy link

mehdinewdesign commented May 7, 2016

Hi
I have a question , that how can to set Listener for each drawer items , for example i want when i clicked on book icon it guide me to another activity.

Thank you...

@dmytroDenysenko
Copy link
Contributor

Hi, you can use yalantis.com.sidemenu.util.ViewAnimator.ViewAnimatorListener for this purposes. It has ScreenShotable onSwitch(Resourceble slideMenuItem, ScreenShotable screenShotable, int position) method, and you can go to some activity depending on position value.

@fengkeyi
Copy link

first,会中文?哈哈

@fengkeyi
Copy link

you ignore something important in the code,read it more careful

@Times125
Copy link

Hi! This is a fancy frame .But I don't know how to use the file "debug.aar"? If I have not import "debug.aar" into the library,the frame will work?

@Eddy2017
Copy link

package yalantis.com.sexocard.sample;

import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import io.codetail.animation.SupportAnimator;
import io.codetail.animation.ViewAnimationUtils;
import yalantis.com.sexocard.interfaces.Resourceble;
import yalantis.com.sexocard.interfaces.ScreenShotable;
import yalantis.com.sexocard.model.SlideMenuItem;
import yalantis.com.sexocard.sample.fragment.ContentFragment;
import yalantis.com.sexocard.util.ViewAnimator;

public class MainActivity extends AppCompatActivity implements ViewAnimator.ViewAnimatorListener {

private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private List<SlideMenuItem> list = new ArrayList<>();
private ContentFragment contentFragment;
private ViewAnimator viewAnimator;
private int res = R.drawable.backmain;
private LinearLayout linearLayout;


Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


contentFragment = ContentFragment.newInstance(R.drawable.backmain);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.content_frame, contentFragment)
            .commit();

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.setScrimColor(Color.TRANSPARENT);
    linearLayout = (LinearLayout) findViewById(R.id.left_drawer);
    linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawerLayout.closeDrawers();
        }
    });


    setActionBar();
    createMenuList();
    viewAnimator = new ViewAnimator<>(this, list, contentFragment, drawerLayout, this);}



private void createMenuList() {
    SlideMenuItem menuItem0 = new SlideMenuItem(ContentFragment.CLOSE, R.drawable.icn_close);
    list.add(menuItem0);
    SlideMenuItem menuItem = new SlideMenuItem(ContentFragment.GAME, R.drawable.controller);
    list.add(menuItem);
    SlideMenuItem menuItem2 = new SlideMenuItem(ContentFragment.SETTINGS, R.drawable.settings);
    list.add(menuItem2);
    SlideMenuItem menuItem3 = new SlideMenuItem(ContentFragment.RULE, R.drawable.rule);
    list.add(menuItem3);
    SlideMenuItem menuItem4 = new SlideMenuItem(ContentFragment.SHARE, R.drawable.share);
    list.add(menuItem4);
    SlideMenuItem menuItem5 = new SlideMenuItem(ContentFragment.RATE, R.drawable.rate);
    list.add(menuItem5);
    SlideMenuItem menuItem6 = new SlideMenuItem(ContentFragment.ABOUT, R.drawable.about);
    list.add(menuItem6);
    SlideMenuItem menuItem7 = new SlideMenuItem(ContentFragment.RELEASE, R.drawable.ic_release);
    list.add(menuItem7);
}


private void setActionBar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    drawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            drawerLayout,         /* DrawerLayout object */
            toolbar,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
    ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            linearLayout.removeAllViews();
            linearLayout.invalidate();
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            if (slideOffset > 0.6 && linearLayout.getChildCount() == 0)
                viewAnimator.showMenuContent();
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}


private ScreenShotable replaceFragment(ScreenShotable screenShotable, int topPosition) {
    this.res = this.res == R.drawable.backmain ? R.drawable.body : R.drawable.backmain;
    View view = findViewById(R.id.content_frame);
    int finalRadius = Math.max(view.getWidth(), view.getHeight());
    SupportAnimator animator = ViewAnimationUtils.createCircularReveal(view, 0, topPosition, 0, finalRadius);
    animator.setInterpolator(new AccelerateInterpolator());
    animator.setDuration(ViewAnimator.CIRCULAR_REVEAL_ANIMATION_DURATION);

    findViewById(R.id.content_overlay).setBackgroundDrawable(new BitmapDrawable(getResources(), screenShotable.getBitmap()));
    animator.start();
    ContentFragment contentFragment = ContentFragment.newInstance(this.res);
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, contentFragment).commit();
    return contentFragment;
}

@Override
public ScreenShotable onSwitch(Resourceble slideMenuItem, ScreenShotable screenShotable, int position) {
    switch (slideMenuItem.getName()) {
        case ContentFragment.CLOSE:
            return screenShotable;
        default:
            return replaceFragment(screenShotable, position);
    }
}

@Override
public void disableHomeButton() {
    getSupportActionBar().setHomeButtonEnabled(false);

}

@Override
public void enableHomeButton() {
    getSupportActionBar().setHomeButtonEnabled(true);
    drawerLayout.closeDrawers();

}

@Override
public void addViewToContainer(View view) {
    linearLayout.addView(view);
}



@Override
public void onBackPressed() {
    //Pop up pour quitter l'application, j'en suis fier!!!
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MainActivity.this.finish();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();

}

}

@dinil-hashtag
Copy link

dinil-hashtag commented Jan 20, 2017

But in parameter "topPosition" the value is changing on different phone. i think the position value is changing on depending on screen size.

@mohamedebrahim96
Copy link

App

=====================================
This is my fragment code - full project on github
https://github.com/mohamedebrahim96/Whos-in-Space

===========================================
you may also see the live version on google play
https://play.google.com/store/apps/details?id=com.vacuum.app.whoisinspace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants