I'm trying to create simple Recyclerview with view binding.
The problem is that my recycler view has no divider. it seems all of my items are
in one row.
Here are my codes :
Here is activity_main.xml :
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity"
>
<com.google.android.material.appbar.MaterialToolbar
android:id="@ id/tool"
android:background="@color/c4"
android:layout_width="0dp"
app:title="Kotlin-Recyclerview"
app:titleTextColor="@color/white"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
android:padding="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/tool" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is recyclerview_rows.xml :
<androidx.cardview.widget.CardView 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="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="@color/black"
android:text="Name"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.09"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04000002" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
RV.Model.kt :
data class RV_Model(val name:String)
RV_Adapter.kt :
class RV_Adapter(private val mylist:List<RV_Model>) :RecyclerView.Adapter<RV_Adapter.MyViewholder>() {
lateinit var adapter_binding:RecyclerviewRowsBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewholder {
adapter_binding = RecyclerviewRowsBinding.inflate(LayoutInflater.from(parent.context),parent,false);
return MyViewholder(adapter_binding)
}
override fun onBindViewHolder(holder: MyViewholder, position: Int) {
val current_item = mylist[position]
adapter_binding.text.text= current_item.name
}
class MyViewholder(val adapter_binding:RecyclerviewRowsBinding) :RecyclerView.ViewHolder(adapter_binding.root) {
}
override fun getItemCount(): Int {
return mylist.size
}
}
MainActivity.kt :
class MainActivity : AppCompatActivity() {
lateinit var binding:ActivityMainBinding
var mylist= ArrayList<RV_Model>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
set_data()
binding.recyclerView.layoutManager= LinearLayoutManager(this)
binding.recyclerView.adapter=RV_Adapter(mylist)
binding.recyclerView.setHasFixedSize(true)
}
fun set_data() {
mylist.add(RV_Model("A"))
mylist.add(RV_Model("B"))
mylist.add(RV_Model("C"))
mylist.add(RV_Model("D"))
}
}
what is the problem ?
CodePudding user response:
change height of recyclerview's and row textview's to wrap content.
CodePudding user response:
There are a lot of ways to create things that divide or sit between items in the list (e.g. adding a visual element to the bottom of your item layout, or adding visual items between your data items) but the simplest way is to just use the standard divider ItemDecoration
:
val divider = DividerItemDecoration(requireContext(), layoutManager.orientation)
recyclerView.addItemDecoration(divider)
There isn't one by default (as you've noticed!)