在ListView中实现button功能
/创建名为my_listview的xml视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view_2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
/创建名为MyListView的java文件
新建adapter对象
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview);
ListView listview = (ListView) findViewById(R.id.list_view_2);;
MyAdapter adapter = new MyAdapter(this,getData(),R.layout.item,
new String[]{"image", "name", "size", "button"},
new int[]{R.id.image, R.id.name, R.id.size, R.id.button});
listview.setAdapter(adapter);
listview.setOnItemClickListener(new ListView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MyListView.this,"我是item点击事件 i = " + i + "l = " + l,Toast.LENGTH_SHORT).show();
}
});
}
private ArrayList<Map<String,Object>> getData() {
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
map = new HashMap<String, Object>();
map.put("image", R.drawable.log);
map.put("name", "王者农药");
map.put("size", "30dp");
map.put("button", "下载");
data.add(map);
map = new HashMap<String, Object>();
map.put("image", R.drawable.log2);
map.put("name", "吃鸡战场");
map.put("size", "30dp");
map.put("button", "下载");
data.add(map);
map = new HashMap<String, Object>();
map.put("image", R.drawable.log3);
map.put("name", "氪金");
map.put("size", "30dp");
map.put("button", "下载");
data.add(map);
return data;
}
创建名为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="horizontal"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="@+id/image"
android:src="@drawable/log"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_weight="3">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:textColor="#130000"
android:textSize="18dp"/>
<TextView
android:id="@+id/size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:textColor="#93851f"
android:text="65M"
android:textSize="20dp"/>
</LinearLayout>
<Button
android:layout_weight="1"
android:id="@+id/button"
android:layout_marginTop="12dp"
android:layout_width="20dp"
android:layout_height="40dp"
android:text="下载"
android:textSize="20dp"/>
</LinearLayout>
创建名为MyAdapter的java文件
通过设置标签获得不同按钮从而实现不同点击事件
public class MyAdapter extends SimpleAdapter {
//上下文
Context context ;
public MyAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
this.context = context;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup){
View view = super.getView(i, convertView, viewGroup);
final Button btn = (Button) view.findViewById(R.id.button);
btn.setTag(i);//设置标签
btn.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(View v) {
switch ((Integer)v.getTag()){
case 0:
Toast.makeText(getApplicationContext(), "点击的是ImageButton" + v.getTag(), Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "hi" + v.getTag(), Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "gey" + v.getTag(), Toast.LENGTH_SHORT).show();
break;
}
}
});
return view;
}