coolhack7
coolhack7

Reputation: 1224

How do I get to scroll smoothly through a customized list view?

My custom List View just won't scroll smoothly enough and all the solutions that I looked up on this site were beyond my understanding.People who are more than willing to explain from previous questions are equally welcome as those providing the solution.Here's my code:

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

}

Upvotes: 1

Views: 74

Answers (1)

Sudhin Philip
Sudhin Philip

Reputation: 644

Using ViewHolder design pattern: Your code is calling findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.

This is clearly mentioned under Google docs

Example:

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

Lazyloading of images: Actually, when you scroll list view, each time a row is visible, then getView function will get triggered. So, if a user scroll a large list view fast up and down, then getView will be triggered many times and scrolling will not be smooth. So it is advised to carry out image loading in a separate thread for smooth scrolling of list view.

Also make sure, you are not creating threads more than a limit. Recommended limit is 6-7 based on maximum number of list items visible to the user.

Hope it helps.

Upvotes: 1

Related Questions