Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create or inflate views programmatically in a RecyclerView OnCreateViewHolder

Basically I wanted to create a layout or inflate a layout programatically with a RecyclerView, but was unable to do so. As I know how to do with xml by inflating it, but out of curiosity I wanted to do it programatically.

My adapter code is as follows:

public class ACShare extends RecyclerView.Adapter<ACShare.VHShare>{

    private List<Integer> listDrawalbe;

    public ACShare(Context context) {
        TypedArray drawables = context.getResources().obtainTypedArray(R.array.s_array_contact_us_share);
        listDrawalbe=new ArrayList<>();
        for (int i = 0; i <  drawables.length(); ++i)
            listDrawalbe.add( drawables.getResourceId(i, -1));
    }

    @Override
    public VHShare onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VHShare(LayoutInflater.from(parent.getContext()).inflate(-1,parent,false));
    }

    @Override
    public void onBindViewHolder(VHShare holder, int position) {
        holder.imageView.setImageResource(listDrawalbe.get(position));
    }


    public class VHShare extends RecyclerView.ViewHolder
    {
        public ImageView imageView;
        public LinearLayout ll;

        public VHShare(View itemView) {
            super(itemView);

            Context context= GeneralFunction.getActivity(itemView);


            ll = new LinearLayout(context);
            ll.setOrientation(android.widget.LinearLayout.VERTICAL);
            ll.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
            ll.setBackgroundColor(0x88ff0000);

            imageView = new ImageView(context);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(-1,-2));
            imageView.setBackgroundColor(0x5500ff00);
            ll.addView(imageView);
        }
    }
}

I didn't know how to do in onCreateViewHolder. I searched StackOverflow and found some links but none of them were found useful, some of them are as follows:

  • Create views programmatically in a RecyclerView.ViewHolder and passing arguments to it
  • dynamically add layout into getview method of adaptor in android
like image 275
Reprator Avatar asked Nov 22 '16 13:11

Reprator


People also ask

What is viewType in onCreateViewHolder?

onCreateViewHolder(parent: ViewGroup, viewType: Int) In onCreateViewHolder, we will inflate the view that we will be using to hold each list item. This method will be called when RecyclerView needs a new ViewHolder of the given type to represent. The params are the Viewgroup parent and int viewType.


1 Answers

Finally, after getting a suggestion, I get the solution to my question,

    public class ACShare extends RecyclerView.Adapter<ACShare.VHShare>{

    private List<Integer> listDrawalbe;
    private ProcessedResult listener;

    public ACShare(Fragment context) {
        listener=(ProcessedResult)context;
        TypedArray drawables = context.getActivity().getResources().obtainTypedArray(R.array.s_array_contact_us_share);
        listDrawalbe=new ArrayList<>();
        for (int i = 0; i <  drawables.length(); ++i)
            listDrawalbe.add( drawables.getResourceId(i, -1));
        drawables.recycle();
    }

    @Override
    public VHShare onCreateViewHolder(ViewGroup parent, int viewType) {

        Context context= GeneralFunction.getActivity(parent);
        LinearLayout ll = new LinearLayout(context);
        RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(RecyclerView.LayoutParams.WRAP_CONTENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);
        ll.setLayoutParams(layoutParams);
        ll.setBackgroundColor(GeneralFunction.getColor(context,R.color.color_tranparent));

        return new VHShare(ll);
    }

    @Override
    public void onBindViewHolder(VHShare holder, int position) {
        holder.imageView.setImageResource(listDrawalbe.get(position));
    }

    @Override
    public int getItemCount() {
        return listDrawalbe.size();
    }

    class VHShare extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        public ImageView imageView;

        VHShare(View itemView) {
            super(itemView);

            Context context= GeneralFunction.getActivity(itemView);

            imageView = new ImageView(context);
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            int padding= (int) context.getResources().getDimension(R.dimen.elevation_header);
            layoutParams.setMargins(padding,padding,padding,padding);
            imageView.setLayoutParams(layoutParams);

            imageView.setPadding(padding,padding,padding,padding);

            LinearLayout linearLayout=(LinearLayout)itemView;
            linearLayout.addView(imageView);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            listener.processedResult(getPosition(), CallBackConstants.MESSAGE);
        }
    }
}
like image 109
Reprator Avatar answered Sep 23 '22 20:09

Reprator