不多说,上源码就行
private class RecentViewHolder {
TextView appName;
ImageView appIcon;
TextView appSize;
}
private List<ResolveInfo> mAppList;
private LayoutInflater mInflater;
private PackageManager pm;
public RecentAdapter(Context c, List<ResolveInfo> appList,
PackageManager pm) {
mAppList = appList;
this.pm = pm;
mInflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void clear(){
if(mAppList!=null){
mAppList.clear();
}
}
public int getCount() {
return mAppList.size();
}
@Override
public Object getItem(int position) {
return mAppList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
RecentViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.app_info_item, null);
holder = new RecentViewHolder();
holder.appName = (TextView) convertView.findViewById(R.id.app_name);
holder.appIcon = (ImageView) convertView
.findViewById(R.id.app_icon);
holder.appSize = (TextView) convertView.findViewById(R.id.app_size);
convertView.setTag(holder);
} else {
holder = (RecentViewHolder) convertView.getTag();
}
ResolveInfo appInfo = mAppList.get(position);
if (appInfo != null) {
String labelName = appInfo.loadLabel(pm).toString();
if (labelName != null) {
holder.appName.setText(labelName);
}
Drawable icon = appInfo.loadIcon(pm);
if (icon != null) {
holder.appIcon.setImageDrawable(icon);
}
}
return convertView;
}
public void remove(int position){
mAppList.remove(position);
this.notifyDataSetChanged();
}
}
再看一个,两个结合一起,才能悟道
* @author allin
*
*/
public class MyListView4 extends ListActivity {
private List<Map<String, Object>> mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = getData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("info", "google 1");
map.put("img", R.drawable.i1);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G2");
map.put("info", "google 2");
map.put("img", R.drawable.i2);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "G3");
map.put("info", "google 3");
map.put("img", R.drawable.i3);
list.add(map);
return list;
}
// ListView 中某项被选中后的逻辑
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.v("MyListView4-click", (String)mData.get(position).get("title"));
}
/**
* listview中点击按键弹出对话框
*/
public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("我的listview")
.setMessage("介绍...")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
public final class ViewHolder{
public ImageView img;
public TextView title;
public TextView info;
public Button viewBtn;
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder=new ViewHolder();
convertView = mInflater.inflate(R.layout.vlist2, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.info = (TextView)convertView.findViewById(R.id.info);
holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
holder.title.setText((String)mData.get(position).get("title"));
holder.info.setText((String)mData.get(position).get("info"));
holder.viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInfo();
}
});
return convertView;
}
}
}
如果ListView中的单个Item的view中存在checkbox,button等view,会导致ListView.setOnItemClickListener无效, 事件会被子View捕获到,ListView无法捕获处理该事件.
解决方法: 在checkbox、button对应的view处加
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
其中focusable是关键 从OnClickListener调用getSelectedItemPosition(),Click 和selection 是不相关的,Selection是通过D-pad or trackball 来操作的,Click通常是点击操作的。
arg2参数才是点击事件位置的参数
第二种方法 就是在自己的getView中
convertView = mInflater.inflate ( R.layout.list_item_text , null );
convertView.setClickable ( true );
convertView.setOnClickListener ( clickListener );
public OnClickListener myClickListener = new OnClickListener () {
public void onClick ( View v )
{
//code to be written to handle the click event
} };
刚刚在网上找到问题的关键,终于解决了:
如果你的自定义ListViewItem中有Button或者Checkable的子类控件的话,那么默认focus是交给了子控件,而ListView的Item能被选中的基础是它能获取Focus,也就是说我们可以通过将ListView中Item中包含的所有控件的focusable属性设置为false,这样的话ListView的Item自动获得了Focus的权限,也就可以被选中了,也就会响应onItemClickListener中的onItemClick()方法,然而将ListView的Item Layout的子控件focusable属性设置为false有点繁琐,我们可以通过对Item Layout的根控件设置其android:descendantFocusability=”blocksDescendant”即可,这样Item Layout就屏蔽了所有子控件获取Focus的权限,不需要针对Item Layout中的每一个控件重新设置focusable属性了,如此就可以顺利的响应onItemClickListener中的onItenClick()方法了。