Android在Listview的一个item中设置多个控件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq356653246/article/details/72884871

最近在做项目时候,遇见一个需求,听上去还听简答的,Listview中肯定是有很多个item的,如何在每个item中设置多个按钮呢?设置一个的很简答,但是设置两个三个以及多个的呢?肯定是自己重新定义item布局再重写adpter了啦~实现出来的样子是这样的
这里写图片描述
首先来定义一个接口

public interface ListItemClickHelp {
    void onClick(View item, View widget, int position, int which);
}

然后接着自己写适配器,核心思想呢就是重写了接口中的onClick()方法,在OnClick中判断具体是哪个按钮被按下。

public class ListItemClickAdapter extends BaseAdapter {
    private Context contxet;
    private ArrayList<string> list;
    private ListItemClickHelp callback;
    private LayoutInflater mInflater;

    public ListItemClickAdapter(Context contxet, ArrayList<string> list,
            ListItemClickHelp callback) {
        this.contxet = contxet;
        this.list = list;
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        mInflater = (LayoutInflater) contxet
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.block_list_item, null);
            holder = new ViewHolder();
            holder.mAd_tv_show = (TextView) convertView
                    .findViewById(R.id.ad_tv_show);
            holder.mAd_btn_one = (Button) convertView
                    .findViewById(R.id.ad_btn_one);
            holder.mAd_btn_two = (Button) convertView
                    .findViewById(R.id.ad_btn_two);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mAd_tv_show.setText(list.get(position));

        final View view = convertView;
        final int p = position;
        final int one = holder.mAd_btn_one.getId();
        holder.mAd_btn_one.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onClick(view, parent, p, one);
            }
        });

        final int two = holder.mAd_btn_two.getId();
        holder.mAd_btn_two.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onClick(view, parent, p, two);
            }
        });

        return convertView;
    }

    public static class ViewHolder {
        TextView mAd_tv_show;
        Button mAd_btn_one;
        Button mAd_btn_two;
    }
}

在主页面调用时候,实现接口方法:

public class MainActivity extends Activity implements ListItemClickHelp {

    private TextView mtv_show;
    private ListView mlv_show;
    private ArrayList<string> mdata = new ArrayList<string>();
    private ListItemClickAdapter madapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initLayout();
    }

    private void initLayout() {
        mdata.add("One");
        mdata.add("Two");
        mdata.add("Three");
        mdata.add("Four");
        mdata.add("Five");
        mdata.add("菠萝");
        mdata.add("苹果");
        mdata.add("葡萄");
        mdata.add("哈密瓜");
        mdata.add("香蕉");

        mtv_show = (TextView) this.findViewById(R.id.tv_show);
        mlv_show = (ListView) this.findViewById(R.id.lv_show);

        madapter = new ListItemClickAdapter(this, mdata, this);
        mlv_show.setAdapter(madapter);

        mlv_show.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<!--?--> adapter, View view,
                    int position, long arg3) {
                mtv_show.setText(mdata.get(position));
            }
        });
    }

    @Override
    public void onClick(View item, View widget, int position, int which) {
        switch (which) {
        case R.id.ad_btn_one:
            mtv_show.setText(mdata.get(position) + "Btn one");
            break;
        case R.id.ad_btn_two:
            mtv_show.setText(mdata.get(position) + "Btn two");
            break;
        default:
            break;
        }
    }

}
展开阅读全文

没有更多推荐了,返回首页