android– 在RecyclerView OnCreateViewHolder中以编程方式创建或扩充视图

android– 在RecyclerView OnCreateViewHolder中以编程方式创建或扩充视图,第1张

概述基本上我想创建一个布局或使用RecyclerView以编程方式扩展布局,但是无法这样做.我知道如何通过膨胀来处理xml,但出于好奇,我想以编程方式进行操作.我的适配器代码如下:publicclassACShareextendsRecyclerView.Adapter<ACShare.VHShare>{privateList<Integer>listDraw

基本上我想创建一个布局或使用RecyclerVIEw以编程方式扩展布局,但是无法这样做.我知道如何通过膨胀来处理xml,但出于好奇,我想以编程方式进行操作.

我的适配器代码如下:

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);        }    }}

我不知道如何在onCreateVIEwHolder上做.我搜索了StackOverflow并找到了一些链接,但没有发现它们有用,其中一些如下:

> Create views programmatically in a RecyclerView.ViewHolder and passing arguments to it
> dynamically add layout into getview method of adaptor in android

解决方法:

最后得到建议后,我得到了我的问题的解决方案,

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();}@OverrIDepublic 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);}@OverrIDepublic voID onBindVIEwHolder(VHShare holder, int position) {    holder.imageVIEw.setimageResource(ListDrawalbe.get(position));}@OverrIDepublic 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 @R_404_5095@= (int) context.getResources().getDimension(R.dimen.elevation_header);        layoutParams.setmargins(@R_404_5095@,@R_404_5095@,@R_404_5095@,@R_404_5095@);        imageVIEw.setLayoutParams(layoutParams);        imageVIEw.set@R_404_5095@(@R_404_5095@,@R_404_5095@,@R_404_5095@,@R_404_5095@);        linearLayout linearLayout=(linearLayout)itemVIEw;        linearLayout.addVIEw(imageVIEw);        itemVIEw.setonClickListener(this);    }    @OverrIDe    public voID onClick(VIEw v) {        Listener.processedResult(getposition(), CallBackConstants.MESSAGE);    }}

}

总结

以上是内存溢出为你收集整理的android – 在RecyclerView OnCreateViewHolder中以编程方式创建或扩充视图全部内容,希望文章能够帮你解决android – 在RecyclerView OnCreateViewHolder中以编程方式创建或扩充视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:http://outofmemory.cn/web/1115762.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存