当前位置:   article > 正文

CoordinatorLayout + TabLayout + ViewPager2(里面再嵌套一个RecyclerView),RecyclerView的滑动冲突解决_viewpager2嵌套recyclerview

viewpager2嵌套recyclerview

用了一个CoordinatorLayout 滑动布局,但是需要用到TabLayout + ViewPager2组合,然后ViewPager2里面又是一个recyclerView列表,写好代码之后,发现recyclerView无法纵向滑动,去掉CoordinatorLayout布局之后就可以滑动了,但是有需求在这儿,不可能去掉呀,所以只能从recyclerView这里想办法解决滑动冲突问题。在网上找了好久,都说是设置监听,都没有效果,最后发现,只需要在ViewPager2这里加一个属性,就可以让ViewPager2里面的RecyclerView正常纵向滑动了。这个属性就是【app:layout_behavior="@string/appbar_scrolling_view_behavior"】。

所有test代码如下:

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <androidx.coordinatorlayout.widget.CoordinatorLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <com.google.android.material.appbar.AppBarLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content">
  15. <LinearLayout
  16. app:layout_scrollFlags="scroll"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:orientation="vertical">
  20. <TextView
  21. android:layout_width="match_parent"
  22. android:layout_height="300dp"
  23. android:text="aaa" />
  24. <TextView
  25. android:layout_width="match_parent"
  26. android:layout_height="300dp"
  27. android:text="aaa" />
  28. <TextView
  29. android:layout_width="match_parent"
  30. android:layout_height="300dp"
  31. android:text="aaa" />
  32. <TextView
  33. android:layout_width="match_parent"
  34. android:layout_height="300dp"
  35. android:text="aaa" />
  36. </LinearLayout>
  37. <com.google.android.material.tabs.TabLayout
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:id="@+id/tabLayout" />
  41. </com.google.android.material.appbar.AppBarLayout>
  42. <androidx.viewpager2.widget.ViewPager2
  43. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:id="@+id/viewPager2"/>
  47. </androidx.coordinatorlayout.widget.CoordinatorLayout>
  48. </LinearLayout>
MainActivity.java:
  1. package com.example.myapplication;
  2. import androidx.annotation.NonNull;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.viewpager2.widget.ViewPager2;
  5. import android.os.Bundle;
  6. import com.google.android.material.tabs.TabLayout;
  7. import com.google.android.material.tabs.TabLayoutMediator;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class MainActivity extends AppCompatActivity {
  11. // recyclerView中的item数据源
  12. private List<String> itemData = new ArrayList<>();
  13. // tabLayout的标题
  14. private String[] tabTitle = {"标题1", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7", "标题8", "标题9"};
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. initItemData();
  20. TabLayout tabLayout = findViewById(R.id.tabLayout);
  21. // 设置横向滑动
  22. tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
  23. ViewPager2 viewPager2 = findViewById(R.id.viewPager2);
  24. viewPager2.setAdapter(new MyViewPager2Adapter(itemData, tabTitle, this));
  25. TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
  26. @Override
  27. public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
  28. // 设置tabLayout的标题
  29. tab.setText(tabTitle[position]);
  30. }
  31. });
  32. // 应用生效
  33. tabLayoutMediator.attach();
  34. }
  35. /**
  36. * 初始化recyclerView中的item数据源
  37. */
  38. private void initItemData() {
  39. for (int i = 0; i < 100; i++) {
  40. itemData.add(i + "");
  41. }
  42. }
  43. }

MyViewPager2Adapter.java:

  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import androidx.annotation.NonNull;
  7. import androidx.recyclerview.widget.LinearLayoutManager;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import java.util.List;
  10. public class MyViewPager2Adapter extends RecyclerView.Adapter<MyViewPager2Adapter.ViewHolder> {
  11. private List<String> itemData;
  12. private String[] TabTitle;
  13. private Context context;
  14. public MyViewPager2Adapter(List<String> itemData, String[] tabTitle, Context context) {
  15. this.itemData = itemData;
  16. TabTitle = tabTitle;
  17. this.context = context;
  18. }
  19. @NonNull
  20. @Override
  21. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  22. return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_pager2_recycler_view, parent, false));
  23. }
  24. @Override
  25. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  26. holder.recyclerView.setAdapter(new MyRecyclerViewAdapter(itemData));
  27. holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
  28. }
  29. @Override
  30. public int getItemCount() {
  31. return TabTitle.length;
  32. }
  33. class ViewHolder extends RecyclerView.ViewHolder {
  34. RecyclerView recyclerView;
  35. public ViewHolder(@NonNull View itemView) {
  36. super(itemView);
  37. recyclerView = itemView.findViewById(R.id.recyclerView);
  38. }
  39. }
  40. }
view_pager2_recycler_view.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <androidx.recyclerview.widget.RecyclerView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/recyclerView" />
  9. </LinearLayout>
MyRecyclerViewAdapter.java:
  1. package com.example.myapplication;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.TextView;
  6. import androidx.annotation.NonNull;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. import java.util.List;
  9. public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
  10. List<String> itemData;
  11. public MyRecyclerViewAdapter(List<String> itemData) {
  12. this.itemData = itemData;
  13. }
  14. @NonNull
  15. @Override
  16. public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  17. return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false));
  18. }
  19. @Override
  20. public void onBindViewHolder(@NonNull MyRecyclerViewAdapter.ViewHolder holder, int position) {
  21. holder.textView.setText(itemData.get(position));
  22. }
  23. @Override
  24. public int getItemCount() {
  25. return itemData.size();
  26. }
  27. class ViewHolder extends RecyclerView.ViewHolder {
  28. TextView textView;
  29. public ViewHolder(@NonNull View itemView) {
  30. super(itemView);
  31. textView = itemView.findViewById(R.id.text);
  32. }
  33. }
  34. }
recycler_view_item.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <TextView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/text" />
  9. </LinearLayout>

最后的长这个样子:

如果还是有滑动冲突的话,可以把MainActivity.xml中的ViewPager2的代码改成这样:

用【NestedScrollView】把它包裹起来,然后属性【app:layout_behavior="@string/appbar_scrolling_view_behavior"】也挪动到【NestedScrollView】中就好了。

  1. <androidx.core.widget.NestedScrollView
  2. app:layout_behavior="@string/appbar_scrolling_view_behavior"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <androidx.viewpager2.widget.ViewPager2
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/viewPager2"/>
  9. </androidx.core.widget.NestedScrollView>

还有一种情况,【NestScrollView】嵌套了一个【RecyclerView】,【RecyclerView】中又嵌套了一个【RecyclerView】,此时第二个【RecyclerView】就无法滑动了,此时可以对第二个【RecyclerView】设置属性允许滑动,就可以继续滑动了:

itemBinding.mineRvChild.setNestedScrollingEnabled(false);

如果出现了AppBarLayout无法上下滑动的情况的话(一般是RecyclerView中滑动一下,切换ViewPager2,然后直接对AppBarLayout进行触摸,发现无法滑动),这种滑动冲突的处理方法是:给ViewPager2设置一个页面滑动监听,当发生页面切换时,重新设置一下AppBarLayout为可以滑动。

  1. binding.supermarketVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
  2. @Override
  3. public void onPageSelected(int position) {
  4. super.onPageSelected(position);
  5. // 左右切换页面后,要重新设置AppBarLayout为可以滑动
  6. CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) binding.appBar.getLayoutParams();
  7. AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) layoutParams.getBehavior();
  8. behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
  9. @Override
  10. public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
  11. return true;
  12. }
  13. });
  14. }
  15. });

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/450071
推荐阅读
相关标签
  

闽ICP备14008679号