Java – How to use buttons and recycleView together

How to use buttons and recycleView together… here is a solution to the problem.

How to use buttons and recycleView together

https://drive.google.com/open?id=1R964AvU-a7TW8Lfp8VqJnmcbvhc-7gcN

I have a recycleView (see figure). You see there are also 2 buttons. This is the layout file .xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/white">

<de.hdodenhof.circleimageview.CircleImageView
      android:layout_marginLeft="10dp"
      android:id="@+id/main_picture"
      android:layout_width="45dp"
      android:layout_height="50dp"
      android:src="@drawable/pfl_img"
      android:layout_centerVertical="true"
      android:layout_alignParentStart="true" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/main_picture"
    android:layout_marginRight="5dp"
    android:layout_marginLeft="10dp"
    android:id="@+id/relativeLayout2">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edem Palonik"
        android:textSize="17sp"
        android:id="@+id/textName"
        android:textColor="@color/black"
        android:layout_above="@+id/textDescription"
        android:layout_alignParentStart="true" />

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Profession and ..."
        android:textColor="@color/black"
        android:textSize="17sp"
        android:id="@+id/textDescription"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

<ImageView
        android:layout_width="20dp"
        android:layout_height="18dp"
        android:layout_marginTop="2dp"
        android:layout_below="@+id/textDescription"
        android:id="@+id/historyIcon"
        android:layout_alignParentStart="true" />

<TextView
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/date"
        android:textSize="14sp"
        android:text="17/12/2017/13:46"
        android:layout_marginTop="2dp"
        android:layout_below="@+id/textDescription"
        android:layout_toEndOf="@+id/historyIcon" />
</RelativeLayout>

<Button
    android:id="@+id/call_button"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/call_img"
    android:layout_centerVertical="true"
    android:layout_toStartOf="@+id/sms_button" />

<Button
    android:id="@+id/sms_button"
    android:layout_width="37dp"
    android:layout_height="32dp"
    android:background="@drawable/sms_img"
    android:layout_alignTop="@+id/call_button"
    android:layout_alignParentEnd="true" />

<View
    android:layout_width="match_parent"
    android:layout_height="0.8dp"
    android:layout_alignParentBottom="true"
    android:background="@color/gray" />

I know, I

can use recyclerViewItemCLickListener, but I want to click the last two buttons separately, so what do I need to do?

So instead of clicking on all rows, I want to click the last button individually, I just want the last button to be clickable.

This is Java code.

public class ConnectionsFragment extends Fragment {
private View mainView;
private RecyclerView recyclerView;
private List<Connections_item> list;
private Button call, sms;

public ConnectionsFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.connections_fragment, container, false);
    recyclerView = (RecyclerView) mainView.findViewById(R.id.recycler_connection);
    ConnectionsAdapter adapter = new ConnectionsAdapter(getActivity(), list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);

init(mainView);
    return mainView;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

list = new ArrayList<>();
    list.add(new Connections_item(R.drawable.pfl_img, "Anun Azganun1", "Inch vor text", R.drawable.missed, "23/11/1998 00:00"));
    list.add(new Connections_item(R.drawable.pfl_img, "Anun Azganun2", "Inch vor text", R.drawable.callagain, "24/11/1998 01:00"));
    list.add(new Connections_item(R.drawable.pfl_img, "Anun Azganun3", "Inch vor text", R.drawable.missed, "25/11/1998 02:00"));

public void init(View v) {
    call = (Button) v.findViewById(R.id.call_button);
    sms = (Button) v.findViewById(R.id.sms_button);

        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Whom you wanna call?", Toast.LENGTH_SHORT).show();
//            }
//        });

        sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Whom you wanna send sms?", Toast.LENGTH_SHORT).show();
//            }
//        });
    }
}

This is the adapter code.

public class ConnectionsAdapter extends RecyclerView.Adapter<ConnectionsAdapter.MyViewHolder> {

Context context;
List<Connections_item> list = new ArrayList<>();

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

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v;
    v = LayoutInflater.from(context).inflate(R.layout.connections_view_item, parent, false);
    MyViewHolder holder = new MyViewHolder(v);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.mainImage.setImageResource(list.get(position).getMainImage());
    holder.fullName.setText(list.get(position).getFullName());
    holder.description.setText(list.get(position).getDescription());
    holder.historyImage.setImageResource(list.get(position).getHistoryIcon());
    holder.date.setText(list.get(position).getDate());

}

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

public class MyViewHolder extends RecyclerView.ViewHolder {

ImageView mainImage, historyImage;
    TextView fullName, description, date;
    Button call, sms;

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

mainImage = (ImageView) v.findViewById(R.id.main_picture);
        historyImage = (ImageView) v.findViewById(R.id.historyIcon);
        fullName = (TextView) v.findViewById(R.id.textName);
        description = (TextView) v.findViewById(R.id.textDescription);
        date = (TextView) v.findViewById(R.id.date);
        call = (Button) v.findViewById(R.id.call_button);
        sms = (Button) v.findViewById(R.id.sms_button);

}
  }
}

Solution

Using an interface you can do this

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

Context mContext;
    ArrayList<Data> mData;
    OnButtonClickListeners onButtonClickListeners;

public TestAdapter(Context mContext, ArrayList<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

public void setOnButtonClickListeners(OnButtonClickListeners listener){
        this.onButtonClickListeners = listener;
    }

@Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder vh;
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        vh = new MyViewHolder(v);
        return vh;
    }

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Bind holder here
    }

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

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

@Bind(R.id.call_button)
        Button btnCall;

@Bind(R.id.sms_button)
        Button btnSms;

public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

btnCall.setOnClickListener(this);
            btnSms.setOnClickListener(this);

}

@Override
        public void onClick(View v) {

if(this.onButtonClickListeners!=null){

switch (v.getId()) {
                    case R.id.call_button:
                        onButtonClickListeners.onCallClick(getAdapterPosition());
                        break;
                    case R.id.sms_button:
                        onButtonClickListeners.onSmsClick(getAdapterPosition());
                        break;
                }
            }

}

}

public interface OnButtonClickListeners{

void onCallClick(int position);
        void onSmsClick(int position);
    }

}

You can then call this interface from the Activity and Fragment like this

private void setUpRecyclerView(){
        mAdapter = new ProfileAdapter(mContext,new ArrayList<String>());
        lm = new LinearLayoutManager(getActivity());
        rvFeeds.setLayoutManager(lm);
        rvFeeds.setAdapter(mAdapter);

mAdapter.setOnButtonClickListeners(new OnButtonClickListeners() {
            @Override
            public void onCallClick(int position) {
                    To do your code here
            }

@Override
            public void onSmsClick(int position) {
                    To do your code here
            }
        })
    }

Related Problems and Solutions