我刚刚在循环视图中实现了可扩展布局。ExpandableLayout library
问题:第一次回收视图滚动速度慢。 expand / collpase工作正常,但列表滚动缓慢
注意: 它在
4 GB RAM
设备上顺利运行但在2 GB RAM
设备上滚动延迟。我测试了Moto g3(2 GB RAM)和Moto g5 Plus(4 GB RAM)
来自Web服务并通过适配器传递数组列表的数据。
嵌套线性布局中的布局行文件(XML)。(可能是它的视图组加载,但我也尝试使用约束布局。仍然是recycleview load laggy)
我在扩展和折叠项目视图期间替换视图(我已经注释了该代码。但我的回收视图仍然很慢)
在itemview中没有使用任何存储的图像。
已尝试setHasFixedSize
,notifyDataChanged
,setCache
,adapter.setHasStableIds(true);
但仍然只有第一次的recycleview加载滞后。
帮我解决这个问题。我仍然在发现这个问题! 您可以查看我的适配器代码!
public class PendingOrdersAdapter extends RecyclerView.Adapter<PendingOrdersAdapter.ViewHolder> {
public LayoutInflater mInflater;
public HashSet<Integer> mExpandedPositionSet = new HashSet<>();
Context context;
ArrayList<CustomerDetails> list;
CustomerDetails details;
public PendingOrdersAdapter(Context context, ArrayList<CustomerDetails> list) {
this.context = context;
this.list = list;
this.mInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View item = mInflater.inflate(R.layout.pending_order_item, parent, false);
return new ViewHolder(item);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
details = list.get(position);
holder.updateItem(position, holder);
holder.tvOrderNo.setText("" + details.getOrderID());
holder.tvCustomerName.setText("" + details.getCustomerName());
holder.tvTotalPrice.setText("" + details.getTotalPrice());
holder.tvCustomerContactNo.setText("" + details.getPrimaryContactNo());
holder.tvProductWeight.setText("" + details.getProductWeight());
holder.tvCustomerStatus.setText("" + details.getCustomerStatus());
holder.tvDeliveryManStatus.setText("" + details.getDeliveryManStatus());
holder.tvAddress.setText("" + details.getAddress());
holder.tvDeliveryMan.setText("" + details.getCustomerName() + " ( " + details.getPrimaryContactNo() + " ) ");
holder.tvTime.setText("" + details.getTime());
holder.tvPickUpDate.setText("" + details.getPickupDate());
holder.tvDeliveryCharge.setText("" + details.getDeliveryCharge());
//Opening DialogFragment on Click Listner
holder.tvDeliveryMan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) (context);
android.app.FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
android.app.Fragment prev = activity.getFragmentManager().findFragmentByTag("dvdialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DeliveryManDetailsDialog newFragment = new DeliveryManDetailsDialog();
newFragment.show(ft, "dvdialog");
}
});
holder.tvAddress.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
//Address icon click listner right side
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (holder.tvAddress.getRight() - holder.tvAddress.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
Intent intent = new Intent(context, RouteActivity.class);
intent.putExtra("custLat", details.getCustLatitude());
intent.putExtra("custLong", details.getCustLongitude());
intent.putExtra("boLat", details.getBoLatitude());
intent.putExtra("boLong", details.getBosLongitude());
context.startActivity(intent);
}
return true;
}
return true;
}
});
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return list.size();
}
public void registerExpand(int position, ViewHolder holder) {
if (mExpandedPositionSet.contains(position)) {
//Replacing views at runtime and arrow animation
ViewGroupUtils.replaceView(holder.timeLayout, holder.statusLayout);
holder.orderbox.setBackgroundResource(R.drawable.box_fill_drawable);
holder.ivArrow.animate().rotation(360).start();
removeExpand(position);
} else {
ViewGroupUtils.replaceView(holder.statusLayout, holder.timeLayout);
holder.orderbox.setBackgroundResource(R.drawable.box_fill_drawable_top);
holder.ivArrow.animate().rotation(180).start();
addExpand(position);
}
}
public void removeExpand(int position) {
mExpandedPositionSet.remove(position);
}
public void addExpand(int position) {
mExpandedPositionSet.add(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ExpandableLayout expandableLayout;
public LinearLayout orderbox, orderbox_bottom;
public ImageView ivArrow;
public TextView tvAddress, tvOrderNo, tvCustomerName, tvTotalPrice, tvCustomerContactNo;
public TextView tvProductWeight, tvCustomerStatus, tvDeliveryManStatus;
public TextView tvDeliveryCharge, tvDeliveryMan, tvTime;
public TextView tvPickUpDate;
public LinearLayout statusLayout, statusParentLayout, timeLayout, timeParentLayout, addressLayout;
public ViewHolder(final View itemView) {
super(itemView);
expandableLayout = (ExpandableLayout)
itemView.findViewById(R.id.expandable_layout);
orderbox = (LinearLayout) itemView.findViewById(R.id.orderbox);
orderbox_bottom = (LinearLayout)
itemView.findViewById(R.id.orderbox_bottom);
ivArrow = (ImageView) itemView.findViewById(R.id.ivArrow);
tvOrderNo = (TextView) itemView.findViewById(R.id.tvOrderNo);
tvCustomerName = (TextView)
itemView.findViewById(R.id.tvCustomerName);
tvTotalPrice = (TextView)
itemView.findViewById(R.id.tvTotalPrice);
tvCustomerContactNo = (TextView)
itemView.findViewById(R.id.tvCustomerContactNo);
tvProductWeight = (TextView)
itemView.findViewById(R.id.tvProductWeight);
tvCustomerStatus = (TextView)
itemView.findViewById(R.id.tvCustomerStatus);
tvDeliveryManStatus = (TextView)
itemView.findViewById(R.id.tvDeliveryManStatus);
tvDeliveryCharge = (TextView)
itemView.findViewById(R.id.tvDeliveryCharge);
tvAddress = (TextView) itemView.findViewById(R.id.tvAddress);
tvDeliveryMan = (TextView)
itemView.findViewById(R.id.tvDeliveryMan);
tvTime = (TextView) itemView.findViewById(R.id.tvTime);
tvPickUpDate = (TextView)
itemView.findViewById(R.id.tvPickUpDate);
statusParentLayout = (LinearLayout)
itemView.findViewById(R.id.statusParentLayout);
statusLayout = (LinearLayout)
itemView.findViewById(R.id.statusLayout);
timeParentLayout = (LinearLayout)
itemView.findViewById(R.id.timeParentLayout);
timeLayout = (LinearLayout)
itemView.findViewById(R.id.timeDateLayout);
addressLayout = (LinearLayout)
itemView.findViewById(R.id.addressLayout);
}
public void updateItem(final int position, final ViewHolder holder) {
holder.orderbox.setBackgroundResource(R.drawable.box_fill_drawable_top);
holder.expandableLayout.setOnExpandListener(new ExpandableLayout.OnExpandListener() {
@Override
public void onExpand(boolean expanded) {
registerExpand(position, holder);
}
});
expandableLayout.setExpand(mExpandedPositionSet.contains(position));
}
}
从Fragment调用适配器
** PendingOrdersAdapter adapter = new PendingOrdersAdapter(getActivity(), detailsArrayList);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
recyclerview.setAdapter(adapter);
adapter.notifyDataSetChanged();**
答案 0 :(得分:0)
创建一个类,例如Util,并将此方法添加到类中并使用它。
public static void setDivider(Context context, RecyclerView recyclerView, RecyclerView.LayoutManager layoutManager) {
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
DividerItemDecoration horizontalDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
Drawable horizontalDivider = ContextCompat.getDrawable(context, R.drawable.horizontal_divider);
horizontalDecoration.setDrawable(horizontalDivider);
recyclerView.addItemDecoration(horizontalDecoration);
}
此处第一个参数为context
,第二个参数通过recyclerview
,第三个参数通过布局管理器。
如果您有更多内容,请更改CacheSize
答案 1 :(得分:0)
使用它
recyclerView.setNestedScrollingEnabled(false);