-1

我有一个RecyclerView用户必须单击元素才能对该特定玩家投票的地方。


当前布局

当前布局


由于它不是那么直观,我想Button在每个元素的右侧添加 s 以让用户了解他需要点击才能投票。


问题

我该如何做这样的定制layout?可能使用GridLayout? 大多数情况下,当按钮(并且只有它)被点击时,我如何获得Button's elementposition


代码

4

2 回答 2

0

用这个替换你的 CoursesAdapter 的 onBindViewHolder。

 @Override
    public void onBindViewHolder(CoursesViewHolder holder, int position) {
        Player player = mArrayCourses.get(position);
        holder.name.setText(player.getName());
        holder.counter.setText(String.valueOf(player.getCount()));
        holder.voteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do your work here
            }
        });

    }
于 2016-08-22T08:39:32.983 回答
0

您可以使用LinearLayout通过赋予它们weight来将按钮添加到您的Recyclerview项目。

您可以在 ViewHolder 类中处理按钮的 onClicklistener ,并且可以通过ViewHolder类中的getAdapterPosition()获取单击按钮的位置。

按您的要求 :

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="ABCD"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="A"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Vote"/>

</LinearLayout>

适配器 :

public class Holder extends RecyclerView.ViewHolder{
        Button  btnVote;
        public Holder(View itemView) {
            super(itemView);
            btnVote = (Button) itemView.findViewById(R.id.btn_vote);

            btnVote.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //list.get(getAdapterPosition()); Use for get the data on selected item
                }
            });
        }
    }
于 2016-08-22T08:22:55.280 回答