简体   繁体   English

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

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

I wanted to show the part of next and previous items of the recyclerview as compared to the currently visible item ( as in this third party library ). 我想显示recyclerview的下一个和上一个项目的部分与当前可见项目( 如第三方库中 )相比。 However I managed to do so with my native recyclerview. 然而,我设法用我的原生Recyclerview这样做。

That's sound great to me, now I want to set the height of my currently visible item larger than the next and previous items to it as shown in the gif of the above mentioned library! 这对我来说听起来很棒,现在我想将当前可见项目的高度设置为大于下一个和前一个项目的高度,如上面提到的库的gif所示!

I have managed to show the next and previous items with my code as in the following stages: 我已设法使用我的代码显示下一个和上一个项目,如以下阶段:

1. Setting the recyclerview adapter as: 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. My RecyclerView in the xml is: 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. My RecyclerView item xml is: 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. What I need to achieve is like: I know I can use the above mentioned library, but I want to set the height of the currently visible item larger than the next and previous items of my recyclerview which are to be shown to the user. 4.我需要实现的是:我知道我可以使用上面提到的库,但是我想设置当前可见项目的高度大于我的recyclerview的下一个和前一个项目,这些项目将显示给用户。

Can somebody please figure out what I am doing wrong with my code? 有人可以弄清楚我的代码出了什么问题吗?

Example Image of what I need 示例我需要的图像

See Here 看这里

I am not familiar the library that you are using but You can also use CarouselLayoutManager to achieve this look, with this library you will have one elevated item that will be above all others and this will look bigger than the rest of the items. 我不熟悉您正在使用的库,但您也可以使用CarouselLayoutManager来实现这种外观,使用此库,您将拥有一个高于其他项目的高架项目,这看起来比其他项目更大。

How to use: 如何使用:

  • In your gradle: 在你的gradle中:

     implementation 'com.azoft.carousellayoutmanager:carousel:version' 
  • When declaring an adapter: 声明适配器时:

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

Edit: 编辑:

If you want to do it natively you can do something like this: 如果你想本地做,你可以做这样的事情:

  • Create recyclerView: 创建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" /> 
  • Attach your recyclerView to SnapHelper like this: 将您的recyclerView附加到SnapHelper,如下所示:

     LinearSnapHelper snapHelper = new LinearSnapHelper(); snapHelper.attachToRecyclerView(recyclerView); 
  • Now you need to provide the logic for your currently centered item: 现在,您需要为当前居中的项目提供逻辑:

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

I had added animation to items of a recycleView using the below method. 我使用以下方法将动画添加到recycleView的项目中。 I hope it may solve your problem if you are able to do something similar to this 如果你能做类似的事情,我希望它可以解决你的问题

Try this changes in your onBindViewHolder of your recycleView adapter. 在您的recycleView适配器的onBindViewHolder中尝试此更改。

@Override
    public void onBindViewHolder(@NonNull final CustomViewHolder holder, int position)
{
    // do your work

    setAnimation(holder.itemView, position);
}

and in setAnimation which receives the view and its 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;
    }

Similarly, if you know the position or location of view which is in middle, you can use your anim to zoom it up or zoom down other views in the recycleView. 同样,如果您知道位于中间的视图的位置或位置,则可以使用您的动画来放大或缩小recycleView中的其他视图。

If you cary about performance it is the best to do all animations things in Layout Manager. 如果您对性能感到不满,最好在布局管理器中执行所有动画操作。 Tamir Abutbul gives you very good solution, why you want to reinvent the wheel? Tamir Abutbul为您提供了非常好的解决方案,为什么要重新发明轮子? Without library there is ready layout manager you need : https://stackoverflow.com/a/41307581/2551094 没有库,您需要的是现成的布局管理器: https//stackoverflow.com/a/41307581/2551094

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

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