0

之前有人问过这个问题,但这些解决方案并没有帮助我。我有一个可滚动的 RecyclerView,并且在任何时候都只能在屏幕上部分显示。它包含一个 RadioButtons 列表和一些与每个项目相关的文本。当您单击 RadioButton 时,在此之前选择的最后一个按钮应该关闭。它不是。您单击的每个按钮都会被选中,而您之前选择的其他按钮也会保持选中状态。

它是这样设置的:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    tools:context=".ui.SettingsActivity">


<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/video_settings"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:textSize="@dimen/clickableTextSize"
            android:layout_alignParentStart="true"
            />
</RelativeLayout>


<androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="100"
        android:gravity="bottom"
        android:orientation="vertical">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="15"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            android:text="@string/set_resolution_and_fps"
            android:textSize="@dimen/history_text_size"
            android:gravity="center_vertical"
            android:background="@color/grey_200"
            android:textColor="@color/black"
            />

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_video_resolution"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="80"
        android:layout_marginTop="5dp"/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/submitVideoSettings"
        android:text="@string/submit"
        android:layout_marginBottom="5dp"
        android:textColor="@color/black"
        android:layout_gravity="center|bottom"

        />

  </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>

回收站长这样:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resolutionRVRadioButton"
        />
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/resolutionRVTV"
        />

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:text="@string/at_symbol"
        />

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/framesPerSecondRVTV"
        android:layout_marginStart="5dp"
        />

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/fps"
        />
</androidx.appcompat.widget.LinearLayoutCompat>

适配器的代码如下所示。

    class ResolutionRVAdapter(private val sharedNavAndMenuViewModel: SharedNavAndMenuViewModel,
                                private val lastResolutionSelected: String,
                                private val capabilityDataList: List<CameraCapabilitySpecificFPS>) : RecyclerView.Adapter<CameraResolutionRVAdapter.ViewHolder>(){
    private var lastCheckedPosition = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CameraResolutionRVAdapter.ViewHolder {
        val v =  LayoutInflater.from(parent.context).inflate(R.layout.camera_resolution_recycler_item, parent, false)
        val viewHolder = ViewHolder(v)

        Timber.i("Added viewHolder: ${viewHolder.resolutionText.text}")

        return ViewHolder(v)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val resolutionRadioButton : RadioButton = itemView.findViewById(R.id.resolutionRVRadioButton)
        val resolutionText : TextView = itemView.findViewById(R.id.resolutionRVTV)
        val framesPerSecondText : TextView = itemView.findViewById(R.id.framesPerSecondRVTV)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val child = capabilityDataList[position]
        Timber.i("onBindViewHolder currently working with resolution ${child.resolution} range of $child.range")
        holder.resolutionText.text = child.resolution.toString()
        holder.framesPerSecondText.text = child.fps

        Timber.d("OnBindViewholder and lastResolutionSelected = $lastResolutionSelected")

        if(lastResolutionSelected.isEmpty()) {
            if(position == 0) {
                holder.resolutionRadioButton.isChecked = true
                Timber.i("No last resolution was selected. Setting index 0 to true")
            }
        } else if(lastResolutionSelected == holder.resolutionText.text.toString()) {
            Timber.i("An old resolution of ${holder.resolutionText.text} was selected. Setting index $position to true")
            holder.resolutionRadioButton.isChecked = true
        }

        holder.resolutionRadioButton.setOnClickListener {
            Timber.i("Index hit was: $position resolution at that index was: ${holder.resolutionText.text}")

            if(holder.resolutionRadioButton.isChecked) {
                //Button was already checked...must be the same button hit before...nothing to do
            } else {
                holder.resolutionRadioButton.isChecked = true
                runOnUiThread {
                    notifyItemChanged(lastCheckedPosition)
                    notifyItemChanged(position)
                }

                lastCheckedPosition = position
            }
        }
    }

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

谁能告诉我我做错了什么?

4

0 回答 0