在ListView中实现button功能

/创建名为my_listview的xml视图



    

/创建名为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> getData() {
        ArrayList> data = new ArrayList>();
        Map map;
        map = new HashMap();
        map.put("image", R.drawable.log);
        map.put("name", "王者农药");
        map.put("size", "30dp");
        map.put("button", "下载");
        data.add(map);
        map = new HashMap();
        map.put("image", R.drawable.log2);
        map.put("name", "吃鸡战场");
        map.put("size", "30dp");
        map.put("button", "下载");
        data.add(map);
        map = new HashMap();
        map.put("image", R.drawable.log3);
        map.put("name", "氪金");
        map.put("size", "30dp");
        map.put("button", "下载");
        data.add(map);
        return data;

    }

创建名为item的xml文件



    
    
        
        
    
    

创建名为MyAdapter的java文件
通过设置标签获得不同按钮从而实现不同点击事件

public class MyAdapter extends SimpleAdapter {
    //上下文
    Context context ;
    public MyAdapter(Context context,
                           List> 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;
        }

在ListView中实现button功能_第1张图片

你可能感兴趣的:(在ListView中实现button功能)