程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将 ArrayLIst 添加到 Recyclerview大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决将 ArrayLIst 添加到 Recyclerview?

开发过程中遇到将 ArrayLIst 添加到 Recyclerview的问题如何解决?下面主要结合日常开发的经验,给出你关于将 ArrayLIst 添加到 Recyclerview的解决方法建议,希望对你解决将 ArrayLIst 添加到 Recyclerview有所启发或帮助;

我在其中有一个 AlertDialog,曾经有一个 ListVIEw,其中显示了 ArrayList 的内容。为了美观,我决定用 RecyclervIEw,Card 替换它,然后问题就开始了。

项目是在工作期间添加到 ArrayList <String> mainListWord 的,它们应该包含在 RecyclervIEw 卡片中。问题是要么整个列表一次落入一张卡片,要么每张下一张卡片都包含前面的元素+1。例如:第一张卡片:1个元素,第二张卡片:2个元素,等等。

如何实现,当一个新元素加入数组时,它会被放置在自己的卡片中?

activity_main.xml

<?xml version="1.0" enCoding="utf-8"?>
<relativeLayout
    xmlns:androID="http://scheR_170_11845@as.androID.com/apk/res/androID"
    xmlns:tools="http://scheR_170_11845@as.androID.com/tools"
    androID:layout_wIDth="match_parent"
    androID:layout_height="match_parent"
    tools:context=".MainActivity">


   <EditText
       androID:ID="@+ID/innerText"
       androID:layout_wIDth="wrap_content"
       androID:layout_height="50dp"
       androID:layout_centerInParent="true"/>

    <button
        androID:ID="@+ID/press"
        androID:layout_wIDth="wrap_content"
        androID:layout_height="wrap_content"
        androID:layout_margintop="10dp"
        androID:text="Press"
        androID:layouT_Below="@ID/innerText"
        androID:layout_centerHorizontal="true"
        androID:onClick="openDialog"/>

</relativeLayout>

recycler_item.xml

<?xml version="1.0" enCoding="utf-8"?>
    <androIDx.cardvIEw.Widget.CardVIEw
    xmlns:androID="http://scheR_170_11845@as.androID.com/apk/res/androID"
    xmlns:tools="http://scheR_170_11845@as.androID.com/tools"
    androID:layout_wIDth="match_parent"
    androID:layout_height="wrap_content"
    xmlns:app="http://scheR_170_11845@as.androID.com/apk/res-auto"
    app:cardCornerRadius="4dp"
    androID:layout_margin="4dp"
    app:cardBACkgroundcolor="@color/colorCardBACk">

    <relativeLayout
        androID:layout_wIDth="match_parent"
        androID:layout_height="match_parent"
        androID:layout_margin="5dp"
        androID:BACkground="@color/colorCard">

        <ImageVIEw
         androID:ID="@+ID/imageVIEw_1"
         androID:layout_wIDth="50dp"
         androID:layout_height="50dp"
         androID:padding="4dp" />
            <TextVIEw
            androID:ID="@+ID/textvIEw_1"
            androID:layout_wIDth="wrap_content"
            androID:layout_height="wrap_content"
            androID:text="Text_1"
            androID:textStyle="bold"
            androID:textcolor="@androID:color/black"
            androID:layout_centerInParent="true"
            androID:textSize="16sp"
            androID:layout_marginStart="5dp"
            androID:layout_toEndOf="@ID/imageVIEw_1"/>

</relativeLayout>
</androIDx.cardvIEw.Widget.CardVIEw>

stats_fragment.xml

<?xml version="1.0" enCoding="utf-8"?>
<relativeLayout
    xmlns:androID="http://scheR_170_11845@as.androID.com/apk/res/androID"
    xmlns:tools="http://scheR_170_11845@as.androID.com/tools"
    androID:layout_wIDth="match_parent"
    androID:layout_height="match_parent"
    tools:context=".MainActivity">


    <androIDx.recyclervIEw.Widget.RecyclerVIEw
        androID:ID="@+ID/recycler_vIEw"
        androID:layout_wIDth="match_parent"
        androID:layout_height="match_parent"
        androID:padding="50dp"
        androID:layout_margin="10dp"
        androID:BACkground="@color/colOraccent"
        androID:scrollbars="vertical"
        androID:layout_above="@ID/butClose"/>

    <button
        androID:ID="@+ID/butClose"
        androID:layout_wIDth="wrap_content"
        androID:layout_height="wrap_content"
        androID:layout_marginBottom="5dp"
        androID:layout_centerHorizontal="true"
        androID:layout_alignParentBottom="true"/>
</relativeLayout>

@H_526_19@mainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText innerText;
    private button press,butClose;

    private ArrayList<RecyclerVIEwItem> recyclerVIEwItem;
    private ArrayList<String> mainListWord;
    private AlertDialog OptionDialog;

        private RecyclerVIEw recyclerVIEw;
        private RecyclerVIEw.Adapter adapter;
        private RecyclerVIEw.LayoutManager layoutManager;

    @requiresAPI(API = Build.VERSION_CODEs.O)
    @OverrIDe
    protected voID onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        setContentVIEw(R.layout.activity_main);
        recyclerVIEwItem =  new ArrayList<>();
        mainListWord =  new ArrayList<>();

        innerText = findVIEwByID(R.ID.innerText);
        press = findVIEwByID(R.ID.butClosE);


    }

    @requiresAPI(API = Build.VERSION_CODEs.O)
    public voID makeRecyclerList(ArrayList<String> incomE){
       String[] ListWord_lenght = income.toArray(new String[0]);
       String keyWord = (String.join("",ListWord_lenght));
        recyclerVIEwItem.add(new RecyclerVIEwItem(R.drawable.star,keyWord));

    }

    @requiresAPI(API = Build.VERSION_CODEs.O)
    public voID openDialog(VIEw v){
        String word = innerText.getText().toString();
        mainListWord.add(word);
            makeRecyclerList(mainListWord);
        Dialogus();
        innerText.setText("");
    }

    public voID Dialogus(){

        LayoutInflater li = (LayoutInflater) this.getSystemservice(Context.LAYOUT_INFLATER_serviCE);
        VIEw v = li.inflate(R.layout.stats_fragment,null,falsE);
            OptionDialog = new AlertDialog.builder(this).create();
            OptionDialog.settitle("TesTinfo");
                recyclerVIEw = v.findVIEwByID(R.ID.recycler_vIEw);
                recyclerVIEw.setHasFixedSize(true);
                adapter = new RecyclerVIEwAdapter(recyclerVIEwItem);
                layoutManager = new linearlayoutmanager(this);
                recyclerVIEw.setAdapter(adapter);
                recyclerVIEw.setLayoutManager(layoutManager);
                    butClose =  v.findVIEwByID(R.ID.butClosE);
                        OptionDialog.setVIEw(v);
                        OptionDialog.setCancelable(true);
                            butClose.setBACkgroundcolor(color.CYAN);
                            butClose.setonClickListener(new VIEw.onClickListener() {
                                public voID onClick(VIEw v) {
                                    OptionDialog.dismiss();
                                }
                            });
                            OptionDialog.show();
            }
}

RecyclerVIEwAdapter.java

public class RecyclerVIEwAdapter extends RecyclerVIEw.Adapter<RecyclerVIEwAdapter.RecyclerVIEwVIEwHolder> {

    private ArrayList<RecyclerVIEwItem> arrayList;

    public static class RecyclerVIEwVIEwHolder extends RecyclerVIEw.VIEwHolder {




    public ImageVIEw imageVIEw_1;
    public TextVIEw textvIEw_1;


        public RecyclerVIEwVIEwHolder(@NonNull VIEw itemVIEw) {
            super(itemVIEw);
            imageVIEw_1 = itemVIEw.findVIEwByID(R.ID.imageVIEw_1);
            textvIEw_1 = itemVIEw.findVIEwByID(R.ID.textvIEw_1);
        }
    }


    public RecyclerVIEwAdapter(ArrayList<RecyclerVIEwItem> arrayList){
        this.arrayList = arrayList;
    }

    @NonNull
    @OverrIDe
    public RecyclerVIEwVIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup vIEwGroup,int vIEwTypE) {

        View view = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.recycler_item,vIEwGroup,falsE);
            RecyclerVIEwVIEwHolder recyclerVIEwVIEwHolder= new RecyclerVIEwVIEwHolder(vIEw);
            return  recyclerVIEwVIEwHolder;

    }

    @OverrIDe
    public voID onBindVIEwHolder(@NonNull RecyclerVIEwVIEwHolder recyclerVIEwVIEwHolder,int position) {
        RecyclerVIEwItem recyclerVIEwItem = arrayList.get(position);
            recyclerVIEwVIEwHolder.imageVIEw_1.setimageresource(recyclerVIEwItem.getimageresource());
            recyclerVIEwVIEwHolder.textvIEw_1.setText(recyclerVIEwItem.getText_1());
    }

    @OverrIDe
    public int getItemCount() {
        return arrayList.size();
    }
    
}

RecyclerVIEwItem.java

package freijer.app.one;

public class RecyclerVIEwItem {

    privatE int imageresource;
    private String text_1;

    public int getimageresource() {
        return imageresource;
    }
    public voID setimageresource(int imageresourcE) {
        this.imageresource = imageresource;
    }
        public String getText_1() {
            return text_1;
        }
        public voID setText_1(String text_1) {
            this.text_1 = text_1;
        }


    public RecyclerVIEwItem(int imageresource,String text_1) {
        this.imageresource = imageresource;
        this.text_1 = text_1;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的将 ArrayLIst 添加到 Recyclerview全部内容,希望文章能够帮你解决将 ArrayLIst 添加到 Recyclerview所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:添加到