In Android Application Development, this is often the case that a listView contains N items. When you click an item, this item is expanded to show some hidden controls in this item, click again. This item is removed and some controls are hidden again. When one item is opened, click another item, and the other item is expanded, the item is closed. (Let's look at it)
In last year, my own article (http://blog.csdn.net/aomandeshangxiao/article/details/6643831), there is a problem of Item selection, the method is stupid, to traverse it again, setting all items should be a waste of resources. Another problem is that when there is more than one screen item in the listview, repeated selection will occur, that is, when you select a slide, you may find that one of the items that appears after sliding is also in the selected status. This problem is very maddening. See the method on the Internet: convertview is not used in getView of the adapter. Each view is re-created. It can solve the problem, but it is still a waste of resources.
First look: The second item is selected
Item 4 is selected:
With the help of others, I am trying to share it!
[Java]
Public class ListViewTestActivity extends Activity implements OnItemClickListener {
Private ListView mListView;
Private ListAdapter mAdapter;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MListView = (ListView) findViewById (R. id. list );
MAdapter = new ListAdapter (this );
MListView. setAdapter (mAdapter );
MListView. setOnItemClickListener (this );
}
@ Override
Public void onItemClick (AdapterView <?> Parent, View view, int position, long id ){
MAdapter. changeImageVisable (view, position );
}
}
The onItemClick method calls the custom changeImageViewVisable method in the Custom ListAdapter.
See ListAdapter:
[Html]
Public class ListAdapter extends BaseAdapter {
Private Context mContext;
Private View mLastView;
Private int mLastPosition;
Public ListAdapter (Context context ){
This. mContext = context;
}
@ Override
Public int getCount (){
Return 8;
}
@ Override
Public Object getItem (int position ){
Return null;
}
@ Override
Public long getItemId (int position ){
Return 0;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
Holder holder;
If (convertView = null ){
LayoutInflater inflater = LayoutInflater. from (mContext );
ConvertView = inflater. inflate (R. layout. list_item, null );
Holder = new Holder ();
Holder. textView = (TextView) convertView. findViewById (R. id. textView );
Holder. UEFAView = (ImageView) convertView. findViewById (R. id. image_uefa );
Holder. mascotView = (ImageView) convertView. findViewById (R. id. image_mascot );
Holder. hint = convertView. findViewById (R. id. hint_image );
ConvertView. setTag (holder );
} Else {
Holder = (Holder) convertView. getTag ();
}
Holder. textView. setText ("Hello, It is" + position );
Return convertView;
}
Class Holder {
TextView textView;
ImageView UEFAView;
ImageView mascotView;
View hint;
}
Public void changeImageVisable (View view, int position ){
If (mLastView! = Null & mLastPosition! = Position ){
Holder holder = (Holder) mLastView. getTag ();
Switch (holder. hint. getVisibility ()){
Case View. VISIBLE:
Holder. hint. setVisibility (View. GONE );
Break;
Default:
Break;
}
}
MLastPosition = position;
MLastView = view;
Holder holder = (Holder) view. getTag ();
Switch (holder. hint. getVisibility ()){
Case View. GONE:
Holder. hint. setVisibility (View. VISIBLE );
Break;
Case View. VISIBLE:
Holder. hint. setVisibility (View. GONE );
Break;
}
}
}
The bottom of the Code is the changeImageVisable method. (Note: In this method, there are minor differences between the Blog Code version and the download code version. The Blog Code has better performance than the download code version, which also reflects the superiority of the Holder class, we must make good use of Holder. We should think about it. Why is the write performance better? You are welcome to leave a message for discussion ).
Layout file:
Main. xml:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "ListView Test"/>
<ListView
Android: id = "@ + id/list"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
> </ListView>
</LinearLayout>
List_item.xml:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
>
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Test"
Android: textSize = "20sp"
/>
<LinearLayout
Android: id = "@ + id/hint_image"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: visibility = "gone">
<ImageView
Android: id = "@ + id/image_uefa"
Android: layout_width = "0dp"
Android: layout_height = "60dp"
Android: layout_weight = "1"
Android: src = "@ drawable/uefa"
/>
<ImageView
Android: id = "@ + id/image_mascot"
Android: layout_width = "0dp"
Android: layout_height = "60dp"
Android: layout_weight = "1"
Android: src = "@ drawable/mascot"
/>
</LinearLayout>
</LinearLayout>
Author: aomandeshangxiao