繁体   English   中英

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

[英]RecyclerView currently visible item's height should be larger than the next and previous items

我想显示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);
    }
});

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"/>

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>

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

有人可以弄清楚我的代码出了什么问题吗?

示例我需要的图像

看这里

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

如何使用:

  • 在你的gradle中:

     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_width="match_parent" android:layout_height="wrap_content" 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); } }); 

我使用以下方法将动画添加到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中的其他视图。

如果您对性能感到不满,最好在布局管理器中执行所有动画操作。 Tamir Abutbul为您提供了非常好的解决方案,为什么要重新发明轮子? 没有库,您需要的是现成的布局管理器: https//stackoverflow.com/a/41307581/2551094

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM