android - 如何在自定义 ListView 中顺畅滚动?

标签 android listview

我的自定义 ListView 滚动不够顺畅,我在该网站上查找的所有解决方案都超出了我的理解范围。非常愿意根据之前的问题进行解释的人与提供解决方案的人一样受到欢迎。这是我的代码:

public class MainActivity extends AppCompatActivity {
String []titles = {"CAPTAIN AMERICA", "DOCTOR STRANGE", "THOR", "SPIDERMAN"};
String []desciption = {"CHRIS EVANS", "BENEDICT CUMBERBATCH", "CHRIS HEMSWORTH", "TOM HOLLAND"};
int []image = {R.drawable.capamerica, R.drawable.doctorstrange, R.drawable.thor, R.drawable.spidey};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = findViewById(R.id.listview);
    MyAdapter adap = new MyAdapter(this, titles, desciption, image);
    lv.setAdapter(adap);
}

class MyAdapter extends BaseAdapter {
String []title;
String []desc;
Context context;
int []img;;
LayoutInflater inflater;

public MyAdapter(MainActivity mainActivity, String[] titles, String[] desciption, int[] image)
{
    context = mainActivity;
    title = titles;
    desc = desciption;
    img = image;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return title.length;
}

@Override
public Object getItem(int i) {
    return i;
}

@Override
public long getItemId(int i) {
    return i;
}

public class Info {
    ImageView iv;
    TextView tv;
    TextView tv1;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    Info info = new Info();
    View v = inflater.inflate(R.layout.layout, null);
    info.iv = v.findViewById(R.id.imgview);
    info.tv = v.findViewById(R.id.titlestring);
    info.tv1 = v.findViewById(R.id.descriptionstring);
    info.iv.setImageResource(img[i]);
    info.tv.setText(title[i]);
    info.tv1.setText(desc[i]);
    return v;
}

最佳答案

使用 ViewHolder 设计模式: 您的代码在 ListView 滚动期间频繁调用 findViewById(),这会降低性能。即使当 Adapter 返回一个膨胀的 View 以供回收时,您仍然需要查找元素并更新它们。避免重复使用 findViewById() 的一种方法是使用“ View 持有者”设计模式。

这在 Google docs 下有明确提及

示例:

static class ViewHolder {
        private TextView title;
        private TextView description;
        private ImageView image;
    }

ViewHolder holder;
@Override
public View getView(int position, View view, ViewGroup parent) {

if (view == null) {
    view = inflator.inflate(R.layout.layout, null);
    holder = new ViewHolder();
    holder.title = (TextView) view.findViewById(R.id.titlestring);
    holder.description = (TextView) view.findViewById(R.id.descriptionstring);
    holder.image = (ImageView) view.findViewById(R.id.imgview);
    view.setTag(holder);

}else {
    holder = (ViewHolder) view.getTag();
}

holder.title.setText(titles[position]);
holder.description.setText(description[position]);
holder.image.setImageResource(image[position])
return view;
}

延迟加载图像: 实际上,当您滚动 ListView 时,每当一行可见时,就会触发 getView 函数。所以,如果用户快速上下滚动一个大的 ListView ,那么getView会被多次触发,滚动会不流畅。因此建议在单独的线程中进行图像加载,以便 ListView 的平滑滚动。

还要确保您创建的线程没有超过限制。根据用户可见的最大列表项数,建议限制为 6-7。

希望对您有所帮助。

关于android - 如何在自定义 ListView 中顺畅滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48807505/

相关文章:

java - "No such column name"错误

php - Android:如何使用参数从mysql获取数据到sqlite

c# - ListView 自动调整大小以显示所有项目

android - 滚动自定义ListView时Android应用程序崩溃

java - 如果 Eclipse 识别出方法上的警告,则无法显示方法定义

android - 在 Android 中创建 Fragment 时何时设置 ImageBitmap?

c# - 如何在 C# 中使用 Caliburn.Micro 更新 ListView 中的项目?

java - 如何将 ListView 保存为文本文档而不是将其加载回程序中

java - ANDROID - 删除 ListView 标题图像的多余空间

java - Canvas 对象必须与之前由 lockCanvas 返回的实例相同