Android 循环利用器视图和卡片在滚动时创建多个副本

Android 循环利用器视图和卡片在滚动时创建多个副本,android,android-studio,android-layout,kotlin,android-cardview,Android,Android Studio,Android Layout,Kotlin,Android Cardview,我正在尝试用一个带卡片的回收器视图制作一张底纸。我可以把卡片放在最下面的一张纸上,但是我的回收视图有问题。如图所示,它创建了多个底部表单,其中包含卡片。我曾试图解决这个问题,但迄今为止没有运气。我如何才能使其底部的表包含一个回收视图卡?我认为问题在于活动\u main中的Recyclerview,但不知道放在哪里 MainActivity.kt class MainActivity : AppCompatActivity() { override fun onCreate(save

我正在尝试用一个带卡片的回收器视图制作一张底纸。我可以把卡片放在最下面的一张纸上,但是我的回收视图有问题。如图所示,它创建了多个底部表单,其中包含卡片。我曾试图解决这个问题,但迄今为止没有运气。我如何才能使其底部的表包含一个回收视图卡?我认为问题在于
活动\u main
中的Recyclerview,但不知道放在哪里

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (supportActionBar != null)
            supportActionBar?.hide()
        val modelList = readFromAsset()

        val adapter = CustomAdapter(modelList, this)

        rcv.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) as RecyclerView.LayoutManager?
        rcv.adapter = adapter;


        configureBackdrop()
    }

    private var mBottomSheetBehavior: BottomSheetBehavior<View?>? = null

    private fun configureBackdrop() {
        // Get the fragment reference
        val fragment = supportFragmentManager.findFragmentById(R.id.filter_fragment)


        fragment?.let {
            // Get the BottomSheetBehavior from the fragment view
            BottomSheetBehavior.from(it.view)?.let { bsb ->

                mBottomSheetBehavior = bsb
            }
        }
    }


    private fun readFromAsset(): List<Model> {

        val modeList = mutableListOf<Model>()
        val bufferReader = application.assets.open("android_version.json").bufferedReader()
        val json_string = bufferReader.use {
            it.readText()
        }

        val jsonArray = JSONArray(json_string);
        for (i in 0..jsonArray.length() - 1) {
            val jsonObject: JSONObject = jsonArray.getJSONObject(i)
            val model = Model(jsonObject.getString("name"), jsonObject.getString("version"))
            modeList.add(model)
        }
        return modeList
    }
}
class MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
如果(supportActionBar!=null)
supportActionBar?.hide()
val modelList=readFromAsset()
val adapter=CustomAdapter(模型列表,此)
rcv.layoutManager=LinearLayoutManager(this,RecyclerView.VERTICAL,false)作为RecyclerView.layoutManager?
rcv.adapter=适配器;
配置背景()
}
私有变量mBottomSheetBehavior:BottomSheetBehavior?=null
私人娱乐配置背景(){
//获取片段引用
val fragment=supportFragmentManager.findFragmentById(R.id.filter_fragment)
碎片?,让我来{
//从片段视图中获取BottomSheetBehavior
BottomSheetBehavior.from(it.view)?.let{bsb->
mBottomSheetBehavior=bsb
}
}
}
private fun readFromAsset():列表{
val modeList=mutableListOf()
val bufferReader=application.assets.open(“android_version.json”).bufferedReader()
val json_string=bufferReader.use{
it.readText()
}
val jsonArray=jsonArray(json_字符串);
对于(i in 0..jsonArray.length()-1){
val-jsonObject:jsonObject=jsonArray.getJSONObject(i)
val model=model(jsonObject.getString(“名称”),jsonObject.getString(“版本”))
modeList.add(模型)
}
回归模型学家
}
}
CustomAdapter.kt

class CustomAdapter(val modelList: List<Model>, val context: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as ViewHolder).bind(modelList.get(position));
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        return ViewHolder(layoutInflater.inflate(R.layout.backdrop_fragment, parent, false))
    }


    override fun getItemCount(): Int {
        return modelList.size;
    }

    lateinit var mClickListener: ClickListener

    fun setOnItemClickListener(aClickListener: ClickListener) {
        mClickListener = aClickListener
    }

    interface ClickListener {
        fun onClick(pos: Int, aView: View)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        init {
            itemView.setOnClickListener(this)
        }

        override fun onClick(p0: View?) {
            mClickListener.onClick(adapterPosition, itemView)
        }

        fun bind(model: Model): Unit {
            itemView.txt.text = model.name
            itemView.sub_txt.text = model.version

            val id = context.resources.getIdentifier(model.name.toLowerCase(), "drawable", context.packageName)
            itemView.img.setBackgroundResource(id)
        }

    }
}
class CustomAdapter(val模型列表:列表,val上下文:上下文):
RecyclerView.Adapter(){
覆盖onBindViewHolder(holder:RecyclerView.ViewHolder,位置:Int){
绑定(modelList.get(position));
}
override fun onCreateViewHolder(父级:ViewGroup,viewType:Int):RecyclerView.ViewHolder{
val layoutInflater=layoutInflater.from(parent.context)
返回视窗支架(LayoutFlater.充气(R.layout.Background_碎片,父对象,假))
}
重写getItemCount():Int{
返回modelList.size;
}
lateinit var mClickListener:ClickListener
有趣的setOnItemClickListener(ACLicklListener:ClickListener){
mClickListener=aClickListener
}
界面ClickListener{
趣味onClick(位置:Int,视图:View)
}
内部类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView),View.OnClickListener{
初始化{
itemView.setOnClickListener(此)
}
覆盖有趣的onClick(p0:视图?){
onClick(adapterPosition,itemView)
}
趣味绑定(模型:模型):单位{
itemView.txt.text=model.name
itemView.sub_txt.text=model.version
val id=context.resources.getIdentifier(model.name.toLowerCase(),“drawable”,context.packageName)
itemView.img.setBackgroundResource(id)
}
}
}
主要活动

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:text="@string/main_activity_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <androidx.recyclerview.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:id="@+id/rcv"/>

    <fragment
        app:behavior_hideable="false"
        app:behavior_peekHeight="100dp"

        android:layout_marginTop="?attr/actionBarSize"
        app:behavior_skipCollapsed="false"
        android:id="@+id/filter_fragment"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        android:layout_height="match_parent"
        android:layout_width="match_parent"

        android:name="behavior.sheet.bottom.BackdropFragment" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

背景碎片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:background="@drawable/backdrop_fragment_background"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/backdrop_content" />


    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:orientation="vertical"
        card_view:cardCornerRadius="30dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="false"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:text="Title"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/img"
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:layout_marginStart="25dp"
                android:layout_toRightOf="@+id/txt"
                android:contentDescription="@string/app_name" />

            <TextView
                android:id="@+id/sub_txt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="25dp"
                android:layout_toRightOf="@+id/img"
                android:autoSizeMaxTextSize="8sp"
                android:autoSizeMinTextSize="6sp"
                android:autoSizeStepGranularity="2sp"
                android:autoSizeTextType="uniform"
                android:text="Title" />
        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>

您在
活动_main
布局文件中有
回收视图
,而不是在
片段
布局文件中

下面是您应该做的,创建一个继承
BottomSheetDialogFragment()
的类。见下文

app/build.gradle
中添加依赖项
implementation'com.google.android.material:material:1.2.0-alpha04'

创建底部工作表类

class BottomSheetExampleDialogFragment : BottomSheetDialogFragment() {

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
) : View? =
    inflater.inflate(R.layout.bottom_sheet_example_dialog_fragment, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
       super.onViewCreated(view, savedInstanceState)

       // Handle RecyclerView here
    }
}
底部图纸布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

请将底部纸张线性布局设置为包裹内容

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        android:background="@drawable/backdrop_fragment_background"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/backdrop_content" />


    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:orientation="vertical"
        card_view:cardCornerRadius="30dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="false"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:text="Title"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/img"
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:layout_marginStart="25dp"
                android:layout_toRightOf="@+id/txt"
                android:contentDescription="@string/app_name" />

            <TextView
                android:id="@+id/sub_txt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="25dp"
                android:layout_toRightOf="@+id/img"
                android:autoSizeMaxTextSize="8sp"
                android:autoSizeMinTextSize="6sp"
                android:autoSizeStepGranularity="2sp"
                android:autoSizeTextType="uniform"
                android:text="Title" />
        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>


谢谢,但这会破坏底部的表单并使应用程序崩溃。你知道在一张底部表单中显示“回收器”视图中所有卡片的方法吗?我做了一些工作,但它现在创建了许多单独的底部表单。有没有一种方法可以将所有卡片的信息都显示在底页的列表中?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        android:background="@drawable/backdrop_fragment_background"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/backdrop_content" />


    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:orientation="vertical"
        card_view:cardCornerRadius="30dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="false"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:text="Title"
                android:textSize="20sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/img"
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:layout_marginStart="25dp"
                android:layout_toRightOf="@+id/txt"
                android:contentDescription="@string/app_name" />

            <TextView
                android:id="@+id/sub_txt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="25dp"
                android:layout_toRightOf="@+id/img"
                android:autoSizeMaxTextSize="8sp"
                android:autoSizeMinTextSize="6sp"
                android:autoSizeStepGranularity="2sp"
                android:autoSizeTextType="uniform"
                android:text="Title" />
        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</LinearLayout>