让代码更简单

当前位置:代码狗 > 开发教程 > Android开发教程 > 正文

Android自定义RecyclerView列表中按钮的点击事件监听实现

重要:本文最后更新于2019-04-19 08:30:54,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗

安卓APP开发中最常见的就是列表了,安卓实现列表的方法很多,比如listview,RecyclerView等。RecyclerView相当于listview的升级版,提供更多方法,提高灵活性。我们在自定义item中加入按钮等控件后,如何监听是点击了整个item还是点击的按钮呢?不管怎么说,要实现监听事件,数据适配器必须暴露接口才行,下面我们一起来看看如何监听自定义RecyclerView列表中按钮的点击事件。

Android自定义RecyclerView列表中按钮的点击事件监听实现

Android自定义RecyclerView列表中按钮的点击事件监听实现

item.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="#FFFFFF"
        android:orientation="vertical"
        android:paddingBottom="20dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="维修单号:"
                android:textSize="18dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/wxdh"
                android:text="20190417"
                android:textSize="18dp"
                tools:layout_editor_absoluteX="90dp"
                tools:layout_editor_absoluteY="0dp"
                tools:ignore="MissingConstraints" />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="设备名称:"
            android:textSize="16dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sbname"
            android:layout_marginBottom="5dp"
            android:text="AR机械手"
            android:textSize="16dp" />
</LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="设备编号:"
            android:textSize="16dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/sbcode"
                android:layout_marginBottom="5dp"
                android:text="CD-3443645756"
                android:textSize="16dp" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="故障地址:"
            android:textSize="16dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/address"
                android:layout_marginBottom="5dp"
                android:text="B05/1F"
                android:textSize="16dp" />
        </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="故障现象:"
            android:textSize="16dp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/content"
                    android:layout_marginBottom="5dp"
                    android:text="发生撞机"
                    android:textSize="16dp" />
            </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="#1ac107"
                android:id="@+id/send"
                android:text="立即发送"
                android:textColor="#FFFFFF"
                android:textSize="18dp" />

            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="#e11534"
                android:id="@+id/delete"
                android:text="删除"
                android:textColor="#FFFFFF"
                android:textSize="18dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

数据实体类

public class zxbx_dfd {
    private String wxdh, sbname, sbcode, address, content;

    public zxbx_dfd(String wxdh, String sbname, String sbcode, String address, String content) {
        this.wxdh = wxdh;
        this.sbname = sbname;
        this.sbcode = sbcode;
        this.address = address;
        this.content = content;
    }

    public String getWxdh() {
        return wxdh;
    }

    public String getSbname() {
        return sbname;
    }


    public String getSbcode() {
        return sbcode;
    }

    public String getAddress() {
        return address;
    }

    public String getContent() {
        return content;
    }
}

重点,适配器MyRecyclerViewAdapter 类

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> implements View.OnClickListener{

    private List<zxbx_dfd> list;//数据源
    private Context context;//上下文

    public MyRecyclerViewAdapter(List<zxbx_dfd> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.zxbx_fd_item,parent,false);
        return new MyViewHolder(view);
    }

    //绑定
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        zxbx_dfd data = list.get(position);
        holder.wxdh.setText(data.getWxdh());
        holder.sbname.setText(data.getSbname());
        holder.sbcode.setText(data.getSbcode());
        holder.address.setText(data.getAddress());
        holder.content.setText(data.getContent());

        holder.itemView.setTag(position);
        holder.send.setTag(position);
        holder.delete.setTag(position);
    }


    //有多少个item
    @Override
    public int getItemCount() {
        return list.size();
    }



    //创建MyViewHolder继承RecyclerView.ViewHolder
    public class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView wxdh,sbname,sbcode,address,content;
        private Button send,delete;

        public MyViewHolder(View itemView) {
            super(itemView);

            wxdh = (TextView) itemView.findViewById(R.id.wxdh);
            sbname = (TextView) itemView.findViewById(R.id.sbname);
            sbcode = (TextView) itemView.findViewById(R.id.sbcode);
            address = (TextView) itemView.findViewById(R.id.address);
            content = (TextView) itemView.findViewById(R.id.content);
            send = (Button) itemView.findViewById(R.id.send);
            delete = (Button) itemView.findViewById(R.id.delete);
            // 为ItemView添加点击事件
            itemView.setOnClickListener(MyRecyclerViewAdapter.this);
            send.setOnClickListener(MyRecyclerViewAdapter.this);
            delete.setOnClickListener(MyRecyclerViewAdapter.this);
        }

    }



    //=======================以下为item中的button控件点击事件处理===================================

    //item里面有多个控件可以点击
    public enum  ViewName {
        ITEM,
        PRACTISE
    }

    //自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener  {
        void onItemClick(View v, ViewName viewName, int position);
        void onItemLongClick(View v);
    }

    private OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并暴露给外面的调用者
    public void setOnItemClickListener(OnItemClickListener  listener) {
        this.mOnItemClickListener  = listener;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag(); //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()){
                case R.id.recycler_view:
                    mOnItemClickListener.onItemClick(v, ViewName.PRACTISE, position);
                    break;
                default:
                    mOnItemClickListener.onItemClick(v, ViewName.ITEM, position);
                    break;
            }
        }
    }
}

在Activity或Fragment中使用

public class bx_dfd extends Fragment {
    private List<zxbx_dfd> fruitList = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.baoxiu_d, null);
        fruitList.clear();
        for(int i=0;i<10;i++){
            fruitList.add(new zxbx_dfd("201904171826"+i,"AR机械手"+i,"CD-4235345436","B01/2F","撞机撞机"));
        }
        RecyclerView recyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(fruitList,getActivity());
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(View v, MyRecyclerViewAdapter.ViewName viewName, int position) {
        switch (v.getId()){
            case R.id.send:
                Toast.makeText(getActivity(),"你点击了发送按钮"+(position+1),Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(getActivity(),"你点击了删除按钮"+(position+1),Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getActivity(),"你点击了item"+(position+1),Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onItemLongClick(View v) {

    }
    });
        return mView;
    }
}

安卓中关于列表的使用必须要有三要素,数据实体类、数据适配器类、还有就是实现类,自定义的列表还需要item布局文件。

感觉很棒!可以赞赏支持我哟~

赞(2) 打赏

评论 抢沙发

×

请作者吃根烤肠!

支付宝