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 thelayout_container (FrameLayout)
. Nothing else.HomeFragment.xml
should contain theRecyclerView
andNavigation 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
How to Remove the Top and Bottom Space on Textview of Android
Why Does Gradle Build My Module in Release Mode When the App Is in Debug
Android - Build a Notification, Taskstackbuilder.Addparentstack Not Working
Accessing Files from Genymotion Sd Card
Smoothscrolltopositionfromtop() Is Not Always Working Like It Should
Equivalent of Clean & Build in Android Studio
How to Programmatically Launch a Specific Application
Android Replace the Current Fragment with Another Fragment
Executing Multiple Asynctask's Parallely
Failed to Connect to Camera Service
Project Has No Default.Properties File! Edit the Project Properties to Set One
Fragment Lifecycle: When "Ondestroy" and "Ondestroyview" Are Not Called
Adt Doesn't Create Default Hello World But Command Line Does
Java.Lang.Noclassdeffounderror: Com.Google.Android.Gms.R$Styleable in Android
How to Connect SQL Server Using Jtds Driver in Android
What Are the Android Sdk Build-Tools, Platform-Tools and Tools? and Which Version Should Be Used