ishwor kafley
ishwor kafley

Reputation: 938

How to set NavigationDrawer item click listener?

I have two classes.One is an abstract classBaseActivity.java which extends AppcompactActivity and another one is DrawerFragment.java which extends Fragment! How do I make BaseActivity class implement NavigationItemSelectedListener and handle navigation item click events from this activity! I have copy pasted the related code below:

BaseActivity.java

public abstract class BaseActivity extends AppCompatActivity {
    private static final String TAG = BaseActivity.class.getSimpleName();
    protected Toolbar mToolbar;
    protected DrawerFragment mNavigationDrawerFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());
        mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setElevation(0);


        // Set navigation drawer 
        mNavigationDrawerFragment = (DrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_drawer);
        mNavigationDrawerFragment.setNavig((DrawerLayout) findViewById(R.id.drawer), mToolbar);

}

Drawer Fragment.java

public class DrawerFragment extends Fragment{

    private NavigationView navigView;
    private Activity activity;
    private static final String TAG = "NavigationDrawer";
    private DrawerLayout drawerLayout;
    private TextView fullNameTextView;
    private ImageView profileImage;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.navigation_drawer_fragment, container, false);
        navigView=(NavigationView)view.findViewById(R.id.main_drawer);

//      Inflating nav_header layout on top of navigation drawer layout
        View temp=getActivity().getLayoutInflater().inflate(R.layout.nav_header,navigView,false);
        navigView.addHeaderView(temp);
        profileImage= (ImageView) temp.findViewById(R.id.profile_image);
        fullNameTextView= (TextView) temp.findViewById(R.id.fullNameTextView);
        return view;
    }

    public void setNavig(DrawerLayout drawerLayout, Toolbar toolbar) {
        ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(activity, drawerLayout,toolbar, R.string.drawer_open, R.string.drawer_close);
        drawerLayout.setDrawerListener(drawerToggle);
        drawerToggle.syncState();
        this.drawerLayout=drawerLayout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity=getActivity();

    }

}

I know how to implement item click listener on DrawerFragment.java class and handle events there.But its not what I am looking for.I intend to do the same from BaseActivity.java class

Upvotes: 2

Views: 2291

Answers (1)

Balman Rawat
Balman Rawat

Reputation: 3922

//MAKE A PUBLIC METHOD IN THE NAVIGATION VIEW THAT RETURNS NAVIGATION VIEW
//in DrawerFragment
public NavigationView getNav(){

   return navView;
}


//use that public method from baseactivity.java and set listener on the obtained object
getNav().setNavigationItemSelectedListener(this);

but the best way is to implement listener on the navigation drawer fragment itself. because it makes navigation drawer complete and decoupled with BaseActivity(or any other class). OR You can do is like the one below Using Interface Mechanism as below:

//INSIDE FragmentDrawer
//Let FragmentDrawer Implement OnItemSelectedListener

//then you need to override onItemSelected() method
@Override
public void OnItemSelected(int pos){

  //Call The method implemented on your Activity using interface
  communicator.displayItem(pos);

 //where communicator has reference to your activity and dispayItem() is implemented method

}

Upvotes: 1

Related Questions