package org.yaoming.listview;
import org.yaoming.util.Common;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class Listview_selectorActivity extends Activity implements OnItemClickListener {
private String[] arrays;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arrays = new String[] {"a","b","c","d","e"};
//使用SharedPreferences是为了每次点击退出时都保存之前选中的项,以后一打开时就可以看到以前的选择
SharedPreferences sp = getSharedPreferences("testListview", 0);
Common.SELECTED = sp.getInt("selected", Common.SELECTED);
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(new MyAdapter(this));
listview.setOnItemClickListener(this);
}
public class MyAdapter extends BaseAdapter{
Context context;
public class ViewHolder{
TextView tv;
ImageView iv;
}
public MyAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrays.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//通过日志测试,原来每次点击listview的每一项,这里都会运行
//通过查看源码,原来是getItemAtPosition()中getAdapter()搞的鬼
Log.i("listview", "zhixingguo ");
LinearLayout layout;
ViewHolder viewholder;
if(null == convertView){
layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item, null);
viewholder = new ViewHolder();
viewholder.tv = (TextView) layout.findViewById(R.id.textView2);
viewholder.iv = (ImageView) layout.findViewById(R.id.imageView1);
convertView = layout;
layout.setTag(viewholder);
}else{
layout = (LinearLayout) convertView;
viewholder = (ViewHolder) layout.getTag();
}
viewholder.tv.setText(arrays[position]);
//在这里加个判断,若为选中项,则改变背景图片和背景色
if(Common.SELECTED == position){
viewholder.iv.setBackgroundResource(R.drawable.asi);
layout.setBackgroundResource(R.color.blue);
}else {
viewholder.iv.setBackgroundResource(R.drawable.icon);
layout.setBackgroundResource(R.color.transparent);
}
return convertView;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//如果在这里简单地通过findviewbyid()改变背景图片,点击时会出现图片乱显示的情况
// ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
// iv.setImageResource(R.drawable.asi);
//改变SELECTED的值,并在getview里判断加载的位置是否为选中的位置
Common.SELECTED = position;
}
}
评论7
最新资源