使ListView平滑滚动

时间:2018-07-23 02:36:46

标签: android list listview android-asynctask

我有以下代码来加载ListView(每个项目都有一个图像和文本):

public View getView(final int i, View convertView, ViewGroup viewGroup) {
        View view = inflater.inflate(R.layout.contact_inflater, null);
        final Holder holder = new ContactAdapter.Holder();

        holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage);
        holder.tvFullName = (TextView) view.findViewById(R.id.tvName);
        holder.tvPhoneNumber = (TextView) view.findViewById(R.id.tvPhoneNo);
        holder.tvFullName.setText(Contact.get(i).get(0));
        holder.tvPhoneNumber.setText(Contact.get(i).get(1));
        holder.button = (Button) view.findViewById(R.id.ivButton);

        Picasso.with(context)
                    .load(Contact.get(i).get(2))
                    .into(holder.ivPhoto);

        return view;
    }

问题是列表无法平滑滚动。如何使它平滑?

编辑: :我已更改为RecycleView,但是我忘记添加以下代码:

if(//IF//){ 
Log.d("InInIn!", "InInIn!"); 

//Turn to Tagged

}

当我将其变成RecycleView时,该按钮将在整个过程中变为“已标记”,并且已标记的按钮会随着滚动而变化。按钮在整个地方都在改变,即使它们不应该改变。有什么问题吗?

5 个答案:

答案 0 :(得分:3)

您应该改用recyclerView。它是ListView的更高级和更灵活的版本。通过https://developer.android.com/guide/topics/ui/layout/recyclerview获得指导。

如果您要坚持使用ListView,那么为了提高性能,您应该回收/重用用户不再可见的视图。

public View getView(final int i, View convertView, ViewGroup viewGroup) {

        Holder holder = null;
        if(convertView == null){
          convertView = inflater.inflate(R.layout.contact_inflater, null);
          final Holder holder = new ContactAdapter.Holder();
          holder.ivPhoto = (ImageView) convertView.findViewById(R.id.ivImage);
          holder.tvFullName = (TextView) convertView.findViewById(R.id.tvName);
          holder.tvPhoneNumber = (TextView) convertView.findViewById(R.id.tvPhoneNo);
          holder.button = (Button) convertView.findViewById(R.id.ivButton);
          convertView.setTag(holder)
        }
        else{
            holder = (Holder)convertView.getTag()
        } 

        holder.tvFullName.setText(Contact.get(i).get(0));
        holder.tvPhoneNumber.setText(Contact.get(i).get(1));

        Picasso.with(context)
                    .load(Contact.get(i).get(2))
                    .into(holder.ivPhoto);

        return convertView;
    }

答案 1 :(得分:0)

您可以调试并检查Picasso收到了什么内容,然后添加:Un listener观看错误

        .into(ontact.get(i).get(2), new Callback() {
            @Override
            public void onSuccess() {
            }

            @Override
            public void onError(Exception e) {
            }
        })

答案 2 :(得分:0)

public View getView(final int i, View convertView, ViewGroup viewGroup) {
  Holder holder;
  if(null == convertView){
    holder = new ContactAdapter.Holder();
    convertView = inflater.inflate(R.layout.contact_inflater, null);
    holder.ivPhoto = (ImageView) view.findViewById(R.id.ivImage);
    holder.tvFullName = (TextView) view.findViewById(R.id.tvName);
    holder.tvPhoneNumber = (TextView) view.findViewById(R.id.tvPhoneNo);
    holder.button = (Button) view.findViewById(R.id.ivButton);
    converView.setTag(holder);
  }else{
   holder = (Holder) convertView.getTag();
  }
   holder.tvFullName.setText(Contact.get(i).get(0));
   holder.tvPhoneNumber.setText(get(i).get(1));
    Picasso.with(context)
                    .load(Contact.get(i).get(2))
                    .into(holder.ivPhoto);
return view;
}

//////////

答案 3 :(得分:0)

您将不得不在列表适配器中使用数据绑定并获取布局组件,而无需在getView方法中使用findViewById。

答案 4 :(得分:0)

尝试一下:

这是新的和更新的getView滚动速度更快

@Override
public View getView(int position, View convertView, ViewGroup paramViewGroup) {
Object current_event = mObjects.get(position);
ViewHolder holder = null;

if (convertView == null) {
    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.row_event, null);
    holder.ThreeDimension = (ImageView) convertView.findViewById(R.id.ThreeDim);
    holder.EventPoster = (ImageView) convertView.findViewById(R.id.EventPoster);
    // This will now execute only for the first time of each row
    RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(measuredwidth, rowHeight);
    holder.EventPoster.setLayoutParams(imageParams);
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}

// This way you can simply avoid conditions
holder.ThreeDimension.setVisibility(object.getVisibility());

return convertView;
 }