简体   繁体   中英

Make ListView Scroll Smoothly

I have the following code to load my ListView (Each Item Has an Image and Text):

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:

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. The buttons are changing all over the place even though they shouldn't. Whats the issue?

You should use recyclerView instead. It is a more advanced and flexible version of ListView. Go through https://developer.android.com/guide/topics/ui/layout/recyclerview for guide.

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.

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

        .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

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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