android - 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>
如果您需要更详细的信息,请发表评论!谢谢你!
解决方案
你已经在你的 XML 中分配了一个 LinearLayoutManager,这就是看起来像 ListView 的类型。
因此,只需删除您将其更改为交错网格的这一行,这不是您想要的:
binding.recyclerViewNotes.layoutManager = StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)
推荐阅读
- javascript - 条件 html 元素仅临时显示
- laravel - 如何使用 vscode 查看 $request 及其子方法中的所有可用方法?
- javascript - Discord.js V12 如何从 args 中删除提到的用户?
- javascript - 如何在博客上按月调用 javascript 函数
- ios - 为什么自动分配的经纬度在 SwiftUI 中不起作用?
- python - 你怎么称呼论点?
- python - 不能对来自 PIL lib 的 png 文件使用 Image.putalpha()。OSError:无法将模式 PA 写入 PNG
- java - 在recyclerview中从firebase获取数据时一次又一次地获取重复数据
- swift - NavigationLink 的目标视图在变量更改时不会更新
- django - 对 Django 使用 dotenv 方法后,API KEY 在 https 查询中仍然可见