Dynami c LinearLayout внутри ScrollView не работает - PullRequest
0 голосов
/ 27 апреля 2020

У меня есть LinearLayout внутри ScrollView. Я хочу динамически добавлять элементы linearLayout. Я показал проблему в ее простейшей форме. Когда прокрутка прокомментирована, оранжевый экран показывает, что я хочу. Однако, когда я раскомментировал прокручиваемый вид белого экрана, это проблема, которую я пытаюсь решить часами. Я попробовал все, но не работает. В чем проблема.

class Activity2 : AppCompatActivity() {

    companion object {
        fun newIntent(context: Context): Intent {
            val intent = Intent(context, Activity2::class.java)
            return intent
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_2)

        val layout = (applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as? LayoutInflater)
            ?.inflate(R.layout.item_tab, null) as? ConstraintLayout

        layout?.run {
            layoutParams = LinearLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
        }

        linearLayoutTabList.addView(layout)
    }
}

item_tab. xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@android:color/holo_orange_dark">

</androidx.constraintlayout.widget.ConstraintLayout>

активность_2. xml

<LinearLayout 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=".Activity2">

    <!--<HorizontalScrollView
        android:id="@+id/scrollViewTabList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">-->
        <LinearLayout
            android:id="@+id/linearLayoutTabList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:orientation="horizontal"/>

    <!--</HorizontalScrollView >-->

</LinearLayout>
...