Recyclerview 未在 ViewPager 内滚动

问题描述 投票:0回答:2

我有

ViewPager2
和 3 个片段,所有片段都有
recyclerview
但在中间片段 recyclerview 中,如果我尝试触摸 recyclerview 项目,则不会滚动,但如果我触摸回收器的右侧或左侧,它会平滑滚动,如图所示。

查看寻呼机布局

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabItemes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="center"
            android:clipToPadding="true"
            app:tabPaddingBottom="0dp"
            app:tabPaddingEnd="0dp"
            app:tabPaddingStart="0dp"
            app:tabPaddingTop="0dp"
            app:tabIndicatorHeight="0dp"
            app:tabMode="fixed"
            app:tabTextColor="@color/black"
            app:tabSelectedTextColor="@color/primary"
            app:tabIndicatorColor="@color/primary_light">
            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="About" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Media" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Connections" />
        </com.google.android.material.tabs.TabLayout>
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/profileViewPager"
            android:orientation="horizontal"
            android:nestedScrollingEnabled="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

ViewPager2 设置

userInfo_ViewPager = view.findViewById(R.id.profileViewPager);
        userInfo_ViewPager.setOffscreenPageLimit(1);
        userProfile_tabAdapter = new UserProfile_TabAdapter(getChildFragmentManager(), getLifecycle());
        TabLayout tabLayout = view.findViewById(R.id.tabItemes);

媒体片段布局

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="true"
    android:nestedScrollingEnabled="true"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:id="@+id/seeAllPhotos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textSize="12sp"
        android:textStyle="bold"
        android:text="See All"
        />

    <com.elaxer.CustomClass.RotateLoading
        android:id="@+id/rotateloading"
        android:layout_width="80dp"
        android:layout_height="80dp"
        app:loading_color="@color/greenElaxer"
        android:layout_gravity="center_horizontal|center_vertical"
        app:loading_speed="11"
        app:loading_width="5dp" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/userPhotos_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        tools:listitem="@layout/recycler_view_item_8"
        tools:spanCount="5"
        tools:layoutManager="GridLayoutManager" />


</LinearLayout>

媒体片段

 public class UserPhoto_fragment extends Fragment {
        RecyclerView photosRecycler;
        StaggeredGridLayoutManager layoutManager;
        List<UserGrid> ImageList;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.userprofile_photos,container,false);
            photosRecycler=view.findViewById(R.id.userPhotos_recycler);
            seeAllPhotos = view.findViewById(R.id.seeAllPhotos);
            StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    
            layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
            ImageList=new ArrayList<>();
            photosRecycler.addItemDecoration(new SpacesItemDecoration(16));
            photosRecycler.setLayoutManager(layoutManager);
            photosRecycler.setHasFixedSize(true);
            adapter=new fetchPhoto_Adapter();
            photosRecycler.setAdapter(adapter);
           
            return view;
        }
    
       
    
        public class fetchPhoto_Adapter extends RecyclerView.Adapter<fetchPhoto_Adapter.ViewHolder>{
    
            @NonNull
            @Override
            public fetchPhoto_Adapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                LayoutInflater inflater= (LayoutInflater) viewGroup.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View v = inflater.inflate(R.layout.userprofile_photogallery,viewGroup,false);
                return new ViewHolder(v);
            }
    
            @Override
            public void onBindViewHolder(fetchPhoto_Adapter.ViewHolder viewHolder, int i) {
                UserGrid userImages = ImageList.get(i);
                Glide.with(getActivity()).load(userImages.getWeb_URL()).into(viewHolder.image);
            }
    
            @Override
            public int getItemCount() {
                if (ImageList!=null && ImageList.size()>0){
                    return ImageList.size();
                }else {
                    return 0;
                }
            }
    
            public class ViewHolder extends RecyclerView.ViewHolder {
                ImageView image;
                public ViewHolder(View itemView) {
                    super(itemView);
                    image = itemView.findViewById(R.id.UserProfile_photoThumb);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                image.setClipToOutline(true);
            }
                }
            }
    
        }

private final RecyclerView.OnItemTouchListener itemTouchListener = new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
            int action = e.getAction();
            Log.d(TAG, "onInterceptTouchEvent: "+action);
            if (photosRecycler.canScrollVertically(1)){
                if (action == MotionEvent.ACTION_MOVE){
                    photosRecycler.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }else {
                if (action == MotionEvent.ACTION_MOVE){
                    photosRecycler.requestDisallowInterceptTouchEvent(false);
                    photosRecycler.removeOnItemTouchListener(this);
                }
                return true;
            }

        }

        @Override
        public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    };
    
    }

我已经尝试过:

  • 在 Recyclerview 和 Viewpager 中启用/禁用嵌套 ScrollView
  • RecyclerView.OnItemTouchListener
    实现获得 recyclerview 焦点
  • 将片段布局更改为约束布局。

我已经没有想法了,谷歌搜索解决方案。您能否建议为什么当我尝试通过触摸项目滚动时我的 recyclerview 不起作用以及为什么它只能在两侧工作。 如果我错过任何要发布的内容,请告诉我。

android android-fragments android-recyclerview android-viewpager android-adapter
2个回答
0
投票

您需要从

Media Fragment Layout
中删除这些行。我看不出这些有什么目的。

android:isScrollContainer="true"
android:nestedScrollingEnabled="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"

0
投票

定义 DisallowParentSwipeOnItemTouchListener:

import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs

class DisallowParentSwipeOnItemTouchListener : RecyclerView.OnItemTouchListener {
    var startPoint = 0f
    override fun onInterceptTouchEvent(
        rv: RecyclerView,
        e: MotionEvent
    ): Boolean {

        when (e.action) {
            MotionEvent.ACTION_DOWN -> {
                startPoint = e.x
            }
            MotionEvent.ACTION_MOVE -> {
                val pointX = e.x
                if (abs(pointX - startPoint) > 5f) {
                    //scrolling horizontally
                    rv.parent.requestDisallowInterceptTouchEvent(true)
                }
            }
            MotionEvent.ACTION_UP,
            MotionEvent.ACTION_CANCEL -> {
                rv.parent.requestDisallowInterceptTouchEvent(false)
            }
        }
        return false
    }
    override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
}

将监听器附加到

RecyclerView
:

val listener = DisallowParentSwipeOnItemTouchListener()
binding.recyclerView.addOnItemTouchListener(listener)
© www.soinside.com 2019 - 2024. All rights reserved.