Вот мой фрагмент XML:
fragment_leaderboards.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/leaderboards_list"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
и мой пользовательский вид для LayoutInflater для ViewHolder:
leaderboard_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="@+id/leaderboard_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Title"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imgShare"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@android:drawable/ic_menu_share"
android:tint="#eeeeee"/>
</LinearLayout>
Приведенный выше XML будет отображать только 1 элемент моего RecyclerView. Также будет отображаться только 1 элемент RecyclerView, если я помещу CardView в LinearLayout: <LinearLayout><Cardview>...</Cardview></LinearLayout>
Однако, если я оберну LinearLayout
в CardView
, он покажет все элементы (которых на данный момент 2): <Cardview><LinearLayout>...</LinearLayout></Cardview>
.
Почему это?
Вот мой код Котлина, если вам интересно:
фрагмент
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_leaderboards, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val viewManager: RecyclerView.LayoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
val viewAdapter: RecyclerView.Adapter<*> = LeaderboardsAdapter(arrayListOf(Player("James", 23, "male"), Player("Jane", 22, "female")))
val recyclerView: RecyclerView = leaderboards_list.apply {
adapter = viewAdapter
layoutManager = viewManager
setHasFixedSize(true)
}
}
LeaderboardsAdapter.kt
class LeaderboardsAdapter(private val players: ArrayList<Player>) : RecyclerView.Adapter<LeaderboardsAdapter.ViewHolder>(){
class ViewHolder(val v: View) : RecyclerView.ViewHolder(v)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LeaderboardsAdapter.ViewHolder {
Log.d("Viewgroup", "$parent.toString() $parent.")
val view = LayoutInflater.from(parent.context).inflate(R.layout.leaderboard_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: LeaderboardsAdapter.ViewHolder, position: Int) {
holder.v.leaderboard_name.text = players[position].name
}
override fun getItemCount(): Int = players.size
}