滚动不适用于 BottomSheet 中的多个 RecyclerView

Scroll not working for multiple RecyclerView in BottomSheet

我使用 DialogFragment 方法实现了 BottomSheet。我在 BottomSheet 中有一个 TabLayoutViewPagerViewPager 有 2 个页面,每个页面都有一个 RecyclerView。第一个(咖啡选项卡)RecyclerView 滚动正常。我现在遇到的问题是,对于第二个(牛奶选项卡),滚动条不起作用。知道我该如何解决这个问题吗?谢谢!

您可以使用我在此处创建的演示项目进行测试:https://github.com/choongyouqi/bottomsheet`

当我试图在 Whosebug 上查找问题时,我发现 . It depicts the bug (at least that is how I look at it), that BottomSheetBehaviour works only for the first scrollable child it finds. It also proposes the usage of different CoordinatorLayout.Behavior proposed and published here

但是,您的情况有点不同。 BottomSheetDialogFragment 被使用。这就是提供的解决方案不起作用的地方。但是,我设法克服了这个问题。已发布 repository,您的项目已在其中修改以正常运行。它使用前面提到的库中的 ViewPagerBottomSheetBehavior

基本上,进行了以下更改:

  1. StatisticFragment 扩展 ViewPagerBottomSheetDialogFragment 而不是 BottomSheetDialogFragment
  2. StatisticsFragment中的onCreateDialog函数修改:

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ViewPagerBottomSheetDialog dialog = (ViewPagerBottomSheetDialog) super.onCreateDialog(savedInstanceState);
        View rootView = View.inflate(getContext(), R.layout.sheet_main, null);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
        dialog.setContentView(rootView);
        mBehavior = ViewPagerBottomSheetBehavior.from((View) rootView.getParent());
        mBehavior.setPeekHeight(400);
        if (viewPager != null && tabLayout != null) {
            initViewPager();
        }
        return dialog;
    }
    
  3. ViewPager 上调用了以下函数:

    BottomSheetUtils.setupViewPager(viewPager);
    

仅此而已。该项目有效。

The following is done behind the scenes:

BottomSheetDialogFragment 只有一种方法:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new BottomSheetDialog(getContext(), getTheme());
}

BottomSheetDialog 已 return 编辑。但是,它已将静态定义的行为设置为 BottomSheetBehavior。需要的是将 ViewPagerBottomSheetDialogFragment 覆盖为 return ViewPagerBottomSheetDialog,其中 CoordinatorLayout.Behavior 设置为 ViewPagerBottomSheetBehavior。此外,需要覆盖自定义 BottomSheet 以适应 ViewPagerBottomSheetBehavior.

如 R. Zagórski 所述,我描述了这种滚动行为的原因 ,即 BottomSheetBehavior 仅支持一个滚动子项。然而,这个答案并不关注底部 Sheet 对话框。

因此 – 就像 R. Zagórski – 我扩展了我自己的 library 来克服这个限制。 从 0.0.3 开始支持底部 Sheet 对话框!您可以在此处找到库和示例应用程序: https://github.com/laenger/ViewPagerBottomSheet

要在您的项目中使用,只需将 Maven 存储库 url 添加到您的 build.gradle:

repositories {
    maven { url "https://raw.github.com/laenger/maven-releases/master/releases" }
}

将库添加到依赖项:

dependencies {
    compile "biz.laenger.android:vpbs:0.0.3"
}

使用 ViewPagerBottomSheetDialogFragment 作为对话片段的超级 class。然后在内容视图中设置任何 ViewPager:

public class DialogFragment extends ViewPagerBottomSheetDialogFragment {
    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        final View contentView = View.inflate(getContext(), R.layout.dialog_bottom_sheet, null);

        final ViewPager viewPager = (ViewPager) contentView.findViewById(R.id.viewpager);
        // ...
        BottomSheetUtils.setupViewPager(viewPager);

        dialog.setContentView(contentView);
    }
}

您不需要将 StatisticFragment 扩展为 ViewPagerBottomSheetDialogFragment,也不需要为此使用任何库。

是你的代码我刚刚对你的 Static Fragment 做了一些修改 View Pager.

这是我进行了更改的 Statistic Fragment

上面所有的回答都没有任何错误。

仅用您的旧 Static fragment 替换此代码,而不是任何其他更改,它将为您提供所需的输出。

我刚刚对您的 View Pager 进行了更改,并使其按您的需要工作。 使用 OnPageChangeListener 方法只获取该视图。

统计Fragment.java

    public class StatisticFragment extends BottomSheetDialogFragment {

        private BottomSheetBehavior mBehavior;
        private TabLayout tabLayout;
        private ViewPager viewPager;

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
            View rootView = View.inflate(getContext(), R.layout.sheet_main, null);

            viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
            tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
            if (viewPager != null && tabLayout != null) {
                initViewPager();
            }

            final ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub
                    View view = viewPager.findViewWithTag(arg0);
                    if (view == null) {
                        return;
                    }
                    CustomPagerAdapter adapter = new CustomPagerAdapter(getContext());
                    viewPager.setAdapter(adapter);
                    viewPager.setOffscreenPageLimit(10);
                    tabLayout.setupWithViewPager(viewPager);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            };

            viewPager.addOnPageChangeListener(pageChangeListener);
            viewPager.post(new Runnable() {
                @Override
                public void run() {
                    pageChangeListener.onPageSelected(viewPager.getCurrentItem());
                }
            });


            dialog.setContentView(rootView);
            mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
            return dialog;


        }

        private void initViewPager() {
            CustomPagerAdapter adapter = new CustomPagerAdapter(getContext());
            viewPager.setAdapter(adapter);
            viewPager.setOffscreenPageLimit(10);
            tabLayout.setupWithViewPager(viewPager);

        }

        @Override
        public void onStart() {
            super.onStart();
            //mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            //mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }

        public class ServiceVideHolder extends RecyclerView.ViewHolder {
            protected ViewGroup mItemView;
            protected TextView mNameView;
            protected TextView mCodeView;

            public ServiceVideHolder(View v) {
                super(v);
                //rootView = v;
                mItemView = (ViewGroup) v.findViewById(R.id.item);
                mNameView = (TextView) v.findViewById(R.id.main_text);
                mCodeView = (TextView) v.findViewById(R.id.sub_text);
            }
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            protected TextView mMainText;
            protected TextView mSubText;

            public ItemViewHolder(View v) {
                super(v);
                mMainText = (TextView) v.findViewById(R.id.main_text);
                mSubText = (TextView) v.findViewById(R.id.sub_text);
            }
        }

        public class ItemAdapter extends RecyclerView.Adapter<ItemViewHolder> {
            private List<Item> items;

            public ItemAdapter(List<Item> items) {
                this.items = items;
            }

            @Override
            public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
                return new ItemViewHolder(view);
            }

            @Override
            public void onBindViewHolder(final ItemViewHolder viewHolder, final int position) {
                final Item item = items.get(position);
                viewHolder.mMainText.setText(item.name);
                viewHolder.mSubText.setText(item.value);
                viewHolder.mMainText.setTextColor(ResourcesCompat.getColor(getResources(), position % 2 == 0 ? R.color.md_red_500 : R.color.md_blue_500, null));
            }

            @Override
            public int getItemCount() {
                return items.size();
            }
        }

        class ViewPagerAdapter extends FragmentPagerAdapter {
            private final List<Fragment> mFragmentList = new ArrayList<>();
            private final List<String> mFragmentTitleList = new ArrayList<>();

            public ViewPagerAdapter(FragmentManager manager) {
                super(manager);
            }

            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            public void addFrag(Fragment fragment, String title) {
                mFragmentList.add(fragment);
                mFragmentTitleList.add(title);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mFragmentTitleList.get(position);
            }
        }

        public class CustomPagerAdapter extends PagerAdapter {

            private Context mContext;

            public CustomPagerAdapter(Context context) {
                mContext = context;
            }

            @Override
            public Object instantiateItem(ViewGroup collection, int position) {
                //CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
                LayoutInflater inflater = LayoutInflater.from(mContext);
                ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.adapter, collection, false);
                rootView.setTag(position);


                Toast.makeText(mContext, "Inside Instanciate Item", Toast.LENGTH_SHORT).show();

                //View rootView = View.inflate(getContext(), R.layout.adapter, null);
                final RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
                ArrayList<Item> items = new ArrayList<>();

                if (position == 0) {
                    items.add(new Item("Coffee 1", "1"));
                    items.add(new Item("Coffee 2", "2"));
                    items.add(new Item("Coffee 3", "3"));
                    items.add(new Item("Coffee 4", "4"));
                    items.add(new Item("Coffee 5", "5"));
                    items.add(new Item("Coffee 6", "6"));
                    items.add(new Item("Coffee 7", "7"));
                    items.add(new Item("Coffee 8", "8"));
                    items.add(new Item("Coffee 9", "9"));
                    items.add(new Item("Coffee 10", "10"));
                } else {
                    items.add(new Item("Milk 1", "1"));
                    items.add(new Item("Milk 2", "2"));
                    items.add(new Item("Milk 3", "3"));
                    items.add(new Item("Milk 4", "4"));
                    items.add(new Item("Milk 5", "5"));
                    items.add(new Item("Milk 6", "6"));
                    items.add(new Item("Milk 7", "7"));
                    items.add(new Item("Milk 8", "8"));
                    items.add(new Item("Milk 9", "9"));
                    items.add(new Item("Milk 10", "10"));
                }

                final ItemAdapter mAdapter = new ItemAdapter(items);

                mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
                mRecyclerView.setNestedScrollingEnabled(false);
                mRecyclerView.setAdapter(mAdapter);
                Paint paint = new Paint();
                paint.setStrokeWidth(1);
                paint.setColor(ResourcesCompat.getColor(getResources(), R.color.md_grey_500, null));
                paint.setAntiAlias(true);
                paint.setPathEffect(new DashPathEffect(new float[]{25.0f, 25.0f}, 0));
                mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).showLastDivider().paint(paint).build()); //.marginResId(R.dimen.leftmargin, R.dimen.rightmargin)

                collection.addView(rootView);


                return rootView;
            }

            @Override
            public void destroyItem(ViewGroup collection, int position, Object view)    {
                collection.removeView((View) view);
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                //CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
                return position == 0 ? "Coffee" : "Milk";
            }

        }
    }

完成。

您可以在 CoordinatorLayout 中使用 2 个 RecyclerView。

<android.support.design.widget.CoordinatorLayout
         android:id="@+id/mainBottomSheet"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@color/white">

         <android.support.v7.widget.RecyclerView
                  android:id="@+id/recyclerViewRight"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" />

         <android.support.v7.widget.RecyclerView
                  android:id="@+id/recyclerViewLeft"
                  android:layout_width="200dp"
                  android:layout_height="match_parent" />

</android.support.design.widget.CoordinatorLayout>

检查这个 post

将此视图用作根视图:

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class DisallowInterceptView extends LinearLayout {
    public DisallowInterceptView(Context context) {
        super(context);
        requestDisallowInterceptTouchEvent(true);
    }

    public DisallowInterceptView(Context context, AttributeSet attrs) {
        super(context, attrs);
        requestDisallowInterceptTouchEvent(true);
    }

    public DisallowInterceptView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        requestDisallowInterceptTouchEvent(true);
    }


    public boolean dispatchTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                requestDisallowInterceptTouchEvent(false);
                break;
        }
        return super.onTouchEvent(event);
    }

}

然后在用于 bottmSheet 的布局中:

<?xml version="1.0" encoding="utf-8"?>
<com.your.package.DisallowInterceptView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:maxHeight="400dp"
    android:minHeight="300dp"
    android:orientation="vertical"
    >

    ...
</LinearLayout>

</com.your.package.DisallowInterceptView>

我遇到了同样的问题,要解决此问题而无需覆盖 BottomSheetBehavior 或不需要额外的库,您可以执行以下操作: 在注册页面更改的底部 sheet 实现中实现回调。

fun onPageChanged(currentPage: Int) {
   recycler1.isNestedScrollingEnabled = currentPage == 0
   recycler2.isNestedScrollingEnabled = currentPage == 1
   dialog?.findViewById<FrameLayout>(R.id.design_bottom_sheet)?.requestLayout()
}

在 onLayoutChild 的 BottomSheetBehavior 实现中,执行了对支持嵌套滚动的第一个子项的查找,并通过此更改重复查找。不是最佳解决方案,但在我的情况下效果很好

解决此问题的更好方法是将以下行添加到 proguard 规则中:

-keep class androidx.viewpager.widget.ViewPager$LayoutParams { int position; }
-keep class com.google.android.material.bottomsheet.BottomSheetBehavior { *** findScrollingChild(...); }

可以在这个 link 上找到有关此的更多详细信息:https://github.com/kafumi/android-bottomsheet-viewpager#proguardr8