我有一个带有项目的可滚动列表视图。每个项目都有如下布局:
我根据需要显示或隐藏id为customer_menu_index
的LinearLayout。剩下的元素会在每个方法调用中显示。
<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>
我在BaseAdapter
的方法getView
中使用这种布局。getView方法的实现。
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;
}
加载后可以正常工作。但是当我滚动列表视图时,项的顺序发生了变化。
必须有一个匹配的else语句来显示或隐藏 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);
}
另外两点:
-
if(a.equals(b) == false)
与if(!a.equals(b))
相同。这只是一个快捷方式,你不必使用这个。 你也应该考虑使用ViewHolder来加速你的代码。