为每个回收器制作自定义项目查看项目



我是安卓系统的新手,我想为每个recyclerView项目制作自定义项目。在rv的一个项目中,可能有几个带有数据的linearLayouts。最初,我把线性布局一个接一个地添加到rv的项目中。但在流程中,另一个项目的布局是项目中的重复。

这是我的Adapter类的onBind函数。

fun onBind(letterItem: LetterItem, position: Int, counter: ArrayList<Int>){
val alphabetList = letterItem.list
binding.apply {
cvLetterBig.setCardBackgroundColor(
ContextCompat.getColor(
context,
letterItem.bgColor
)
)
DrawableCompat.setTint(
binding.imageIcon.drawable,
ContextCompat.getColor(context, letterItem.bgColor)
)
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, 0, (10 * dp).toInt(), 0, (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.setTextColor(ContextCompat.getColor(context, R.color.grey20_800))
titleView.gravity = Gravity.CENTER_VERTICAL
titleView.setPadding((25 * dp).toInt(), 0, 0, 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://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.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:clipToPadding="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:clipToPadding="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://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.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:outlineSpotShadowColor="@color/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:textColor="@color/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:textColor="@color/grey20_800"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

或者有人知道如何解决它或其他使用方法吗?

您可以为您的案例使用多视图类型和视图支架。请查看此链接,了解如何使用多视图支架进行回收视图

介质极客对极客

最新更新