android-fragments 回收站视图在ViewPager中不滚动

yeotifhr  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(156)

我有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>

ViewPager 2设置

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.OnItemTouchListener获取回收视图焦点的实现
  • 已将片段布局更改为约束布局。

我正在耗尽的想法和谷歌搜索的解决方案。你能建议为什么我的recyclerview是不工作的时候,我试图滚动触摸项目,为什么它只工作的双方。如果我错过了什么后,请让我知道。

bvn4nwqk

bvn4nwqk1#

你需要从Media Fragment Layout中删除这些行。我看不出这些行有什么用途。

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

相关问题