I have a recycler view list in my app. There it has select and unselect option. After selecting the items I need to create a particular item with that selected list.
So I have added the create button in my fragment and showed the selection and unselection in my adapter onBindViewHolder.
Here I need to send the selected item's value to fragment and need to set the post call in the fragment.
My Fragment:
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
mcreatevendorlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
execute(new InitiationRequest("FINTesting", mcreate.getName()), mCreateInitiationListener);
mAdapter.setOnItemClickListner(new MembersAdapter.onItemClickListner() {
@Override
public void onClick(User str) {
}
});
}
});
My Adapter:
MembersAdapter.onItemClickListner onItemClickListner;
public void setOnItemClickListner(MembersAdapter.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner{
void onClick(User str);//pass your object types.
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
User message = (User) mModelList.get(position);
holder.select_Add.setOnClickListener(new View.OnClickListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View view) {
message.setSelected(!message.isSelected());
holder.select_Add.setText(message.isSelected() ? "ADDED" : "Add");
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(message);
}
});
}
My mainxml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/create_vendorlist"
android:text="Create Vendor"
android:textColor="@color/black"
android:background="@drawable/background"
android:layout_gravity="center"
android:padding="15dp"
android:layout_margin="15dp"
android:textSize="15sp"
android:fontFamily="@font/cachet_std_medium"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"/>
</LinearLayout>
Listview.xml:
<TextView
android:id="@+id/tv_username"
style="@style/Username"
android:layout_alignParentLeft="@+id/myCardView"
android:paddingTop="@dimen/spacing_d2"
tools:text="UserName"/>
<CheckBox
android:id="@+id/tv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:buttonTint="@color/white"
android:gravity="center"
/>