首页 > 解决方案 > Android Kotlin:如何在片段中垂直对齐 RecyclerView

问题描述

我想像 ListView 一样垂直对齐 RecyclerView 中的所有项目,但我对设置它们感到困惑。
换句话说,它们是水平看到的:https
://i.stack.imgur.com/ossLb.jpg 项目的图像在这里:https

://i.stack.imgur.com/sjDws.png Main Activity(CalendarActivity.kt ) 得到 Fragment(HomeFragment) ,它获取项目的信息。

CalendarActivity (Main Activity)
L HomeFragment (items are shown in here)
  L item (note_layout.xml)

我见过的许多解决方案都是RecyclerView.layoutManager = LinearLayoutManager(this)在 MainActivity 中添加这个。
即使我在 CalendarActivity 中添加,它也会返回错误:
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
在 HomeFragment 中,我binding.recyclerViewNotes.layoutManager = LinearLayoutManager(activity)在 onCreateView 中添加,但它返回相同的错误。

它当然与ViewBinding有关。但我想说的是我没有参考参考。
在这种情况下我该怎么办?我被困在这里。
有人帮忙吗?

日历活动.kt

class CalendarActivity : AppCompatActivity() {
    val TAG: String = "CalenderActivity"
    //private lateinit var binding: FragmentHomeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_calendar)
        Log.d(TAG, "CalendarActivity - onCreate() called")
        //binding = FragmentHomeBinding.inflate(layoutInflater)
        //binding.recyclerViewNotes.layoutManager = LinearLayoutManager(this)
        //setContentView(binding.root)

        val navController = Navigation.findNavController(this, R.id.fragment)
        NavigationUI.setupActionBarWithNavController(this, navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        Log.d(TAG, "CalendarActivity - onSupportNavigateUp() called")
        return NavigationUI.navigateUp(
            Navigation.findNavController(this, R.id.fragment),
            null
        )
    }
}

HomeFragment.kt

class HomeFragment : BaseFragment() {
    private lateinit var binding: FragmentHomeBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //binding.recyclerViewNotes.layoutManager = LinearLayoutManager(activity)
        binding = FragmentHomeBinding.inflate(inflater)
        return binding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        binding.recyclerViewNotes.setHasFixedSize(true)
        binding.recyclerViewNotes.layoutManager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)

        launch {
            context?.let {
                val notes = NoteDatabase(it).getNoteDao().getAllNotes()
                binding.recyclerViewNotes.adapter = NotesAdapter(notes)
            }
        }

        binding.buttonAdd.setOnClickListener {
            val action = HomeFragmentDirections.actionAddNote()
            Navigation.findNavController(it).navigate(action)
        }
    }
}

片段主页.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".ui.HomeFragment"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_notes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/button_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="40dp"
        android:clickable="true"
        android:src="@drawable/ic_add" />
</LinearLayout>

note_layout.xml

<?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="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/text_date"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="2"
        android:text="2021/01/01"
        android:textColor="#111111"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/text_view_title"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:layout_marginTop="5dp"
        android:text="Title"
        android:textStyle="bold"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline" />

</LinearLayout>

如果您需要更详细的信息,请发表评论!谢谢你!

标签: androidkotlinandroid-recyclerview

解决方案


你已经在你的 XML 中分配了一个 LinearLayoutManager,这就是看起来像 ListView 的类型。

因此,只需删除您将其更改为交错网格的这一行,这不是您想要的:

binding.recyclerViewNotes.layoutManager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)

推荐阅读