赞
踩
用了一个CoordinatorLayout 滑动布局,但是需要用到TabLayout + ViewPager2组合,然后ViewPager2里面又是一个recyclerView列表,写好代码之后,发现recyclerView无法纵向滑动,去掉CoordinatorLayout布局之后就可以滑动了,但是有需求在这儿,不可能去掉呀,所以只能从recyclerView这里想办法解决滑动冲突问题。在网上找了好久,都说是设置监听,都没有效果,最后发现,只需要在ViewPager2这里加一个属性,就可以让ViewPager2里面的RecyclerView正常纵向滑动了。这个属性就是【app:layout_behavior="@string/appbar_scrolling_view_behavior"】。
所有test代码如下:
activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <androidx.coordinatorlayout.widget.CoordinatorLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <com.google.android.material.appbar.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <LinearLayout
- app:layout_scrollFlags="scroll"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="300dp"
- android:text="aaa" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="300dp"
- android:text="aaa" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="300dp"
- android:text="aaa" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="300dp"
- android:text="aaa" />
- </LinearLayout>
- <com.google.android.material.tabs.TabLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/tabLayout" />
-
- </com.google.android.material.appbar.AppBarLayout>
- <androidx.viewpager2.widget.ViewPager2
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/viewPager2"/>
- </androidx.coordinatorlayout.widget.CoordinatorLayout>
-
-
-
- </LinearLayout>
MainActivity.java:
- package com.example.myapplication;
-
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.viewpager2.widget.ViewPager2;
-
- import android.os.Bundle;
-
- import com.google.android.material.tabs.TabLayout;
- import com.google.android.material.tabs.TabLayoutMediator;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class MainActivity extends AppCompatActivity {
-
- // recyclerView中的item数据源
- private List<String> itemData = new ArrayList<>();
- // tabLayout的标题
- private String[] tabTitle = {"标题1", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7", "标题8", "标题9"};
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initItemData();
-
- TabLayout tabLayout = findViewById(R.id.tabLayout);
- // 设置横向滑动
- tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
-
- ViewPager2 viewPager2 = findViewById(R.id.viewPager2);
- viewPager2.setAdapter(new MyViewPager2Adapter(itemData, tabTitle, this));
- TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
- @Override
- public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
- // 设置tabLayout的标题
- tab.setText(tabTitle[position]);
- }
- });
- // 应用生效
- tabLayoutMediator.attach();
-
- }
-
- /**
- * 初始化recyclerView中的item数据源
- */
- private void initItemData() {
- for (int i = 0; i < 100; i++) {
- itemData.add(i + "");
- }
- }
- }
MyViewPager2Adapter.java:
- package com.example.myapplication;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
-
- import androidx.annotation.NonNull;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.RecyclerView;
-
- import java.util.List;
-
- public class MyViewPager2Adapter extends RecyclerView.Adapter<MyViewPager2Adapter.ViewHolder> {
-
- private List<String> itemData;
-
- private String[] TabTitle;
-
- private Context context;
-
- public MyViewPager2Adapter(List<String> itemData, String[] tabTitle, Context context) {
- this.itemData = itemData;
- TabTitle = tabTitle;
- this.context = context;
- }
-
- @NonNull
- @Override
- public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_pager2_recycler_view, parent, false));
- }
-
- @Override
- public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
- holder.recyclerView.setAdapter(new MyRecyclerViewAdapter(itemData));
- holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
- }
-
- @Override
- public int getItemCount() {
- return TabTitle.length;
- }
-
- class ViewHolder extends RecyclerView.ViewHolder {
- RecyclerView recyclerView;
- public ViewHolder(@NonNull View itemView) {
- super(itemView);
- recyclerView = itemView.findViewById(R.id.recyclerView);
- }
- }
- }
view_pager2_recycler_view.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <androidx.recyclerview.widget.RecyclerView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/recyclerView" />
-
- </LinearLayout>
MyRecyclerViewAdapter.java:
- package com.example.myapplication;
-
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
-
- import androidx.annotation.NonNull;
- import androidx.recyclerview.widget.RecyclerView;
-
- import java.util.List;
-
- public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
-
- List<String> itemData;
-
- public MyRecyclerViewAdapter(List<String> itemData) {
- this.itemData = itemData;
- }
-
- @NonNull
- @Override
- public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
- return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item, parent, false));
- }
-
- @Override
- public void onBindViewHolder(@NonNull MyRecyclerViewAdapter.ViewHolder holder, int position) {
- holder.textView.setText(itemData.get(position));
- }
-
- @Override
- public int getItemCount() {
- return itemData.size();
- }
-
- class ViewHolder extends RecyclerView.ViewHolder {
- TextView textView;
- public ViewHolder(@NonNull View itemView) {
- super(itemView);
- textView = itemView.findViewById(R.id.text);
- }
- }
- }
recycler_view_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/text" />
-
- </LinearLayout>
最后的长这个样子:
如果还是有滑动冲突的话,可以把MainActivity.xml中的ViewPager2的代码改成这样:
用【NestedScrollView】把它包裹起来,然后属性【app:layout_behavior="@string/appbar_scrolling_view_behavior"】也挪动到【NestedScrollView】中就好了。
- <androidx.core.widget.NestedScrollView
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <androidx.viewpager2.widget.ViewPager2
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/viewPager2"/>
- </androidx.core.widget.NestedScrollView>
还有一种情况,【NestScrollView】嵌套了一个【RecyclerView】,【RecyclerView】中又嵌套了一个【RecyclerView】,此时第二个【RecyclerView】就无法滑动了,此时可以对第二个【RecyclerView】设置属性允许滑动,就可以继续滑动了:
itemBinding.mineRvChild.setNestedScrollingEnabled(false);
如果出现了AppBarLayout无法上下滑动的情况的话(一般是RecyclerView中滑动一下,切换ViewPager2,然后直接对AppBarLayout进行触摸,发现无法滑动),这种滑动冲突的处理方法是:给ViewPager2设置一个页面滑动监听,当发生页面切换时,重新设置一下AppBarLayout为可以滑动。
- binding.supermarketVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
- @Override
- public void onPageSelected(int position) {
- super.onPageSelected(position);
- // 左右切换页面后,要重新设置AppBarLayout为可以滑动
- CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) binding.appBar.getLayoutParams();
- AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) layoutParams.getBehavior();
- behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
- @Override
- public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
- return true;
- }
- });
- }
- });
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。