-1

I have a RecyclerView where the user has to click the elements to cast a vote to that specific player.


Current Layout

Current Layout


As it's not that much intuitive, I'd like to add Buttons on the right side of each element to let the user understand that he needs to click if he wants to cast a vote.


Question

How do I make a custom layout like that? Probably using a GridLayout? And mostly, how do I get the Button's element's position when the button (and it only) gets clicked?


Code

FET
  • 942
  • 4
  • 13
  • 35

2 Answers2

0

You can use LinearLayout to add button to your Recyclerview item by giving them weight.

You can handle onClicklistener of that button in ViewHolder class and you can get position of clicked button by getAdapterPosition() in ViewHolder class.

As per your request :

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>

Adapter :

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
                }
            });
        }
    }
SANAT
  • 8,489
  • 55
  • 66
  • May I ask you to leave me a brief snippet I can read and understand? Posted some code if you may need to have a look! – FET Aug 22 '16 at 08:30
  • The list is my RecyclerView? – FET Aug 22 '16 at 08:57
  • @FET is it useful for you ? – SANAT Aug 22 '16 at 09:09
  • I think, fact is I get all the data from a provider. To get such data I need to get this provider first. As I have all fragments, I usually do this: `private DataProvider provider = (DataProvider) getActivity();`. But this adapter class doesn't extend either fragment or activity, so I don't know how to get it and so get the data list – FET Aug 22 '16 at 09:12
  • @FET The adapter can't extend an Activity or a Fragment ! But you can have a reference inside the adapter to the right context. – Gabriele Mariotti Aug 22 '16 at 11:10
  • @GabrieleMariotti So what should I do in order to get the provider and finally the needed data? – FET Aug 22 '16 at 13:32
0

Replace your CoursesAdapter's onBindViewHolder with this.

 @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
            }
        });

    }
Bedant
  • 326
  • 1
  • 13
  • How do I get the position of the element clicked? – FET Aug 22 '16 at 08:46
  • public void onBindViewHolder(CoursesViewHolder holder, int position) here position is your current item position – Bedant Aug 22 '16 at 10:03
  • Fact is I get all the data from a provider. To get such data I need to get this provider first. As I have all fragments, I usually do this: private DataProvider provider = (DataProvider) getActivity();. But this adapter class doesn't extend either fragment or activity, so I don't know how to get the data I need from it in this class – FET Aug 22 '16 at 10:05
  • you can pass the context of your activity or fragment to the constructor of your adapter class and use it. – Bedant Aug 22 '16 at 19:26