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: