我有一个带有多个视图的recyclerView,该视图的每一行都是一个recyclerView。当我滚动recyclerView时,在创建的每一行上都比较麻烦。这是我的一些代码。在下面,您可以看到主recyclerView的配置:
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mRvPromotions.setLayoutManager(mLayoutManager);
mRvPromotions.setItemViewCacheSize(20);
mRvPromotions.setDrawingCacheEnabled(true);
mRvPromotions.setHasFixedSize(true);
mRvPromotions.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
mRvPromotions.setAdapter(adapter);
,您将在下面看到绑定在适配器中的内部recyclerView的代码:
mRvPromotion.setLayoutManager(new GridLayoutManager(itemView.getContext(), 3));
mRvPromotion.setItemViewCacheSize(20);
mRvPromotion.setDrawingCacheEnabled(true);
mRvPromotion.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
adapter = new PlaylistPromotionsRVAdapter(new PlaylistModelCallBack(), new PlaylistPromotionVHFactory());
mRvPromotion.setAdapter(adapter);
adapter.submitList(getVM().getPlaylist());
我使用mRvPromotion.setNestedScrollingEnabled(false);
和ViewCompat.setNestedScrollingEnabled(childRecyclerView, false);
,但存在时滞行为钢。
对于加载映像,我使用了Fresco并调整了其配置。
如何防止滚动滞后?
编辑
这是我的适配器:
public class PlaylistRVAdapter extends ListAdapter<PlayListPromotionAndSlider, BaseViewHolder> {
private final PlaylistPageVHFactory mFactory;
private final PublishSubject<PlaylistSliderVHAction> mClickSliderPS = PublishSubject.create();
private final PublishSubject<PlaylistPromotionsVHAction> mClickPromotionPS = PublishSubject.create();
@Inject
PlaylistRVAdapter(@NonNull PlaylistPromotionModelCallBack diffCallback, PlaylistPageVHFactory factory) {
super(diffCallback);
mFactory = factory;
}
@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return mFactory.create(parent, viewType);
}
@Override
public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
switch (getItemViewType(position)) {
case 0:
PlaylistSliderVH sliderVH = (PlaylistSliderVH) holder;
sliderVH.getVM().setObject(getItem(position).getSlide());
sliderVH.bind();
sliderVH.itemOnClick(mClickSliderPS);
break;
case 1:
PlaylistPromotionsVH promotionsVH = (PlaylistPromotionsVH) holder;
promotionsVH.getVM().setObject(getItem(position).getPromotion());
promotionsVH.bind();
promotionsVH.itemOnClick(mClickPromotionPS);
break;
}
}
public Observable<PlaylistSliderVHAction> getClickPS() {
return mClickSliderPS;
}
public PublishSubject<PlaylistPromotionsVHAction> getmClickPromotionPS() {
return mClickPromotionPS;
}
@Override
public int getItemViewType(int position) {
return getItem(position).getSlide() != null ? 0 : 1;
}
}
例如,我的viewHolder PlaylistPromotionsVH如下:
public class PlaylistPromotionsVH extends BaseViewHolder<PlaylistPromotionsVHAction, PlaylistMasterModel, PlaylistPromotionsVM> {
@BindView(R.id.txtPromotionTitle)
AppCompatTextView mTxtPromotionTitle;
@BindView(R.id.txtMore)
AppCompatTextView mTxtMore;
@BindView(R.id.rvPromotion)
RecyclerView mRvPromotion;
PlaylistPromotionsRVAdapter adapter;
public PlaylistPromotionsVH(View itemView, PlaylistPromotionsVM viewModel) {
super(itemView, viewModel);
ButterKnife.bind(this, itemView);
}
@Override
public void bind() {
mTxtPromotionTitle.setText(mVM.getPlaylistName());
mRvPromotion.setLayoutManager(new GridLayoutManager(itemView.getContext(), 3));
mRvPromotion.setItemViewCacheSize(20);
mRvPromotion.setDrawingCacheEnabled(true);
mRvPromotion.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
mRvPromotion.setHasFixedSize(true);
int spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12,
itemView.getContext().getResources().getDisplayMetrics());
mRvPromotion.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int spanCount = 3;
if (position >= 0) {
int column = position % spanCount; // item column
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = 0;
outRect.right = 0;
outRect.top = 0;
outRect.bottom = 0;
}
}
});
adapter = new PlaylistPromotionsRVAdapter(new PlaylistModelCallBack(), new PlaylistPromotionVHFactory());
mRvPromotion.setAdapter(adapter);
adapter.submitList(getVM().getPlaylist());
}
@Override
public void itemOnClick(PublishSubject<PlaylistPromotionsVHAction> actionSubject) {
RxView.clicks(mTxtMore)
.map(o -> new PlaylistPromotionsVHAction(getAdapterPosition()))
.repeat()
.subscribe(actionSubject);
}
}
在下面,您可以看到主recyclerView每行的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@color/colorPromotionBackground"
android:descendantFocusability="blocksDescendants"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:id="@+id/rlPromotionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/txtPromotionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:layout_toRightOf="@+id/txtMore"
android:gravity="right"
android:textColor="@android:color/white"
tools:ignore="RtlHardcoded" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/txtMore"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/ic_see_more" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvPromotion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rlPromotionTitle"
android:focusableInTouchMode="true"
android:overScrollMode="never"
android:scrollbars="none"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
更新
这是我用壁画加载图像的代码:
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
.setResizeOptions(new ResizeOptions(width, height))
.build();
simpleDraweeView.setController(
Fresco.newDraweeControllerBuilder()
.setOldController(simpleDraweeView.getController())
.setImageRequest(request)
.build());
答案 0 :(得分:3)
首先,您可以将{{1}}用于嵌套的RecyclerView的预取功能。有关更多信息,请检查此article
我认为,主要原因是从服务器加载图像会阻塞主线程。您可以异步执行此操作。请检查此article。
答案 1 :(得分:1)
自然会出现此问题,因为同时需要进行滚动操作,要解决此问题,我建议仅通过向每个孩子添加LayoutManger来禁用所有孩子的滚动操作
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false){
@Override
public boolean canScrollVertically() {
return false;
}
};
如果孩子持有网格布局管理器像这样禁用它
new GridLayoutManager(getContext(),2){
@Override
public boolean canScrollVertically() {
return false;
}
} ;