如何将OnClickListener设置为RecyclerView中的两个按钮?

时间:2017-08-03 03:34:31

标签: android android-layout android-recyclerview recycler-adapter android-viewholder

在我的Android应用程序中,我有一个活动来显示RecyclerView,其中每行由TextView和2个按钮组成。就像那样:

ListAdapter Layout

嗯,经过多次互联网解释,我已经制作了这个适配器:

public class ListAdapter extends 
RecyclerView.Adapter<ListAdapter.ViewHolder> {

    private LayoutInflater layoutInflater;
    protected Vector<List> lists;

    public ListAdapter(Context context, Vector lists) {
        layoutInflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.lists = lists;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.listadapter, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        List list = lists.elementAt(position);
        holder.name.setText(list.getName());
        //holder.delete.set HOW DO I GET BUTTON HERE?
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder 
implements View.OnClickListener {
        public TextView name;
        public Button edit;
        public Button delete;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.listname);
            edit = (Button) itemView.findViewById(R.id.edit);
            delete = (Button) itemView.findViewById(R.id.delete);

            edit.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.edit:
                    break;
                case R.id.delete:
                    break;
                default:
                    break;
            }
        }
    }
}

我没有在recyclerview的每个视图中找到有2个按钮(只有添加图像的示例)的示例,如第一张图片中所示。我的意思是,猜测我的RecyclerView中有10个List元素,我怎么能在每一个中都有一个带有List和2个按钮名称的TextView并处理clickListener?。如下图所示,我亲手为你看看我在说什么:

enter image description here

这是显示recyclerView的活动;不要把它考虑在内,因为我很确定方法是错的。我从sqlite数据库获取列表信息:

public class ShowListOfLists extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_list_of_lists);
    LocalDB localDB = new LocalDB(this, "localBD", null, 1);
    Vector <List> ListAdapter = localDB.getAllPrivateList();

    recyclerView = (RecyclerView) findViewById(R.id.recyclerviewlistas);
    recyclerView.setAdapter(new ListAdapter(this, ListAdapter));
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
}

列表具有以下属性:

-idList int
-name String
-Description String
-creationDate Date
-active int (0 or 1)
-deactivationDate Date

提前谢谢。

/ ******** EDIT ************** ********** /

谢谢你我能够展示recyclerView并且效果很好。但我明白了:

recyclerviewresult

而不是:

enter image description here

这是适配器的XML代码和设计:

ListAdapter Layout

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/rectangle_bg"
android:orientation="horizontal"
android:weightSum="1"
android:layout_marginTop="5dp">

<TextView
    android:id="@+id/listname"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_weight="0.75"
    android:gravity="center_vertical"
    android:text="TextView"
    android:textSize="15sp"/>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.25"
    android:gravity="end"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/delete"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="@android:color/transparent"
        android:contentDescription="Delete"
        app:srcCompat="@android:drawable/ic_menu_delete" />

    <ImageButton
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="@android:color/transparent"
        android:contentDescription="Edit"
        app:srcCompat="@android:drawable/ic_menu_edit" />

    </LinearLayout>

</LinearLayout>

这是recyclerview的XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    tools:context="com.pc.kanayel.runoutof.ShowListOfLists"
    android:orientation="vertical"
    android:id="@+id/recyclerviewlistas">

</android.support.v7.widget.RecyclerView>

2 个答案:

答案 0 :(得分:10)

您只需向ListAdapter添加一些代码即可。它就在那里,你必须实现onClickListener。您的案例代码应如下所示。

您可以传递所需的任何参数。这取决于您想要实现的功能。在这里,我演示了在列表中单击项目的顺序。

选项1(功效/性能效率)

那么,下面的代码究竟意味着什么?您已创建ViewHolder并已实施OnClickListener。那是正确的。现在您需要将OnClickListener设置为两个按钮。但点击时这些按钮的作用由我们在interface内创建的ViewHolder定义。

当应用程序运行RecyclerView时,将通过为每个查看者调用onCreateViewHolder()方法创建尽可能多的ViewHolders,以填充可用屏幕和列表项。当您向上/向下滚动时,这些ViewHolders将被重复使用。未调用OnCreateViewHolder(),仅调用onBindViewHolder()来更新viewholder的内容。下面的代码使得,当创建ViewHolder时,它还将创建MyClickListener,将由OnClickListener的查看者使用,并且当重新使用视图持有者时,它将不会创建新的OnClickListener。这意味着我们的方法具有高效性。

选项2(效率不高)

您还可以在setOnClickListener()onBindViewHolder()。但是,正如我在上面的段落中所提到的,每次滚动以更新viewholder的内容时都会调用此方法。现在它每次都会创建新的OnClickListener对象。虽然选项1 在每个ViewHolder上都有OnClickListener,但它会重复使用它们。

选项1的代码

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {

    private LayoutInflater layoutInflater;
    protected Vector<List> lists;

    public ListAdapter(Context context, Vector lists) {
        layoutInflater = (LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.lists = lists;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.listadapter, null);
        ViewHolder = holder = new ViewHolder(view, new ViewHolder.MyClickListener() {
            @Override
            public void onEdit(int p) {
                // Implement your functionality for onEdit here
            }

            @Override
            public void onDelete(int p) {
                // Implement your functionality for onDelete here
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        List list = lists.elementAt(position);
        holder.name.setText(list.getName());
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        MyClickListener listener;

        TextView name;
        Button edit;
        Button delete;

        public ViewHolder(View itemView, MyClickListener listener) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.listname);
            edit = (Button) itemView.findViewById(R.id.edit);
            delete = (Button) itemView.findViewById(R.id.delete);

            this.listener = listener;

            edit.setOnClickListener(this);
            delete.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.edit:
                    listener.onEdit(this.getLayoutPosition());
                    break;
                case R.id.delete:
                    listener.onDelete(this.getLayoutPosition());
                    break;
                default:
                    break;
            }
        }

        public interface MyClickListener {
            void onEdit(int p);
            void onDelete(int p);
        }
    }
}

编辑第二个问题

要制作列表项,请将width的{​​{1}}设置为recyclerview。将它作为某种布局的子视图也更好。例如,如下所示。

match_parent

更改适配器内的此代码:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pc.kanayel.runoutof.ShowListOfLists">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerviewlistas"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

<RelativeLayout>

到此:

View view = layoutInflater.inflate(R.layout.listadapter, null);

答案 1 :(得分:1)

你可以这样做:

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        List list = lists.elementAt(position);
        holder.name.setText(list.getName());
        holder.edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Do your stuff
                }
            });
        holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Do your stuff
                }
            });
    }