をスクロールしたときにこれは私のアダプターのコードです:ラグrecyclerview
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.ViewHolder> {
ArrayList<CatModel> objects_;
Context context;
Class res = R.drawable.class;
public class ViewHolder extends RecyclerView.ViewHolder {
TextView cat_text,cat_des;
ImageView cat_img;
public ViewHolder(View v) {
super(v);
cat_text = (TextView) v.findViewById(R.id.cat_txt);
cat_des = (TextView) v.findViewById(R.id.cat_des);
cat_img = (ImageView) v.findViewById(R.id.cat_img);
}
}
public CatAdapter(ArrayList<CatModel> arrayList, Context context) {
this.context = context;
objects_ = arrayList;
}
@Override
public CatAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cat_list_view, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.cat_text.setText(objects_.get(position).txt);
holder.cat_des.setText(objects_.get(position).des);
try {
Field field = res.getField(objects_.get(position).img);
int drawableId = field.getInt(null);
holder.cat_img.setImageDrawable(context.getResources().getDrawable(drawableId));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return objects_.size();
}
}
CatModel
クラス
public class CatModel {
public String txt,img,des;
}
CatModel.img
は、私がR.strings
に入れて、私のdrawables
のIDです。 私のすべての項目int arraylist
は約20項目です。私のdrawables
は、SVG
に最適化されています。しかし、私がスクロールしているとき、それは滑らかではありません。それを最適化するために私は何ができますか?
。それは遅くなることが知られており、リフレクションを回避するためにコードをリファクタリングします。 – Francesc
もっと説明できますか? @Francesc –
resオブジェクト(getField)で反射を使用しています。反射は本質的に遅く、おそらく16msの障壁の上にあなたを押しているでしょう。私はあなたが何をしようとしているのかはよく分かりませんが、あなたのドロウアブルのリストまたはハッシュマップを作成し、そのリストまたはハッシュマップを使ってドロウブルをその位置にリンクすることができるはずです。 – Francesc