Fatema
Fatema

Reputation: 41

How to open a viewpager fragment when a recyclerview item is clicked

I'm new in android. I have a navigation drawer where i used recyclerview for the item. I have three tabs representing three fragments in a viewpager. My problem is that i can not open a fragment by clicking recycler view item. And another problem is that I tried FragmentTransaction but don't know how to get the fragment id. Please help me i'm stuck on it.You can give me any tutorial link.

Here is my code...

In my recyclerview adapter i have tried:

public void onBindViewHolder(MyViewHandler holder, int position) {

    final Information current = data.get(position);
    holder.title.setText(current.title);
    holder.icon.setImageResource(current.iconId);
    holder.title.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fragmentJump(current);
        }
    });

}
private void fragmentJump(Information mItemSelected) {
    mFragment = new FragmentIncome();
    mBundle = new Bundle();
    mBundle.putParcelable("item_selected_key", mItemSelected);// here my Information class is not parcelable how to solve it?
    mFragment.setArguments(mBundle);
    switchContent(R.id.frag1, mFragment);/// Here how to get the fragment id in R.id.frag1 of viewpager
}
public void switchContent(int id, Fragment fragment) {
    if (mContext == null)
        return;
    if (mContext instanceof MainActivity) {
        MainActivity mainActivity = (MainActivity) mContext;
        Fragment frag = fragment;
        mainActivity.switchContent(id, frag);
    }

}

And in the MainActivity i have created this method:

 public void switchContent(int id, Fragment fragment) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(id, fragment, fragment.toString());
    ft.addToBackStack(null);
    ft.commit();
}

I have followed this link how to open a different fragment on recyclerview OnClick

Upvotes: 0

Views: 1527

Answers (1)

pawar
pawar

Reputation: 70

You can do this by using interface callback when a recyclerview item is clicked:

You could do this:

class NavdrawerRecyclerAdapter extends RecyclerView.Adapter<> {

private OnFragmentInteractionListener mListener;

public NavdrawerRecyclerAdapter(OnFragmentInteractionListener listener){
mListner = listner;
}

//call and pass position inside onClick
public void onRecyclerItemClicked(int pos){
mListener.openFragment(pos);
}


public interface OnFragmentInteractionListener{
public void openFragment(int pos);
}}

Now your mainactivity which has tablayouts implement this interface.

public class MainActivity extends AppCompatActivity implements NavdrawerRecyclerAdapter.OnFragmentInteractionListener{

NavdrawerRecyclerAdapter adapter = new NavdrawerRecyclerAdapter(this);

@override
openFragment(int position){
TabLayout.Tab tab = tabLayout.getTabAt(position);
tab.select();
}
}

Try it and tell me if this works.

Upvotes: 0

Related Questions