先看效果圖 後端
1.recyclerview1嵌套recyclerview2,adpter1是recyclerview1的適配器,adapter2是recyclerview2的適配器2.頁面解析:截圖那部分是recyclerview1的一個item,畫紅框的是recyclerview2,能夠明顯看到,圖片是recyclerview2的item 緩存
3.個人問題點:adpter1中onBindViewHolder方法中直接new一個adapter2,而後設置到recyclerview2上,致使每次刷新adapter都是一個新的對象。網上說解決圖片閃爍的處理方法:(mAdapter.setHasStableIds(true);) + (重寫adpater的getItemId,保證itemId的惟一性)+調用notifyDataSetChanged()bash
還有:給imageview.setTag()app
(實際上是能夠解決的,解決不了的話,確定自身代碼還有問題,好比我這個例子)ide
這些方法對我來講都沒有做用,每次斷點進去getTag都是null,我就納了悶了。後來修改了adpter1中的代碼: 在onBindViewHolder方法中,判斷onBindViewHolder的適配是否爲null,爲空再new動畫
if (helper.itemOrder_GoodInfor.getAdapter() == null) {
OrderListGoodsAdapter1 adapter = new OrderListGoodsAdapter1(mContext);
adapter.setHasStableIds(true);
GridLayoutManager managerBtn = new GridLayoutManager(mContext, 1);
helper.itemOrder_GoodInfor.setLayoutManager(managerBtn);
((SimpleItemAnimator) helper.itemOrder_GoodInfor.getItemAnimator()).setSupportsChangeAnimations(false);
helper.itemOrder_GoodInfor.getItemAnimator().setChangeDuration(0);// 經過設置動畫執行時間爲0來解決閃爍問題
helper.itemOrder_GoodInfor.setTag(R.id.itemOrder_GoodInfor, item.getId());
helper.itemOrder_GoodInfor.setAdapter(adapter);
helper.itemOrder_GoodInfor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
return helper.itemView.onTouchEvent(event);
}
});
// helper.itemOrder_GoodInfor.setOnItemClickListener(new ByRecyclerView.OnItemClickListener() //{
// @Override
// public void onClick(View v, int position) {
// Object tag = helper.itemOrder_GoodInfor.getTag(R.id.itemOrder_GoodInfor);
// MyOrderDetailActivity.start(mContext, (int) tag);
// }
// });
// adapter.setNewData(item.getGoods());
adapter.updateData(item.getGoods());
} else {
((OrderListGoodsAdapter1) helper.itemOrder_GoodInfor.getAdapter()).updateData(item.getGoods());
}
複製代碼
adapter2我這邊是OrderListGoodsAdapter1部分代碼以下:給Imageview orderCenter_iv設置setTagui
public void updateData(List<OrderGroup.GoodsBean> dataSet) {
this.listData = dataSet;
notifyDataSetChanged();
}
private void onBindMainViewHolder(ViewHolder binding, int position) {
OrderGroup.GoodsBean item = listData.get(position);
/**
* 解決滑動recyclerview致使glide圖片加載閃爍
* 1.利用setTag
* 2.Glide設置skipMemoryCache(true),跳過緩存
*/
String tag = (String)binding.orderCenter_iv.getTag(R.id.orderCenter_iv);
String image = item.getImage();
if (item.getSpec_image() != null && !"".equals(item.getSpec_image())){
image = item.getSpec_image();
}
if (!image.equals(tag)) {
// 加載圖片
// GlideUtil.imageUrl(articleViewHolder.iv, bean.getImage(), height, height);
Glide.with(mContext).load(image)
.transform(new CropSquareTransformation())
.dontAnimate()
.skipMemoryCache(false)
// .apply(options)
.into(binding.orderCenter_iv);
binding.orderCenter_iv.setTag(R.id.orderCenter_iv,image);
}
String tag1 = (String)binding.orderCenter_iv.getTag(R.id.orderCenter_iv);
binding.orderCenter_goodName.setText(TextViewUtils.noEmpty(item.getGoods_name()));
binding.tv_model.setText(TextViewUtils.noEmpty(item.getGoods_attr()));
binding.tv_price.setText(TextViewUtils.noEmpty(item.getGoods_price()));
binding.tv_count.setText("x" + +item.getTotal_num());
}
複製代碼
那麼列表刷新圖片就不會閃爍,可是點擊item項進入商品詳情,發現數據錯亂了!!!!this
原來我是在recyclerview2中setOnItemClickListener,因此獲取的商品id都是錯誤的,後來改在recyclerview1中setOnItemClickListener,發現點擊被嵌套的列表recyclerview2沒有反應。原來是recyclerview嵌套recyclerview致使的衝突。最終解決方法: recyclerview2設置setOnTouchListener監聽,將焦點放棄,給recyclerview1的item處理spa
helper.itemOrder_GoodInfor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
return helper.itemView.onTouchEvent(event);
}
});
複製代碼
而後在adapter1中設置item的點擊監聽code
helper.itemView.setOnClickListener(view -> {
MyOrderDetailActivity.start(mContext, item.getId());
});
複製代碼
好了,完美解決! 注意點:由於後端偷懶,這個接口並無作分頁處理,因此分頁是否會原由其餘問題,須要本身處理哦。