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.
Advertisement
Answer
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(); } }); } }