[英]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.