In my application I'm using a recycler view to show some data and for each item in recyclerview I'm adding a list of checked text view. These checked textviews are added according to the size of list provided and it works fine. Now I want to add the click listener for those items to know if they are checked or not?
When items are checked those checked items should be taken to another activity. But now the click listener is added directly in recycler view which won't access me to do out of box code. So I'm looking for how to add a click listener and access it in another activity.
My Recyclerview code:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.allio.customer.R;
import com.allio.customer.models.Types_Item;
import com.bumptech.glide.Glide;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.github.florent37.expansionpanel.ExpansionLayout;
import com.github.florent37.expansionpanel.viewgroup.ExpansionLayoutCollection;
public class Types_adapter extends FirestoreRecyclerAdapter<Types_Item, Types_adapter.RecyclerHolder> {
private final ExpansionLayoutCollection expansionsCollection = new ExpansionLayoutCollection();
Context context;
public Types_adapter(@NonNull FirestoreRecyclerOptions<Types_Item> options, Context context) {
super(options);
expansionsCollection.openOnlyOne(true);
this.context = context;
}
@Override
protected void onBindViewHolder(@NonNull RecyclerHolder holder, int position, @NonNull Types_Item model) {
expansionsCollection.add(holder.getExpansionLayout());
holder.textView.setText(model.getName());
Glide.with(holder.imageView.getContext())
.load(model.getImageURL())
.into(holder.imageView);
CheckedTextView[] textView = new CheckedTextView[model.getProblems().size()];
holder.problems.removeAllViews();
for (int i = 0; i < model.getProblems().size(); i++){
textView[i] = new CheckedTextView(context);
textView[i].setText(model.getProblems().get(i));
textView[i].setPadding(10,10,10,10);
textView[i].setTextSize(15);
int finalI = i;
textView[i].setOnClickListener(v -> {
if (textView[finalI].isChecked()){
textView[finalI].setChecked(false);
textView[finalI].setCheckMarkDrawable(0);
}else {
textView[finalI].setChecked(true);
textView[finalI].setCheckMarkDrawable(R.drawable.checked);
}
});
holder.problems.addView(textView[i]);
}
}
@NonNull
@Override
public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return RecyclerHolder.buildFor(parent);
}
public final static class RecyclerHolder extends RecyclerView.ViewHolder {
private static final int LAYOUT = R.layout.item_types;
ExpansionLayout expansionLayout;
TextView textView;
ImageView imageView;
LinearLayout problems;
public static RecyclerHolder buildFor(ViewGroup viewGroup){
return new RecyclerHolder(LayoutInflater.from(viewGroup.getContext()).inflate(LAYOUT, viewGroup, false));
}
public RecyclerHolder(View itemView) {
super(itemView);
expansionLayout = itemView.findViewById(R.id.expansionLayout);
textView = itemView.findViewById(R.id.type_name);
imageView = itemView.findViewById(R.id.type_image);
problems = itemView.findViewById(R.id.problems_list);
}
public ExpansionLayout getExpansionLayout() {
return expansionLayout;
}
}
}
make an interface class and bind this interface into constructor of adapter class.
public interface CustomDialogListener {
void onItemClicked(CheckedTextView checkedTextView);
}
this is for adapter class
private CustomDialogListener mViewClickListener;
public ProductsAdapter(Context context,boolean status) {
this.context = context;
this.status = status;
this.mViewClickListener = (CustomDialogListener) context;
}
and assign interface class method on your required action like onClick on any view.
textView[i].setOnClickListener(v -> {
if (textView[finalI].isChecked()){
textView[finalI].setChecked(false);
textView[finalI].setCheckMarkDrawable(0);
}else {
textView[finalI].setChecked(true);
textView[finalI].setCheckMarkDrawable(R.drawable.checked);
}
listener.onItemClicked(textView);
});
add implementation of interface in parent activity class and override the interface method.
public class ParentActivity extends AppCompatActivity implements
YourAdapter.CustomDialogListener {
@Override
public void onItemClicked(CheckedTextView checkedTextView) {
//Log.d("Item clicked", checkedTextView.isChecked()+"");
}
}
thats the way to track event of recyclerView. hope i made myself clear.