在ListView中實現button功能
阿新 • • 發佈:2018-12-05
/建立名為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;
}