-2

May be duplicate,

Navigation Drawer RecyclerView Items Click to an Activity

Facing the same issue. Please help to solve this issue.

I have a navigation drawer having recycler view in it. I have set onClickListener in adapter. Now I am trying to open a fragment after clicking on any drawer item.

EDIT:

I am using Drawerlayout and ActionBarDrawerToggle to have navigation drawer in my actionbaractivity. I am using recycler view to populate the data in navigation drawer. In recyclerview adapter, I am able to put onClickListener.

public static class ViewHolder extends RecyclerView.ViewHolder       implements View.OnClickListener 
{ 
 @Override
        public void onClick(View v) {

        }
}

I am able to open an activity in onclick but i want to open a fragment in onclick.

Using ,

FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

But failed from all the ways.

Community
  • 1
  • 1
Custadian
  • 845
  • 1
  • 11
  • 37
  • 2
    You know it's a duplicate, and the only thing you do is creating a question that just says "Facing the same issue" ? How can you not see this is not the right way to go ??? – 2Dee Jan 28 '15 at 13:05
  • 1
    Please explain completely and precisely what you have tried and what specific problems you have encountered. – CommonsWare Jan 28 '15 at 13:06
  • Any suggestion will be helpful – Custadian Jan 29 '15 at 04:30

2 Answers2

0

For a fragment transaction, you probably want to handle the navigation drawer RecyclerView's clicks in your activity rather than in the adapter:

final GestureDetector mGestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return true;
    }

});

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener(){

    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView,MotionEvent motionEvent) {
        View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());


        if (child != null && mGestureDetector.onTouchEvent(motionEvent)) {
            drawer.closeDrawers();
            Toast.makeText(MainActivity.this,"The Item Clicked is: "+recyclerView.getChildPosition(child),Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView recyclerView,MotionEvent motionEvent){

    }
});

See this tutorial for further explanation and code: http://www.android4devs.com/2015/01/recycler-view-handling-onitemtouch-for.html

5ELuqLbb85Hk
  • 161
  • 1
  • 9
0

In your adapter put this :

@Override

public void onClick(View v) {

MainActivity.onClickedMenu(getPosition());

}

In your MainActivity put this :

public static void onClickedMenu(int id){
// Do what you want 

}

The Simple is the best.

Felipe Gualberto
  • 2,389
  • 1
  • 11
  • 4