简体   繁体   中英

ListView scrolling smooth in Android?

I've seen a few similar questions, but couldn't solve my problem. I have a ListView with one ImageView and TextView. I'm using custom adapter and viewHolder pattern but my list still doesn't scroll smooth? I have already downloaded images in my mipmap folder, so I am not downloading them directly. I don't know what else can I do to improve fast scroll, like in Quora or any other professional ListView app? Here is my adapter:

class customAdapter extends BaseAdapter {

ArrayList<Object> itemList;
Activity context;
public LayoutInflater inflater;

public customAdapter(Activity context,ArrayList<Object> itemList) {
    super();

    this.context = context;
    this.itemList = itemList;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

private static class ViewHolder {
    TextView text;
    ImageView image;
}

@Override
public int getCount() {
    return itemList.size();
}

@Override
public Object getItem(int position) {
    return itemList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent){

    ViewHolder holder;
    if(convertView == null){
        holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.custom_row, parent, false);
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.image = (ImageView) convertView.findViewById(R.id.image);
        convertView.setTag(holder);
    } else
        // View is being recycled, retrieve the viewHolder object from tag
        holder = (ViewHolder) convertView.getTag();

        ListItem items = (ListItem) itemList.get(position);
        holder.text.setText(items.getText());
        holder.image.setImageResource(items.getImg());

        return convertView;
    }
}

Here is my custom_row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:id="@+id/image"
    android:layout_marginTop="25dp"
    android:layout_marginLeft="20dp"
    android:src="@mipmap/austria" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/text"
    android:layout_marginTop="45dp"
    android:layout_marginLeft="30dp"
    android:textColor="#ffffff"
    android:textStyle="bold" />

And here is the sample of pictures I'm using: http://img.freeflagicons.com/thumb/round_icon/austria/austria_640.png

Check the size of the images. Loading bigger sized images impact listview performance.

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