Android recyclerView在旧设备上无法平滑滚动,需要绑定很多视图
我有一个带有适配器的recyclerView,它正在为自定义布局折叠单元库充气。膨胀后,在onCreateViewHolder中,我初始化了许多视图,主要是文本视图,如下所示:Android recyclerView在旧设备上无法平滑滚动,需要绑定很多视图,android,performance,android-recyclerview,android-viewholder,Android,Performance,Android Recyclerview,Android Viewholder,我有一个带有适配器的recyclerView,它正在为自定义布局折叠单元库充气。膨胀后,在onCreateViewHolder中,我初始化了许多视图,主要是文本视图,如下所示: @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { FoldingCell v = (FoldingCell) LayoutInflater.from(parent.getC
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
FoldingCell v = (FoldingCell) LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_layout, parent, false);
ConstraintLayout continueBTN = v.findViewById(R.id.continueBTN);
TextView num = v.findViewById(R.id.num);
TextView name = v.findViewById(R.id.name);
TextView surname = v.findViewById(R.id.surname);
TextView add = v.findViewById(R.id.add);
TextView add2 = v.findViewById(R.id.add2);
TextView currency = v.findViewById(R.id.currency);
TextView currency2 = v.findViewById(R.id.currency2);
ConstraintLayout del = v.findViewById(R.id.DeleteICN);
ConstraintLayout rnme = v.findViewById(R.id.renameBtn);
TextView fText = v.findViewById(R.id.fText);
TextView tText = v.findViewById(R.id.tText);
TextView continue = v.findViewById(R.id.cnt);
TextView total= v.findViewById(R.id.total);
FoldingCell cell = v.findViewById(R.id.fcell);
ViewHolder vh = new ViewHolder(cell, continueBTN,num, name, surname, add, add2, price, currency, currency2, del, rnme, fText, tText, continue,total );
vh.bind(onCardClickListener);
return vh;
}
以上所有视图都可能通过BaaS进行更改,这就是为什么我要将它们插入viewHolder中(如果有意义的话)。cell view是保存上述其他视图的容器,我也需要初始化,因为如果回收的折叠单元格恰好展开,那么当再次在屏幕上滚动时,我希望它保持展开状态,因为默认情况下,折叠单元格是折叠的
因此,在onBindViewHolder中,我就是这样做的:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
// if this view holder position was left unfolded
if (unfoldedIndexes.contains(position)) {
// show it as unfolded again
holder.cell.unfold(true);
// only bind the views created in onCreateViewHolder that correspond to the unfolded state of the cell
bindUnfoldedState(holder, holder.getLayoutPosition());
}else{
holder.cell.fold(true);
// only bind the views created in onCreateViewHolder that correspond to the folded state of the cell
bindFoldedState(holder, holder.getLayoutPosition());
}
}
public void bindFoldedState(ViewHolder holder, int position){
CustomLayout customLayout = myArrayList.get(position);
String formattedPrice = context.getString(R.string.currency, customLayout.getP());
holder.currency.setText(formattedPrice);
holder.add.setText(customLayout.getConName());
holder.num.setText(customLayout.getNum());
holder.total.setText(""+customLayout.getTotal());
}
public void bindUnfoldedState(ViewHolder holder, int position){
holder.tText.setTextColor(Color.parseColor("#6F90CF"));
holder.fText.setText(location.getToTime());
// …
// and the rest of the views exactly the same way
if (location.getRequestBtnClickListener() != null) {
// seting some onClickListeners on views
}
}
这一切都很好。。在模拟器上。然而,在一台真正的旧设备上,使用的是2015 api 23 Galaxy A5,当一个新视图进入屏幕时,该应用程序会掉很多帧,使滚动抖动。设备上的开发工具显示绑定数据需要16毫秒以上的时间
为了证实这一点,我对InBindViewHolder中的所有代码进行了注释,并且滚动过程非常平滑
所以问题是,如何快速绑定上述所有数据?recyclerView中的每个元素都有可能通过BAA更改的所有上述子视图
我创建的布局中也没有最平坦的层次结构。我专门使用Constraint Layout,这可能会有所帮助,但我不认为问题源自层次结构,因为我在上面提到了onBindViewHolder测试。将onCreateViewHolder方法更新为:
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
FoldingCell v=LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_layout, parent, false);
return new ViewHolder(v);
}
创建一个视图保持架,如下所示:
public static class ViewHolder extends RecyclerView.ViewHolder {
public ConstraintLayout continueBTN
public TextView textView,name,surname,add;
public ViewHolder(View itemView) {
super(itemView);
continueBTN = (ConstraintLayout) itemView.findViewById(R.id.continueBTN);
num= (TextView) itemView.findViewById(R.id.num);
name= (TextView) itemView.findViewById(R.id.name);
surname= (TextView) itemView.findViewById(R.id.surname);
add= (TextView) itemView.findViewById(R.id.add);
/...../
}
}
如果回收器视图位于嵌套滚动视图内,请使用以下行:
recyclerView.setNestedScrollingEnabled(false);
按原样使用bindViewHolder。
希望这对您有所帮助基本上,您是在“OncreateViewholder”方法中找到您的视图的。因此,您必须在ViewHolder上找到您的视图。这可能会有所帮助,但在调用onBindViewHolder时,滚动仍然非常不平稳。Tery adapter.setHasStableIdstrue;它使用唯一的ID维护您的项目:并确保您的onBindViewHolder尽可能轻。