我不明白滚动浏览我的 RecyclerView 时刹车的原因是什么。滚动由 mRecyclerView.smoothScrollToPosition(position);
调用
当您滚动浏览抽搐列表时(渲染时间范围 > 16 毫秒)
AlphaAdapter.java
public class AlphaAdapter extends RecyclerView.Adapter<AlphaAdapter.ViewHolder> {
private static final String TAG = "AlphaAdapter";
private Context mContext;
private List<PrimaryWeapon> mGunList;
private boolean isAnimate;
private final int VIEW_TYPE_NULL = 0;
private final int VIEW_TYPE_ITEM = 1;
public AlphaAdapter(Context mContext, List<PrimaryWeapon> mGunList) {
this.mContext = mContext;
this.mGunList = mGunList;
}
public AlphaAdapter(Context mContext, List<PrimaryWeapon> mGunList, boolean a) {
this.mContext = mContext;
this.mGunList = mGunList;
this.isAnimate = a;
}
@Override
public int getItemViewType(int position) {
try {
if (mGunList.get(position).getNameWeapon().equals(""))
return VIEW_TYPE_NULL;
else return VIEW_TYPE_ITEM;
} catch (Exception e) {
e.printStackTrace();
Log.e("AlphaAdapter", String.valueOf(position));
return VIEW_TYPE_NULL;
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
if (viewType == VIEW_TYPE_ITEM)
view = LayoutInflater.from(mContext).inflate(R.layout.item_game, parent, false);
else
view = LayoutInflater.from(mContext).inflate(R.layout.item_null, parent, false);
return new ViewHolder(view);
}
@Override
public void onViewDetachedFromWindow(ViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.itemView.clearAnimation();
}
@Override
public boolean onFailedToRecycleView(ViewHolder holder) {
return true;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
try {
long startTime = System.currentTimeMillis();
if (!getWeapon(position).getNameWeapon().equals("")) {
if (isAnimate) {
if (mLastAnimPosition < position) {
mLastAnimPosition = position;
Animation a = AnimationUtils.loadAnimation(mContext, R.anim.item_game_anim);
holder.itemView.startAnimation(a);
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
Picasso.with(mContext)
.load("file:///android_asset/" + getWeapon(position).getImagePath())
.placeholder(R.drawable.loading)
.error(R.drawable.load_fail)
.into(holder.image);
holder.main.setText(getWeapon(position).getNameWeapon());
holder.desc.setText(getWeapon(position).getNameSkin());
}
Log.i(TAG, String.valueOf(System.currentTimeMillis() - startTime) + "ms onBind (" + String.valueOf(position) + ");");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public long getItemId(int position) {
return position;
}
private PrimaryWeapon getWeapon(int position) {
return mGunList.get(position);
}
@Override
public int getItemCount() {
return mGunList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView main, desc;
ImageView image;
ViewHolder(View itemView) {
super(itemView);
main = (TextView) itemView.findViewById(R.id.item_textMain);
desc = (TextView) itemView.findViewById(R.id.item_textDescription);
image = (ImageView) itemView.findViewById(R.id.item_image);
}
}
}
参与适配器的布局元素包括LinearLayout、ImageView、2个TextView。
一开始我以为是下载图片的问题,尝试将它们加载到AsyncTask中,通过Picasso和不上传位图,但全部去掉它们的标记后,列表仍然会滞后。
dump scroll后的RAM,显示原来ViewHolder有71个内存实例,添加后
@Override
public boolean onFailedToRecycleView(ViewHolder holder) {
return true;
}
这个值减少到 15-16,although the rate for RecyclerView - 5 复制,它应该覆盖它们,但是当你滚动时不会创建新的。
item_null.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="74dp"
android:layout_height="80dp">
</LinearLayout>
item_game.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:padding="2dp"
android:id="@+id/layout"
android:layout_height="80dp"
android:fitsSystemWindows="true"
android:layout_width="74dp">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="56dp"
/>
<TextView
android:layout_gravity="top"
android:layout_width="match_parent"
android:singleLine="true"
android:id="@+id/item_textMain"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="1dp"
android:gravity="left"
android:textColor="#FFF"
android:maxLines="1"
android:textSize="8sp"
android:layout_height="10dp" />
<TextView
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:id="@+id/item_textDescription"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:gravity="left"
android:textColor="#FFF"
android:singleLine="true"
android:maxLines="1"
android:maxLength="40"
android:textSize="6sp"
android:layout_height="12dp" />
</LinearLayout>
最佳答案
您正在将上下文传递给适配器。首先,这可能会导致内存泄漏,也可能会影响您的性能。无需将上下文传递给适配器,只需简单地从 ViewHolder 获取它即可。您始终可以在 RecyclerView.Adapter 中获取上下文引用,而无需传递它。
To dump the RAM after the scroll is, it shows that there are 71 memory instances originally ViewHolder.
从转储来看,这很可能是这种情况。
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
...
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Context context = holder.itemView.getContext();
...
}
关于java - RecyclerView 滚动时滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41923817/