Solution to the Problem selected by listview item

Source: Internet
Author: User

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!

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);    }    @Overridepublic 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:

public class ListAdapter extends BaseAdapter {private Context mContext;private View mLastView;private int mLastPosition;public ListAdapter(Context context) {this.mContext = context;}@Overridepublic int getCount() {return 8;}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic 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:

<?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:

<?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>

The layout was simply written and two pictures of the European Cup were created.

Final code: http://download.csdn.net/detail/aomandeshangxiao/4384922

If it is helpful, just pin it down below.

In fact, if there is more than one screen, there will still be problems. Have you participated in the discussion ???

If the listview item contains a button, you want to respond to the onitemclicklistener () method by adding:

android:descendantFocusability= "blocksDescendants"

The changes are as follows:

import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class ListAdapter extends BaseAdapter {private Context mContext;private int mLastPosition = -1;public ListAdapter(Context context) {this.mContext = context;}@Overridepublic int getCount() {return 100;}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Holder holder = new Holder();;if(convertView == null ) {LayoutInflater inflater = LayoutInflater.from(mContext);convertView = inflater.inflate(R.layout.list_item, null);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);if (position == mLastPosition) {    holder.hint.setVisibility(View.VISIBLE);} else {    holder.hint.setVisibility(View.GONE);}return convertView;}class Holder {TextView textView;ImageView UEFAView;ImageView mascotView;View hint;}public void changeImageVisable(int position) {    if(position != mLastPosition) {        mLastPosition = position;    } else {        mLastPosition = -1;    }    notifyDataSetChanged();}}

What's the difference?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.