Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Base Adapters For All Recycler View Adapters

Tags:

People also ask

How do you write the difference between customer adapter and RecyclerView?

RecyclerView AdapterViewHolders are managed by RecyclerView adapters by creating the Views and ViewHolders and binding the ViewHolder to an item in the list. Adapters notify the RecyclerView on specific changes to the data. Adapters can also handle interactions with Items such as clicks.

How many times onCreateViewHolder called?

By default it have 5. you can increase as per your need.

What is onBindViewHolder in Android?

onBindViewHolder(VH holder, int position) Called by RecyclerView to display the data at the specified position. void. onBindViewHolder(VH holder, int position, List<Object> payloads) Called by RecyclerView to display the data at the specified 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.