程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为每个 recyclerView 项目制作自定义项目大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为每个 recyclerView 项目制作自定义项目?

开发过程中遇到为每个 recyclerView 项目制作自定义项目的问题如何解决?下面主要结合日常开发的经验,给出你关于为每个 recyclerView 项目制作自定义项目的解决方法建议,希望对你解决为每个 recyclerView 项目制作自定义项目有所启发或帮助;

我是 androID 新手,我想为每个 recyclerVIEw 项目制作自定义项目。在 rv 的一项中,可能有几个带有数据的线性布局。最初我将线性布局一一添加到 rv 的项目中。但在另一个项目的过程中,项目中的布局是重复的。

这是我的 Adapter 类的 onBind 函数。

fun onBind(letterItem: LetterItem,position: Int,counter: ArrayList<Int>){
        val AlphabetList = letterItem.List
          binding.apply {
            cvLetterBig.setCardBACkground@R_301_6004@(
                ContextCompat.get@R_301_6004@(
                    context,letterItem.bg@R_301_6004@
                )
            )
            DrawableCompat.setTint(
                binding.imageIcon.drawable,ContextCompat.get@R_301_6004@(context,letterItem.bg@R_301_6004@)
            )
            textLetterBig.text = letterItem.text
            binding.linearLayout.visibility = VIEw.GONE

            for (x in 0 until AlphabetList.sizE) {
                Log.d("-------------","onBind: x: $x")
                val ll = binding.llAdding
                val dp = context.resources.displaymetrics.density

                val layout = linearLayout(context)
                layout.orIEntation = linearLayout.HORIZONTAL
                layout.layoutParams =
                    linearLayout.LayoutParams(
                        linearLayout.LayoutParams.MATCH_PARENT,linearLayout.LayoutParams.MATCH_PARENT
                    )
                VIEwUtils.setmargins(layout,(10 * dp).toInt(),(10 * dp).toInt())

                val titleVIEw = TextVIEw(context)
                val tlparams =
                    VIEwGroup.LayoutParams(
                        VIEwGroup.LayoutParams.MATCH_PARENT,VIEwGroup.LayoutParams.WRAP_CONTENT
                    )
                titleVIEw.ID = x
                titleVIEw.layoutParams = tlparams
                titleVIEw.text = context.getString(AlphabetList[x])
                titleVIEw.textSize = 20F
                titleVIEw.setText@R_301_6004@(ContextCompat.get@R_301_6004@(context,R.@R_301_6004@.grey20_800))
                titleVIEw.gravity = Gravity.CENTER_VERTICAL
                titleVIEw.setpadding((25 * dp).toInt(),0)

                val imageVIEw = ImageVIEw(context)
                val ilparams =
                    linearLayout.LayoutParams(
                        VIEwGroup.LayoutParams.WRAP_CONTENT,linearLayout.LayoutParams.WRAP_CONTENT
                    )

                ilparams.gravity = Gravity.CENTER
                imageVIEw.layoutParams = ilparams
                imageVIEw.setimageDrawable(
                    ContextCompat.getDrawable(
                        context,R.drawable.ic_checked
                    )
                )
                layout.ID = x
                layout.tag = x
                layout.addVIEw(imageVIEw)
                layout.addVIEw(titleVIEw)
                layout.setonClickListener(clickListener)

                ll.addVIEw(layout) // here im adding that layout,whit in proccess it is duplicaTing
              }
        }

这是我的片段

<?xml version="1.0" enCoding="utf-8"?>
<layout xmlns:androID="http://scheR_936_11845@as.androID.com/apk/res/androID"
    xmlns:app="http://scheR_936_11845@as.androID.com/apk/res-auto"
    xmlns:tools="http://scheR_936_11845@as.androID.com/tools">

    <linearLayout
        androID:layout_wIDth="match_parent"
        androID:layout_height="match_parent">
        <androIDx.consTraintlayout.Widget.ConsTraintLayout
            androID:layout_wIDth="match_parent"
            androID:layout_height="match_parent">

            <androIDx.recyclervIEw.Widget.RecyclerVIEw
                androID:ID="@+ID/rv_Alphabet"
                androID:layout_wIDth="match_parent"
                androID:layout_height="wrap_content"
                androID:cli@R_772_10866@adding="false"
                androID:layout_marginHorizontal="25dp"
                androID:layout_margintop="25dp"
                androID:padding="10dp"
                androID:BACkground="@drawable/bg_Alphabet"
                app:layout_consTraintEnd_toEndOf="parent"
                app:layout_consTraintStart_toStartOf="parent"
                app:layout_consTrainttop_totopOf="parent" />

            <androIDx.recyclervIEw.Widget.RecyclerVIEw
                androID:ID="@+ID/rv_big_Alphabet"
                androID:layout_wIDth="match_parent"
                androID:layout_height="0dp"
                androID:paddingVertical="8dp"
                androID:cli@R_772_10866@adding="true"
                androID:layout_marginHorizontal="25dp"
                app:layout_consTraintBottom_toBottomOf="parent"
                app:layout_consTraintEnd_toEndOf="parent"
                app:layout_consTraintleft_toleftOf="parent"
                app:layout_consTrainttop_toBottomOf="@ID/rv_Alphabet" />

        </androIDx.consTraintlayout.Widget.ConsTraintLayout>
    </linearLayout>
</layout>

这是rv的下一项

<?xml version="1.0" enCoding="utf-8"?>
<layout xmlns:androID="http://scheR_936_11845@as.androID.com/apk/res/androID"
    xmlns:app="http://scheR_936_11845@as.androID.com/apk/res-auto"
    xmlns:tools="http://scheR_936_11845@as.androID.com/tools">

    <androIDx.consTraintlayout.Widget.ConsTraintLayout
        androID:ID="@+ID/cl_big"
        androID:layout_wIDth="match_parent"
        androID:layout_height="wrap_content"
        androID:layout_marginVertical="10dp">

        <androIDx.cardvIEw.Widget.CardVIEw
            androID:ID="@+ID/cv_letter_big"
            androID:layout_wIDth="match_parent"
            androID:layout_height="match_parent"
            androID:outlinespotShadow@R_301_6004@="@@R_301_6004@/white"
            app:cardCornerRadius="8dp"
            app:layout_consTraintBottom_toBottomOf="parent"
            app:layout_consTraintEnd_toEndOf="parent"
            app:layout_consTraintStart_toStartOf="parent"
            app:layout_consTrainttop_totopOf="parent">

            <androIDx.consTraintlayout.Widget.ConsTraintLayout
                androID:ID="@+ID/cl_outter"
                androID:layout_wIDth="match_parent"
                androID:layout_height="match_parent">

                <TextVIEw
                    androID:ID="@+ID/text_letter_big"
                    androID:layout_wIDth="match_parent"
                    androID:layout_height="40dp"
                    androID:gravity="center"
                    androID:text="A"
                    androID:text@R_301_6004@="@@R_301_6004@/white"
                    androID:textSize="20sp"
                    app:layout_consTraintEnd_toEndOf="parent"
                    app:layout_consTraintStart_toStartOf="parent"
                    app:layout_consTrainttop_totopOf="parent" />

                <androIDx.consTraintlayout.Widget.ConsTraintLayout
                    androID:ID="@+ID/cl_inner"
                    androID:layout_wIDth="match_parent"
                    androID:layout_height="match_parent"
                    androID:layout_margin="0.5dp"
                    androID:BACkground="@drawable/bg_Alphabet"
                    app:layout_consTraintleft_toleftOf="parent"
                    app:layout_consTraintright_toRightOf="parent"
                    app:layout_consTrainttop_toBottomOf="@ID/text_letter_big">

                    <linearLayout
                        androID:ID="@+ID/ll_adding"
                        androID:layout_wIDth="match_parent"
                        androID:layout_height="match_parent"
                        androID:orIEntation="vertical"
                        androID:paddingVertical="20dp"
                        androID:paddingStart="20dp"
                        app:layout_consTraintBottom_toBottomOf="parent"
                        app:layout_consTraintEnd_toEndOf="parent"
                        app:layout_consTraintStart_toStartOf="parent"
                        app:layout_consTrainttop_totopOf="parent">


                        <linearLayout
                            androID:ID="@+ID/linearLayout"
                            androID:layout_wIDth="match_parent"
                            androID:layout_height="match_parent"
                            androID:layout_marginBottom="10dp"
                            androID:orIEntation="horizontal">

                            <ImageVIEw
                                androID:ID="@+ID/image_icon"
                                androID:layout_wIDth="20dp"
                                androID:layout_height="20dp"
                                androID:layout_gravity="center_vertical"
                                app:srcCompat="@drawable/ic_checked" />

                            <TextVIEw
                                androID:layout_wIDth="match_parent"
                                androID:layout_height="wrap_content"
                                androID:layout_marginStart="25dp"
                                androID:gravity="center_vertical"
                                androID:text="Advocat"
                                androID:text@R_301_6004@="@@R_301_6004@/grey20_800"
                                androID:textSize="20sp" />
                        </linearLayout>
                    </linearLayout>
                </androIDx.consTraintlayout.Widget.ConsTraintLayout>
            </androIDx.consTraintlayout.Widget.ConsTraintLayout>
        </androIDx.cardvIEw.Widget.CardVIEw>
    </androIDx.consTraintlayout.Widget.ConsTraintLayout>
</layout>

或者有没有人知道怎么解决或者其他的使用方法?

解决方法

您可以为您的案例使用多种视图类型和视图支架,请查看此链接以了解如何使用多个视图支架制作 recyclerview

@H_114_2@medium geeksforgeeks

大佬总结

以上是大佬教程为你收集整理的为每个 recyclerView 项目制作自定义项目全部内容,希望文章能够帮你解决为每个 recyclerView 项目制作自定义项目所遇到的程序开发问题。

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

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