RecyclerView当前可见项目的高度应大于下一个和上一个项目

Jun*_*san 7 java user-interface android android-recyclerview

我想显示recyclerview的下一个和上一个项目与当前可见项目(如在此第三方库中)相比的部分。但是我设法用我的本地recyclerview做到了。

这对我来说听起来很棒,现在我想将当前可见项目的高度设置为大于下一个和上一个项目的高度,如上述库的gif所示!

在以下阶段,我设法用代码显示了下一个和上一个项目:

1.将recyclerview适配器设置为:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());

layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

locationTypeFragmentBinding.mRecylerview.setLayoutManager(layoutManager);

locationTypeFragmentBinding.mRecylerview.setHasFixedSize(true);

final LocationTypePictureAdapter locationTypePictureAdapter =
   new LocationTypePictureAdapter(getActivity(), locationDetails,false);

final SnapHelper snapHelper = new PagerSnapHelper();

snapHelper.attachToRecyclerView(locationTypeFragmentBinding.mRecylerview);

locationTypeFragmentBinding.mRecylerview.post(new Runnable() {

    @Override
    public void run() {

    locationTypeFragmentBinding.mRecylerview.
      setAdapter(locationTypePictureAdapter);
    }
});
Run Code Online (Sandbox Code Playgroud)

2.我在xml中的RecyclerView是:

     <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecylerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonsHolder"
        android:clipToPadding="false"
        android:orientation="horizontal"
        android:padding="@dimen/_10sdp"
        android:paddingStart="@dimen/_15sdp"
        android:paddingEnd="@dimen/_15sdp"
        android:scrollbarStyle="outsideOverlay"
        android:visibility="gone"/>
Run Code Online (Sandbox Code Playgroud)

3.我的RecyclerView项目xml是:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/mainContainer"
       android:layout_width="match_parent"
       android:layout_height="@dimen/_200sdp">

    <android.support.v7.widget.CardView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/_5sdp"
        android:layout_marginEnd="@dimen/_5sdp"
        android:background="@android:color/transparent"
        app:cardElevation="0dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/locationPicture"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

4.我需要达到的目标是: 我知道我可以使用上述库,但是我想将当前可见项的高度设置为大于要显示给用户的recyclerview的下一项和上一项。 。

有人可以找出我的代码在做什么错吗?

我需要的示例图片

看这里

Tam*_*bul 3

我不熟悉您正在使用的库,但您也可以使用CarouselLayoutManager来实现这种外观,使用这个库,您将拥有一个高于所有其他项目的提升项目,并且该项目看起来比其他项目更大。

如何使用:

  • 在你的gradle中:

    implementation 'com.azoft.carousellayoutmanager:carousel:version'
    
    Run Code Online (Sandbox Code Playgroud)
  • 声明适配器时:

    final CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.VERTICAL);
    
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    
    Run Code Online (Sandbox Code Playgroud)

编辑:

如果你想在本地进行,你可以这样做:

  • 创建recyclerView:

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fadingEdge="horizontal"
    android:overScrollMode="never"
    android:requiresFadingEdge="horizontal" />
    
    Run Code Online (Sandbox Code Playgroud)
  • 将您的 recyclerView 附加到 SnapHelper,如下所示:

    LinearSnapHelper snapHelper = new LinearSnapHelper();
    snapHelper.attachToRecyclerView(recyclerView);
    
    Run Code Online (Sandbox Code Playgroud)
  • 现在您需要为当前居中的项目提供逻辑:

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            float position = (float) recyclerView.computeHorizontalScrollOffset() / (float) itemHeight;
            int itemPosition = (int) Math.floor(position);
        }
        super.onScrollStateChanged(recyclerView, newState);
        }
    });
    
    Run Code Online (Sandbox Code Playgroud)