在Android应用开发中,很多时候都会遇到这样的需求,一个listView,含有N项,当点击某项时,该项展开,显示该项中隐藏的某些控件,再点击,该项收回,重新隐藏部分控件,当一项打开状态,点击另一项,另一项展开,该项关闭。(说的有点绕,看下图)


 里面有Item的选择问题,用的方法比较笨,要遍历一遍,把所有的item全部都设置一下,应该是很浪费资源的。还有一个问题就是,当listview的item多于一个屏幕的时候,会出现重复选择问题,就是当你选中一项滑动的时候,可能会发现滑动后出现的某项也是在选中状态,这个问题令人十分抓狂。见网上有个方法是:在adapter的getView里面不使用convertview。每一个view都是重新创建一个。能够解决问题,但是还是有点浪费资源。

        

先看下效果图:第二项被选中

qlistview修改item内容_Encoding

第四项被选中:

qlistview修改item内容_android_02


        这个方法也是在他人的帮助下,努力得来,分享一下!


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);
	}
}



       

这里应该注意到与平常的不同,onItemClick方法里面调用了自定义ListAdapter里面的自定义changeImageViewVisable方法。

看ListAdapter:


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;
		}
	}

}



       

   代码的最下端是changeImageVisable方法。( 注意:在这个方法中,博客代码版本和提供下载代码版本中有细微差异,博客代码较下载代码性能更优,这也体现了Holder类的优越性,一定要好好用好Holder,应好好思考下,为什么这样写性能就更好些呢?欢迎留言讨论 )。

        布局文件:

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>



        

布局只是简单的写了下,弄了两张欧洲杯的图。

      最后代码下载地址:

如果是有所帮助,在下面微微顶一个吧。

      其实这样做的话,如果多于一屏,还是会出现问题。有没有参与讨论的???

      如果ListView的Item里面有Button,想要响应OnItemClickListener()方法,加上:


android:descendantFocusability= "blocksDescendants"






       改动如下:


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;
	}

	@Override
	public int getCount() {
		return 100;
	}

	@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 = 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();
	}
}