您可以使用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
}
});
}
}