Fragment Replacing in Recyclerview Item

Fragment replacing in RecyclerView item

I finnaly found solution. The problem is I set a common container id. But in recycler view need to set unique container id for each item.

So, my code now this:

MyFragment fragment = MyFragment.newInstance("fragment1");
fragmentManager.beginTransaction().replace(UNIQUE_CONTAINER_ID, fragment).commit();

If someone will be useful, here is my complete code (implementation fragment in recycler view):

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) 
{

...

// Delete old fragment
int containerId = holder.mediaContainer.getId();// Get container id
Fragment oldFragment = fragmentManager.findFragmentById(containerId);
if(oldFragment != null) {
fragmentManager.beginTransaction().remove(oldFragment).commit();
}

int newContainerId = View.generateViewId();// Generate unique container id
holder.mediaContainer.setId(newContainerId);// Set container id

// Add new fragment
MyFragment fragment = MyFragment.newInstance("fragment1");
fragmentManager.beginTransaction().replace(newContainerId, fragment).commit();

...

}

Upd.: Instead of using your own method to generate a unique id, it is recommended to use View.generateViewId()

Opening another fragment on RecyclerView item click doesn't hide RecylerView

This is how i got it working-

  • activity_main.xml should contain the layout_container (FrameLayout). Nothing else.
  • HomeFragment.xml should contain the RecyclerView and Navigation drawer
  • Main_activity.java should have the code to replace the layout_container with
    HomeFragment Just put the code there no need of any listener.
  • In HomeFragment.java we can set the listener on RecyclerView items to open the other
    Fragments/Activities.

android fragment inside individual item of recyclerview

hmm... recyclerview is not for that, is it for recycle views ^^, if you want to dislpays fragments in a 'list' you can create a class extending LinearLayout which will display your fragments.



Related Topics



Leave a reply



Submit