简体   繁体   English

使 ListView 平滑滚动

[英]Make ListView Scroll Smoothly

I have the following code to load my ListView (Each Item Has an Image and Text):我有以下代码来加载我的 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;
}

The problem is the list isn't scrolling smoothly.问题是列表滚动不顺畅。 How do I make it smooth?我如何使它平滑?

EDIT: I have changed to RecycleView but I forgot to add the following code:编辑:我已更改为 RecycleView 但我忘记添加以下代码:

if(//IF//){ 
    Log.d("InInIn!", "InInIn!"); 
    
    //Turn to Tagged
}

When I turn it into a RecycleView, The button turns Tagged all over the pace and the tagged buttons change as a scroll.当我把它变成一个 RecycleView 时,按钮会在整个节奏中变成 Tagged 并且被标记的按钮会随着滚动而变化。 The buttons are changing all over the place even though they shouldn't.即使它们不应该改变,按钮也在各处发生变化。 Whats the issue?这是什么问题?

You should use recyclerView instead.您应该改用recyclerView It is a more advanced and flexible version of ListView.它是 ListView 的更高级和更灵活的版本。 Go through https://developer.android.com/guide/topics/ui/layout/recyclerview for guide.通过https://developer.android.com/guide/topics/ui/layout/recyclerview获取指南。

If you want to stick with ListView then in order to improve the performance you should recycle/reuse the views that are no longer visible to the user.如果您想坚持使用 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;
}

You can debug and check what recived to Picasso, you add: un listener for watch the error您可以调试和检查接收到毕加索的内容,您添加:un listener for watch the error

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

            @Override
            public void onError(Exception e) {
            }
        })
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;
}

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

try this:尝试这个:

This is the new and updated getView scroll faster这是新的和更新的 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM