RecyclerView不会在嵌套片段中滚动

时间:2018-06-17 16:17:18

标签: android android-fragments android-recyclerview android-nested-fragment

我正在使用NavigationDrawer并且在项目选择上,我正在使用hide / show技术而不是替换来更改片段。

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

Fragmanet BottomNavTasksFragmet 包含两个片段,一个用于列表(RecyclerView),另一个用于Google Maps。但是,包含RecyclerView的片段在第二次打开时不响应任何触摸。它在第一次加载时平滑滚动,但在第二次显示时不起作用。

这是 BottomNavTasksFragment

的xml布局
<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" />

单击BottomNavigationItem时会替换FrameLayout bottomNavContainer

@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;
}

单击“任务”时,滚动功能无效。见下图] Drawer

Stuck,RecyclerView没有响应,但是Menu正在运行 List

如果需要更多信息,请与我们联系。

由于

0 个答案:

没有答案