I am using NavigationDrawer
and on item selection, I am changing fragment using hide/show technique instead of replace.
if(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT) != null){
//if the fragment exists, show it.
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT)).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.flContent,
BottomNavTasksFragment.newInstance(apiToken, empId), TAG_TASKS_FRAGMENT)
.commit();
}
// Hide other fragments
The Fragmanet BottomNavTasksFragmet contains two fragments, one for list (RecyclerView) and other for Google Maps. However, a fragment containing RecyclerView does not respond to any touch when opened second time. It scrolls smoothly on first load but doesn't work when shown second time.
This is xml layout of BottomNavTasksFragment
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.biocare.fragments.BottomNavTasksFragment">
<FrameLayout
android:id="@+id/bottomNavContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="@color/colorSeparator"
app:itemBackground="@drawable/navigation_bar_item_bg"
app:itemIconTint="@color/bottom_nav_icon_color_selector"
app:itemTextColor="@color/bottom_nav_icon_color_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/menu_bottom_navigation" />
The FrameLayout bottomNavContainer
is replaced when clicked on BottomNavigationItem.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_bottom_nav_tasks,
container, false);
final BottomNavigationView navigationView = view.findViewById(R.id.bottomNavigationView);
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_bottom_tasks:
if(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT) != null){
//if the fragment exists, show it.
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT)).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.bottomNavContainer,
TasksFragment.newInstance(apiToken, empId), TAG_TASKS_FRAGMENT)
.commit();
}
// Hide map fragment
if(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT) != null){
fragmentManager.beginTransaction()
.hide(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT)).commit();
}
break;
case R.id.navigation_bottom_map:
if(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT) != null){
//if the fragment exists, show it.
fragmentManager.beginTransaction()
.show(fragmentManager.findFragmentByTag(TAG_MAP_FRAGMENT)).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.bottomNavContainer,
MapViewFragment.newInstance(apiToken, empId), TAG_MAP_FRAGMENT)
.commit();
}
// Hide Tasks fragment
if(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT) != null){
fragmentManager.beginTransaction()
.hide(fragmentManager.findFragmentByTag(TAG_TASKS_FRAGMENT)).commit();
}
break;
}
return false;
}
});
FragmentTransaction t = fragmentManager.beginTransaction();
TasksFragment tasksFragment = TasksFragment.newInstance(apiToken, empId);
t.add(R.id.bottomNavContainer, tasksFragment, TAG_TASKS_FRAGMENT);
t.commit();
return view;
}
When clicked on Tasks, Scroll doesn't work. See next image]
Stuck, RecyclerView not responding, however Menu is working
If further information is required, let me know.
Thanks