Проблема с макетом ограничения не отображается, если я не добавлю абсолютное измерение - PullRequest
0 голосов
/ 22 января 2020

У меня есть этот макет, и я пытаюсь включить соответствующий LL на основе позиции. Однако первый линейный макет будет отображаться и отображаться только в том случае, если я задаю текстовое представление в абсолютном измерении, например 200dp для ширины, я также пробовал 0dp с ограничениями.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:id="@+id/draft_rider_container"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:text="lost"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

<LinearLayout
    android:id="@+id/section"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:visibility="gone">

    <TextView
        android:id="@+id/fake"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        tools:text="Andrea Dovizioso's"></TextView>

</LinearLayout>

<LinearLayout
    android:id="@+id/row"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:visibility="gone">

    <TextView
        android:id="@+id/rider_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        tools:text="Andrea Dovizioso's"></TextView>

</LinearLayout>

Этот макет отображается в виде рециркулятора, который находится на моем фрагменте, который выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".roster.RosterFragment">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/player_rider_list"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#ffffff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="15dp"
    android:src="@drawable/baseline_edit_white_24dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:rippleColor="@color/colorPrimary" />

А вот мой адаптер:

class PlayerRidersAdapter :  RecyclerView.Adapter<PlayerRidersAdapter.ViewHolder>() {

var riderList = ArrayList<Rider>()

override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.rider_item_layout, null, false)
    return ViewHolder(view)
}

override fun getItemCount(): Int {
    return riderList.count() + 2
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    when(position) {
        0 -> showHeader(holder)
        3 -> showSection(holder)
        else -> showRow(holder)
    }
}

fun showHeader(holder: ViewHolder) {
    holder.headerContainer.visibility = View.VISIBLE
    holder.sectionContainer.visibility = View.GONE
    holder.rowContainer.visibility = View.GONE
}

fun showRow(holder: ViewHolder) {
    holder.headerContainer.visibility = View.GONE
    holder.sectionContainer.visibility = View.GONE
    holder.rowContainer.visibility = View.VISIBLE

    //holder.name.text = rider.name
}

fun showSection(holder: ViewHolder) {
    holder.headerContainer.visibility = View.GONE
    holder.sectionContainer.visibility = View.VISIBLE
    holder.rowContainer.visibility = View.GONE
}

fun addRiders(riders : ArrayList<Rider>){
    riderList.addAll(riders)
    notifyDataSetChanged()
}

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    var headerContainer = view.findViewById<LinearLayout>(R.id.header)
    var sectionContainer = view.findViewById<LinearLayout>(R.id.section)
    var rowContainer = view.findViewById<LinearLayout>(R.id.row)
    var name = view.findViewById<TextView>(R.id.rider_name)
}
}

1 Ответ

0 голосов
/ 24 января 2020

В вашем адаптере укажите следующее в onCreateViewHolder () :

val view = LayoutInflater.from(parent.context).inflate(R.layout.rider_item_layout, parent, false)

Указание аргумента root позволит правильно определить размер накаченного макета , Скорее всего, это ваша проблема.

Однако match_parent иногда имеет странное поведение при использовании в ConstraintLayout , поэтому используйте вместо него 0dp и не забывайте ограничивать затронутые стороны , Было бы неплохо, если бы ConstraintLayout либо обработал этот случай, либо сообщил об ошибке, либо, по крайней мере, поддержал проверку Lint.

В сторону: ограничения на TextViews , которые хранятся в LinearLayout , не имеют никакого эффекта. Ограничения применяются только к прямым детям ConstraintLayout .

...