Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

Android Kotlin: How to align vertically RecyclerView in fragment

I wanna align vertically all of items in RecyclerView like ListView but I'm confused to set them.
In other words, They are seen horizontally : https://i.stack.imgur.com/ossLb.jpg
item's image is here : https://i.stack.imgur.com/sjDws.png

Main Activity(CalendarActivity.kt) gets Fragment(HomeFragment) which gets item's information.

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

Many solutions I've seen are Add this RecyclerView.layoutManager = LinearLayoutManager(this) in MainActivity.
Even if I added in CalendarActivity, It returns error :
kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
And In HomeFragment, I added binding.recyclerViewNotes.layoutManager = LinearLayoutManager(activity) in onCreateView, but It returns same error.

It is related to ViewBinding of course. But what I wanna say is I didn't search way with reference.
What shoud I do in this situation? I'm stuck here.
Someone help please? ??

CalendarActivity.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)
        }
    }
}

fragment_home.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>

If you need more detailed information, please comment! thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have already assigned a LinearLayoutManager in your XML, and that's the type that looks like ListView.

So just delete this line where you are changing it to a staggered grid, which is not what you want:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...