Scroll not working for multiple RecyclerView in BottomSheet

前端 未结 6 696
天命终不由人
天命终不由人 2020-12-14 09:04

I implemented BottomSheet using the DialogFragment approach. I have a TabLayout and ViewPager in the BottomSheet

6条回答
  •  囚心锁ツ
    2020-12-14 09:40

    When trying to look for the problem on StackOverflow I found this thread. 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.

    However, your case is a bit different. BottomSheetDialogFragment is used. And this is where the provided solution does not work. However, I managed to overcome this problem. Published repository, where your project was modified to be working. It uses the ViewPagerBottomSheetBehavior from the library mentioned earlier.

    Basically, the following changes were made:

    1. StatisticFragment extends ViewPagerBottomSheetDialogFragment and not BottomSheetDialogFragment
    2. The onCreateDialog function in StatisticsFragment is changed:

      @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. The following function is invoked on the ViewPager:

      BottomSheetUtils.setupViewPager(viewPager);
      

    And that is all. The project works.

    The following is done behind the scenes:

    BottomSheetDialogFragment has only one method:

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

    There BottomSheetDialog is returned. However, it has statically defined behaviour set to BottomSheetBehavior. What was needed was to override ViewPagerBottomSheetDialogFragment to return ViewPagerBottomSheetDialog where it's CoordinatorLayout.Behavior is set to ViewPagerBottomSheetBehavior. Also, the custom BottomSheet was needed to be overriden to accustom to ViewPagerBottomSheetBehavior.

提交回复
热议问题