Search code examples
javaandroidandroid-recyclerviewonitemclicklistener

How to use button and recycleView together


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

I have a recycleView (see picture). You see there are 2 buttons too. Here's 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 wanna click on last 2 buttons separately, so what do I need to do?

So I wanna click on last buttons separately, without clicking all line, I want only last buttons to be clickable.

Here's the 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();
//            }
//        });
    }
}

Here's 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

  • As adapter holds the recyclerView item and buttons are in the item so all code related to button will be in the adapter only.

    In adapter modify the code :

    @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());
    
            holder.call.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                       Toast.makeText(getActivity(), "Whom you wanna call?", Toast.LENGTH_SHORT).show();
                    }
               });
    
            holder.sms.setOnClickListener(new View.OnClickListener() {
                   @Override
                    public void onClick(View view) {
                        Toast.makeText(getActivity(), "Whom you wanna send sms?",                                }
                });
    
        }
    

    In java file just remove call and sms button initialization and listners.