Closed3

RecyclerView の listitem の指定方法

tkttkt
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:listitem="@layout/recyclerview_item"
        android:padding="@dimen/big_padding"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

https://github.com/tktcorporation/android-room-with-a-view-kotlin/blob/7fcc6cfe402ce91f9584ba334279d35c1b10377b/app/src/main/res/layout/activity_main.xml

要素を xml の中で id を用いて指定している

tkttkt
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/list"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView">

        </androidx.recyclerview.widget.RecyclerView>
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return MainFragmentBinding.inflate(inflater, container, false)
            .apply {

                ...

                list.run {
                    layoutManager = LinearLayoutManager(context)
                    addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))

                    adapter = UserListAdapter(viewLifecycleOwner, this@MainFragment.viewModel).also {
                        userListAdapter = it
                    }

                }
            }
            .run {
                root
            }
    }

https://qiita.com/que9/items/52725c3caf33f2a9b44e#fragmentクラス-mainfragment
xml には記述せず、fragment 側のコードで入れ込んでいるっぽい?

このスクラップは2020/12/25にクローズされました