简体   繁体   中英

Listview items change order after scrolling

I have scrollable Listview with items. Every item has following layout.

I show or hide LinearLayout with id customer_menu_index as necessary. Ramaining elements are displayed in every method calling.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:id="@+id/customer_menu_index" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/menu_listview_item_alphabet_bg" android:orientation="horizontal" android:visibility="gone"> <TextView android:id="@+id/customer_menu_index_letter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:paddingBottom="5dp" android:paddingTop="5dp" android:text="bla" android:textColor="@color/menu_listview_item_title" android:textSize="10sp" android:textStyle="bold" android:typeface="sans" /> </LinearLayout> <RelativeLayout android:id="@+id/customer_menu_item" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/menu_listview_item_selector" android:orientation="horizontal" > <TextView android:id="@+id/customer_menu_item_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="0dp" android:paddingBottom="0dp" android:paddingTop="5dp" android:textColor="@color/menu_listview_item_title" android:textSize="10sp" android:textStyle="bold" android:typeface="sans" /> <TextView android:id="@+id/customer_menu_item_subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/customer_menu_item_title" android:layout_marginLeft="5dp" android:layout_marginRight="0dp" android:paddingBottom="5dp" android:paddingTop="1dp" android:textColor="@color/menu_listview_item_subtitle" android:textSize="8sp" android:textStyle="normal" android:typeface="sans" /> </RelativeLayout> </LinearLayout> 

I use this layout in method getView of BaseAdapter . Implementation of getView method.

    public View getView(int position, View convertView, ViewGroup parentView) {

    View vi = convertView;

    if (convertView == null)
    {
        vi = inflater.inflate(R.layout.customer_menu_index_item, null);
    }

    LinearLayout indexLayout = (LinearLayout) vi.findViewById(R.id.customer_menu_index);

    HashMap<String, String> category = new HashMap<String, String>();
    category = mData.get(position);

    String currentIndexLetter = category.get(CustomerMenu.KEY_COMPANY).substring(0, 1);
    if (currentIndexLetter.equals(mLastIndexLetter) == false)
    {
        TextView letter = (TextView) vi.findViewById(R.id.customer_menu_index_letter);
        letter.setText(currentIndexLetter);
        mLastIndexLetter = currentIndexLetter;
        indexLayout.setVisibility(LinearLayout.VISIBLE);
    }

    TextView title = (TextView) vi.findViewById(R.id.customer_menu_item_title);
    TextView subtitle = (TextView) vi.findViewById(R.id.customer_menu_item_subtitle);
    title.setText(category.get(CustomerMenu.KEY_COMPANY));
    subtitle.setText(category.get(CustomerMenu.KEY_CONTACT_PERSON));

    return vi;
}

It works fine after loading. But when I scroll the listview, order of items is changed.

You must have a matching else statement to display or hide the LinearLayout:

if (currentIndexLetter.equals(mLastIndexLetter) == false)
{
    TextView letter = (TextView) vi.findViewById(R.id.customer_menu_index_letter);
    letter.setText(currentIndexLetter);
    mLastIndexLetter = currentIndexLetter;
    indexLayout.setVisibility(LinearLayout.VISIBLE);
}
else {
    indexLayout.setVisibility(LinearLayout.GONE);
}

Two other points:

  • if(a.equals(b) == false) is the same as if(!a.equals(b)) . This is simply a shortcut, you don't have to use this.
  • You should also consider using a ViewHolder to speed up your code.

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