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

Posted

技术标签:

【中文标题】RecyclerView 当前可见项高度应大于下一个和上一个项【英文标题】:RecyclerView currently visible item's height should be larger than the next and previous items 【发布时间】:2019-11-22 14:14:35 【问题描述】:

我想显示 recyclerview 的下一个和上一个项目与当前可见项目 (as in this third party library) 相比的部分。不过,我设法用我的原生 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);
    
);

2。我在 xml 中的 RecyclerView 是:

     <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecylerview"
        android:layout_
        android:layout_
        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"/>

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_
       android:layout_>

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

        <RelativeLayout
            android:layout_
            android:layout_>

            <ImageView
                android:id="@+id/locationPicture"
                android:layout_
                android:layout_/>

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

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

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

我需要的示例图片

See Here

【问题讨论】:

你为什么不改用ViewPager?有很多你需要的库 【参考方案1】:

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

使用方法:

在你的毕业典礼中:

implementation 'com.azoft.carousellayoutmanager:carousel:version'

声明适配器时:

final CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.VERTICAL);

final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);

编辑:

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

创建recyclerView:

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_
android:layout_
android:fadingEdge="horizontal"
android:overScrollMode="never"
android:requiresFadingEdge="horizontal" />

像这样将你的 recyclerView 附加到 SnapHelper:

LinearSnapHelper snapHelper = new LinearSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);

现在您需要为当前居中的项目提供逻辑:

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);
    
);

【讨论】:

好吧,由于性能下降问题,我不想使用任何第三方库! 我已经使用这个库很长时间了,我的表现从未受到这个库的影响。有时最好寻找已经编好的东西,而不是自己重新发明***。 感谢您的帮助,但我必须说它不是一个干净的库! 请问有什么不干净的地方吗?我们说的是 4 行代码 表现!我不想降低性能,因为在我提到的库中,它给了我想要的用户界面,但性能降低了!【参考方案2】:

我使用以下方法为 recycleView 的项目添加了动画。如果您能够做类似的事情,我希望它可以解决您的问题

在您的 recycleView 适配器的 onBindViewHolder 中尝试此更改。

@Override
    public void onBindViewHolder(@NonNull final CustomViewHolder holder, int position)

    // do your work

    setAnimation(holder.itemView, position);

在 setAnimation 中接收视图及其位置

void setAnimation(View view, int position) 
//this allows new views coming in the recycle view to slide to left while scrolling down and slide to right while scrolling up.
        if (lastPosition < position) 
            Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
            view.startAnimation(animation);
         else 
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_in_right);
            view.startAnimation(animation);
        
        lastPosition = position;
    

同样,如果您知道中间视图的位置或位置,您可以使用动画放大或缩小recycleView 中的其他视图。

【讨论】:

【参考方案3】:

如果您关心性能,最好在布局管理器中执行所有动画操作。 Tamir Abutbul 给你很好的解决方案,你为什么要重新发明***?如果没有库,您需要准备好布局管理器:https://***.com/a/41307581/2551094

【讨论】:

以上是关于RecyclerView 当前可见项高度应大于下一个和上一个项的主要内容,如果未能解决你的问题,请参考以下文章

如何获取回收站当前可见项目位置

RecyclerView 自动滚动到 WebView/Fresco SimpleDraweeView 项

尝试自动刷新当前页面视图时从ViewPager获取下一页面视图

在键盘打开时将项目添加到 RecyclerView 时向下滚动

如何将回收器高度设置为 recyclerView 中的最高项目?

Android RecyclerView 添加和删除项