I'm currently building a schedule app where users can input information, and the app will organize the information in a list with each day of the week as the header with each corresponding activity under the day. I'm using a parent child recyclerview to do this (i.e. each day of the week is a part of the parent recyclerview and each activity in the schedule is part of the child recyclerview). Right now I'm trying to add a swipe to delete information in the child recyclerview. Does anyone know how I would go about doing this using the parent child recyclerview configuration, where I have two different adapters for each recyclerview?
Code for Parent RecyclerView:
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.ViewHolder> {
ArrayList<DayHeader> weekList;
private Context mContext;
public MainRecyclerViewAdapter(Context mContext, ArrayList<DayHeader> weekList) {
this.mContext = mContext;
this.weekList = weekList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.dayofweekheader,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
DayHeader section = weekList.get(position);
String nameofday = section.getDayName();
ArrayList<medinfo> meditems = section.getmMedItems();
holder.weekdayname.setText(nameofday);
MedicationAdapter childrecyclerAdapter = new MedicationAdapter(mContext,meditems);
holder.childrecyclerview.setAdapter(childrecyclerAdapter);
}
#Override
public int getItemCount() {
return weekList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView weekdayname;
RecyclerView childrecyclerview;
public ViewHolder(#NonNull View itemView) {
super(itemView);
weekdayname = itemView.findViewById(R.id.weekdayheader);
childrecyclerview = itemView.findViewById(R.id.childrecyclerview);
}
}
}
Code for Child RecyclerView:
public class MedicationAdapter extends RecyclerView.Adapter<MedicationAdapter.MedicationViewHolder>{
private ArrayList<medinfo> mMedList;
private Context mContext;
public static class MedicationViewHolder extends RecyclerView.ViewHolder {
public TextView mMedname;
public TextView mTime;
public TextView mNumbPills;
public ImageView popup;
public MedicationViewHolder(#NonNull View itemView) {
super(itemView);
mMedname = itemView.findViewById(R.id.medname);
mTime = itemView.findViewById(R.id.time);
mNumbPills = itemView.findViewById(R.id.numberofpills);
popup = itemView.findViewById(R.id.menumore);
}
}
public MedicationAdapter(Context mContext, ArrayList<medinfo> medList) {
this.mContext = mContext;
this.mMedList = medList;
}
#NonNull
#Override
public MedicationViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item,parent,false);
MedicationViewHolder mvh = new MedicationViewHolder(v);
return mvh;
}
#Override
public void onBindViewHolder(#NonNull MedicationViewHolder holder, int position) {
medinfo currentItem = mMedList.get(position);
String medName = "Medication: " + currentItem.getmMedName();
holder.mMedname.setText(medName);
String medTime = "Time: " + currentItem.getmTime();
holder.mTime.setText(medTime);
String numbPills = "# of Pills: " + currentItem.getmNumbPills();
holder.mNumbPills.setText(numbPills);
}
#Override
public int getItemCount() {
return mMedList.size();
}
public void removeItem(int position) {
mMedList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mMedList.size());
}
}
I also have this SwipeItem class:
public class SwipeItem extends ItemTouchHelper.SimpleCallback {
MedicationAdapter mMedAdapter;
SwipeItem(MedicationAdapter mMedAdapter){
super(0,ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT);
this.mMedAdapter = mMedAdapter;
}
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getBindingAdapterPosition();
this.mMedAdapter.removeItem(position);
}
}
So how can I implement the SwipeItem in my adapter codes to delete each child item?
I have a problem where the method "deleteItem" cannot be resolved at this line adapters.deleteItem(viewHolder.getAdapterPosition());even I have created that method at public void deleteItem(int position).Do I placed the method wrongly?
Java codes:
final FirestoreRecyclerAdapter adapters;
fStore = FirebaseFirestore.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final String current = user.getUid();
Query query = fStore.collection("Users").document(user.getUid()).collection("Diary");
FirestoreRecyclerOptions<ModelClass> options = new FirestoreRecyclerOptions.Builder<ModelClass>()
.setQuery(query, ModelClass.class)
.build();
adapters = new FirestoreRecyclerAdapter<ModelClass, ModelViewHolder>(options) {
#NonNull
#Override
public ModelViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_diary, parent, false);
return new ModelViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ModelViewHolder holder, int position,
#NonNull ModelClass model) {
holder.date.setText(model.getDate());
holder.note.setText(model.getText());
holder.time.setText(model.getTime());
holder.divider.setText(model.getDivider());
}
public void deleteItem(int position) {
getSnapshots().getSnapshot(position).getReference().delete();
}
};
adapters.startListening();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
SpacingItemDecoration itemDecorator = new SpacingItemDecoration(20);
recyclerView.addItemDecoration(itemDecorator);
recyclerView.setAdapter(adapters);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
adapters.deleteItem(viewHolder.getAdapterPosition());
}
}).attachToRecyclerView(recyclerView);
}
public class ModelViewHolder extends RecyclerView.ViewHolder {
TextView date, time, note, divider;
public ModelViewHolder(#NonNull View itemView) {
super(itemView);
date = itemView.findViewById(R.id.date_text);
time = itemView.findViewById(R.id.time_textview);
note = itemView.findViewById(R.id.note_textview);
divider = itemView.findViewById(R.id.line_textview);
}
}
You can't do it since adapters is a FirestoreRecyclerAdapter which doesn't contain this method.
Just extend the FirestoreRecyclerAdapter and use it.
public class CustomAdapter extends FirestoreRecyclerAdapter<....>{
public CustomAdapter(#NonNull FirestoreRecyclerOptions<....> options) {
super(options);
}
public void deleteItem(int i){ ... }
#Override
protected void onBindViewHolder(){....}
#Override
public ModelViewHolder onCreateViewHolder(){ ...}
}
i have two adapters one for my card view and the other one for my drawer. The cards contains links to other options in the app and the drawer contains the same options. What i want is when user clicks on the card, the options in the drawer menu should get selected. Here is my code.
Drawer Adapter:
#SuppressWarnings({"rawtypes", "ConstantConditions"})
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.ViewHolder> {
private List<DrawerItem> items;
private Map<Class<? extends DrawerItem>, Integer> viewTypes;
private SparseArray<DrawerItem> holderFactories;
private OnItemSelectedListener listener;
public DrawerAdapter(List<DrawerItem> items) {
this.items = items;
this.viewTypes = new HashMap<>();
this.holderFactories = new SparseArray<>();
processViewTypes();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
ViewHolder holder = holderFactories.get(viewType).createViewHolder(parent);
holder.adapter = this;
return holder;
}
#Override
#SuppressWarnings("unchecked")
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
items.get(position).bindViewHolder(holder);
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
return viewTypes.get(items.get(position).getClass());
}
private void processViewTypes() {
int type = 0;
for (DrawerItem item : items) {
if (!viewTypes.containsKey(item.getClass())) {
viewTypes.put(item.getClass(), type);
holderFactories.put(type, item);
type++;
}
}
}
public void setSelected(int position) {
DrawerItem newChecked = items.get(position);
if (!newChecked.isSelectable()) {
return;
}
for (int i = 0; i < items.size(); i++) {
DrawerItem item = items.get(i);
if (item.isChecked()) {
item.setChecked(false);
notifyItemChanged(i);
break;
}
}
newChecked.setChecked(true);
notifyItemChanged(position);
if (listener != null) {
listener.onItemSelected(position);
}
}
public void setListener(OnItemSelectedListener listener) {
this.listener = listener;
}
static abstract class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private DrawerAdapter adapter;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
adapter.setSelected(getAdapterPosition());
}
}
public interface OnItemSelectedListener {
void onItemSelected(int position);
}
}
Card Adapter:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
;
private RecyclerView parentRecycler;
private List<Card> data;
public CardAdapter(List<Card> data) {
this.data = data;
}
#Override
public void onAttachedToRecyclerView(#NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
parentRecycler = recyclerView;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_card, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Card card = data.get(position);
holder.getAdapterPosition();
Glide.with(holder.itemView.getContext())
.asGif()
.load(card.getCardIcon())
.into(holder.gifImageView);
holder.textView.setText(card.getCardName());
}
#Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private GifImageView gifImageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
gifImageView = itemView.findViewById(R.id.card_gif);
textView = itemView.findViewById(R.id.card_name);
itemView.findViewById(R.id.container).setOnClickListener(this);
}
public void showText() {
int parentHeight = ((View) gifImageView.getParent()).getHeight();
float scale = (parentHeight - textView.getHeight()) / (float) gifImageView.getHeight();
gifImageView.setPivotX(gifImageView.getWidth() * 0.5f);
gifImageView.setPivotY(0);
gifImageView.animate().scaleX(scale)
.withEndAction(new Runnable() {
#Override
public void run() {
textView.setVisibility(View.VISIBLE);
// gifImageView.setColorFilter(Color.BLACK);
}
})
.scaleY(scale).setDuration(200)
.start();
}
public void hideText() {
textView.setVisibility(View.INVISIBLE);
gifImageView.animate().scaleX(1f).scaleY(1f)
.setDuration(200)
.start();
}
#Override
public void onClick(View v) { parentRecycler.smoothScrollToPosition(getAdapterPosition());
}
}
}
}
P.S: I am new to programming any kind of help is appreciated.[As you can see in the image below that I have cards in the main view and the same options are in the drawer menu for example question papers. Now when the user clicks on question paper in the main view the question paper in drawer menu should get selected.[][1]][1]
thank you!
[1]: https://i.stack.imgur.com/FooVb.jpg
i have a list and i showed that in recycler view
some of items have blue background and other items have gray background
i want to edit selected item background (
The selected item means the item that has been clicked )
this is my adapter class
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {
private Context context;
private List<User> users = new ArrayList<>();
public UserAdapter(List<User> users, Context context) {
this.users = users;
this.context = context;
}
#NonNull
#Override
public UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new UserViewHolder(LayoutInflater.from(context).inflate(R.layout.item_user, parent, false));
}
#Override
public void onBindViewHolder(#NonNull UserViewHolder holder, int position) {
holder.binUser(users.get(position), position);
}
#Override
public int getItemCount() {
return users.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
private RelativeLayout rlItemUser;
public UserViewHolder(#NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_itemUser_name);
rlItemUser = itemView.findViewById(R.id.itemUser_rootView);
}
public void binUser(User user, int position){
tvName.setText(user.getName());
if (user.getMode().equals("passenger")){
rlItemUser.setBackgroundColor(context.getResources().getColor(R.color.colorGray));
tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
} else if (user.getMode().equals("driver")){
rlItemUser.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
tvName.setTextColor(context.getResources().getColor(R.color.colorGray));
}
}
}
in the bindUser method:
rlItemUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rtlItemUser.setBackgroundColor(context.getResources().getColor(your color);
adapter.notifyItemChanged(position);
}
});
public abstract class BaseAdapters extends RecyclerView.Adapter<BaseAdapters.MyViewHolder> implements View.OnClickListener {
protected Context parentContext;
public int layout_id;
protected List<?> dataList = new ArrayList<>();
public class MyViewHolder extends RecyclerView.ViewHolder {
MyViewHolder(View view) {
super(view);
declareViews(view,this);
}
}
#Override
public void onClick(View view) {
onClickViews(view);
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int i) {
bindView(holder, i);
}
public void notifyList(List<?> filterdNames) {
this.dataList = filterdNames;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
if (dataList.size() == 0)
return 5;
else
return dataList.size();
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(layout_id, parent, false);
return new MyViewHolder(itemView);
}
public abstract MyViewHolder bindView(MyViewHolder holder, int position);
public abstract void onClickViews(View view);
public abstract void declareViews(View view,MyViewHolder holder);
}
How can i perform on click of every item selection using holder in child class extending with it.
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.MyViewHolder> {
public int layout_id;
protected List<?> dataList = new ArrayList<>();
Context BASE_CONTEXT;
public View itemview;
public BaseAdapter(Context context) {
this.BASE_CONTEXT = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(layout_id, viewGroup, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder viewHolder, int position) {
onBindViewHold(position, dataList.get(position));
}
public abstract View getView(View view);
#Override
public int getItemCount() {
return dataList.size() == 0 ? 0 : dataList.size();
}
public abstract void onBindViewHold(int position, Object itemView);
class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(#NonNull View itemView) {
super(itemView);
itemview = itemView;
getView(itemView);
}
}
public <T extends View> T bind(int id) {
return itemview.findViewById(id);
}
}
public class Adapter extends BaseAdapter {
TextView tv;
Adapter(Context context, ArrayList<String> arrayList){
super(context);
dataList=arrayList;
layout_id=R.layout.content_main2;
}
#Override
public View getView(View view) {
tv = bind(R.id.tv);
return view;
}
#Override
public void onBindViewHold( final int position, Object itemView) {
String text=(String) itemView;
Log.e("tv",tv.toString());
tv.setText(text);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(BASE_CONTEXT, ""+position, Toast.LENGTH_SHORT).show();
}
});
}
}
You can refer this sample adapter class and edit it as per your requirement:
public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder> {
List<abs.Ex> exList;
Context context;
absadapter(List exList) {
this.exList= exList;
}
public static class exViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
TextView exname;
ImageView exlogo;
exViewHolder(View itemView){
super(itemView);
cardView= itemView.findViewById(R.id.cardView);
exname= itemView.findViewById(R.id.ex_name);
exlogo=itemView.findViewById(R.id.exlogo);
}
}
#Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
return evh;
}
#Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position){
holder.exname.setText(exList.get(position).name);
holder.exlogo.setImageResource(exList.get(position).logoId);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (position == 0 ){
v.getContext().startActivity(new Intent(v.getContext(), abdetails.class));
}
if (position == 1) {
v.getContext().startActivity(new Intent(v.getContext(), declinecrunch.class));
}
if (position == 2) {
v.getContext().startActivity(new Intent(v.getContext(), dumsidebend.class));
}
}
});
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return exList.size();
}
Refer this for Main Class:
public class abs extends AppCompatActivity {
RecyclerView recyclerView;
Context context;
private List<Ex> exlist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abs);
recyclerView= findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager=new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
initializeData();
initializeAdapter();
}
private void initializeData(){
exlist=new ArrayList<>();
exlist.add(new abs.Ex("Crunches",R.drawable.crunchesgif));
exlist.add(new abs.Ex("Decline Crunch",R.drawable.declinecrunch));
exlist.add(new abs.Ex("Dumbell Side Bends",R.drawable.dumbbellsidebend));
exlist.add(new abs.Ex("Hanging Leg Raises",R.drawable.hanginglegraises));
exlist.add(new abs.Ex("Incline Leg Raises",R.drawable.inclinelegraises));
exlist.add(new abs.Ex("Kneeling Cable Crunch",R.drawable.cablecrunch));
exlist.add(new abs.Ex("Legs Raises",R.drawable.legraises));
exlist.add(new abs.Ex("Flat Bench Lying Leg Raises",R.drawable.flatbenchlyinglegraise));
exlist.add(new abs.Ex("Seated Jack Knife",R.drawable.seatedjackknife));
exlist.add(new abs.Ex("Twisting Hip Raise",R.drawable.twistinghipraise));
exlist.add(new abs.Ex("Weighted Crunch",R.drawable.weightedcrunch));
exlist.add(new abs.Ex("Plank",R.drawable.plank));
exlist.add(new abs.Ex("Side Plank",R.drawable.sideplank));
exlist.add(new abs.Ex("Superman",R.drawable.superman));
exlist.add(new abs.Ex("Twist Crunch",R.drawable.twistcrunch));
}
public void initializeAdapter(){
absadapter rvadapter=new absadapter(exlist);
recyclerView.setAdapter(rvadapter);
}
class Ex{
String name;
int logoId;
Ex(String name,int logoId){
this.name=name;
this.logoId=logoId;
}
}
This is extend version of rupali answer, i found recycle issue on recycle-view viewholder while scrolling on items. so if needed you can use below one,
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseRecyclerViewAdapter.BaseViewHolder> {
public int layout_id;
protected List<?> dataList = new ArrayList<>();
protected BaseActivity Context;
public BaseRecyclerViewAdapter(BaseActivity context) {
this.Context = context;
}
#NonNull
#Override
public BaseViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(layout_id, viewGroup, false);
return new BaseViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder viewHolder, int position) {
onViewHolderBind(viewHolder,position, dataList.get(position));
}
#Override
public int getItemCount() {
return dataList.size();
}
public abstract void onViewHolderBind(BaseViewHolder viewHolder, int position, Object data);
public class BaseViewHolder extends RecyclerView.ViewHolder {
public BaseViewHolder(#NonNull View itemView) {
super(itemView);
}
}
}
Uses
public class LicenseListAdapter extends BaseRecyclerViewAdapter {
public LicenseListAdapter(BaseActivity appLicensePlanActivity, List<LicenseRatePlanResponse> licenseRatePlanResponseData) {
super(appLicensePlanActivity);
dataList = licenseRatePlanResponseData;
layout_id = R.layout.item_list_license_plan;
}
#Override
public void onViewHolderBind(BaseViewHolder viewHolder, int position, Object data) {
Button btnBuyButtonOfLicencePlaneILLP;
TextView lblAmountOfLicencePlaneILLP, lblDayOfLicencePlaneILLP;
lblAmountOfLicencePlaneILLP = viewHolder.itemView.findViewById(R.id.lblAmountOfLicencePlaneILLP);
lblDayOfLicencePlaneILLP = viewHolder.itemView.findViewById(R.id.lblDayOfLicencePlaneILLP);
btnBuyButtonOfLicencePlaneILLP = viewHolder.itemView.findViewById(R.id.btnBuyButtonOfLicencePlaneILLP);
final LicenseRatePlanResponse tempLicensePlan = (LicenseRatePlanResponse) data;
lblAmountOfLicencePlaneILLP.setText(KSUtility.GetFloatWithOutPrecesion(tempLicensePlan.getAmount()).toString() +" only");
lblDayOfLicencePlaneILLP.setText(tempLicensePlan.getValidityDays().toString() +" days");
btnBuyButtonOfLicencePlaneILLP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PaymentHelper.OpenPaymentDialog(Context, TransactionType.LicenseRenew, tempLicensePlan.getAmount(),tempLicensePlan.getLicenseRatePlanId());
}
});
}
}
overriding the method which handles click events in sub-classes of "BaseAdapters" should do it( based on your comments on other posts i assume bindView() handles click events; override whichever method you know handles click events):
class new_adapter extends BaseAdapters{
//...
#Override
public MyViewHolder bindView(MyViewHolder holder, int position){
//you could alter how click events are handled here.
}
//...
}
There can be many ways, one of them is, you can use Java Generics.
Set click in your onCreateViewHolder, set click and pass your list item of clicked row, or anything you need. You can customize method onClickViews if you need position or anyother field.
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int position) {
..
itemView.setOnClickListener(v -> onClickViews(itemView, dataList.get(position)));
..
}
and
public abstract void onClickViews(View view, T object);
Use like
public class SampleAdapter extends BaseAdapters<Item> {
#Override
public int getRowLayout() {
return R.layout.row;
}
#Override
public void onClickViews(View view, Item object) {
// row clicked
}
}
I edited your class a bit.
public abstract class BaseAdapters<T> extends RecyclerView.Adapter<BaseAdapters.MyViewHolder> {
protected List<T> dataList = new ArrayList<>();
public class MyViewHolder extends RecyclerView.ViewHolder {
MyViewHolder(View view) {
super(view);
declareViews(view, this);
}
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int i) {
bindView(holder, i);
}
public void notifyList(List<T> filteredNames) {
dataList = filteredNames;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return dataList.size();
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int position) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(getRowLayout(), parent, false);
itemView.setOnClickListener(v -> onClickViews(itemView, dataList.get(position)));
return new MyViewHolder(itemView);
}
public abstract #LayoutRes
int getRowLayout();
public abstract MyViewHolder bindView(MyViewHolder holder, int position);
public abstract void onClickViews(View view, T object);
public abstract void declareViews(View view, MyViewHolder holder);
}
The Base Recycler Adapter will be like this
public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int BOTTOM_VIEW_TYPE = 3;
private List<Object> listModels;
private Context mContext;
private OnItemClickListener onClickListener = null;
public BaseRecyclerViewAdapter(Context context) {
this.mContext = context;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case BOTTOM_VIEW_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_bottomview_type, parent, false);
return new ChildViewHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
final Object model = listModels.get(position);
if (model != null && model.getId() > 0) {
((BaseViewHolder) holder).bind(model, position);
} else if (model != null) {
((TopViewViewHolder) holder).bind(model, position);
}
}
#Override
public int getItemViewType(int position) {
if (listModels.get(position).getId() > 0) {
return BASE_VIEW_TYPE;
} else {
return TOP_VIEW_TYPE;
}
}
#Override
public int getItemCount() {
return null == listModels ? 0 : listModels.size();
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onClickListener = onItemClickListener;
}
private class ChildViewHolder extends RecyclerView.ViewHolder {
TextView tv_bottomName;
BottomViewHolder(View v) {
super(v);
tv_bottomName = v.findViewById(R.id.tv_bottomName);
}
void bind(Object object, final int position) {
tv_bottomName.setOnClickListener(view -> onClickListener.onItemClick(object, tv_bottomName, position));
}
}
}
And use a Custom interface for OnItemClickListner
public interface OnItemClickListener {
void onItemClick(Object object, View view, int position);}
And Implement the listner in activity or fragment