سؤال

It is advised to use View Recycling and View Holder pattern while using list view in our app.But the problem I am facing after using View Recycling and View Holder Pattern is that when i try to scroll my list view the order of the list items disturbs/changes randomly. I don't know what is happening right now. Searched a lot but find nothing. In a severe need.. please help.. Thanks in advance..

هل كانت مفيدة؟

المحلول

First: You are not creating a new ViewHolder when convertView is null.

Second: You have a ViewHolder instance as a member variable of the class and you continually manipulate this ViewHolder. There should be one ViewHolder instance per row item on screen and you should create and manipulate the ViewHolder **only within getView()*.

Instead of this:

private ViewHolder holder;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    holder = ...;
    setData();
}

private void setData() {
    holder.tv_name.setText(...);
    // etc.
}

Do this:

// no private ViewHolder instance

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = ...; // create new or getTag
    setData(holder);
}

private void setData(ViewHolder holder) {
    holder.tv_name.setText(...);
    // etc.
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top