我有一个名为 ShoppingListActivity 的活动。它的布局中唯一的东西是一个名为 ShoppingListFragment 的片段。该片段获取一个 listId 作为参数,并应列出 RecyclerView 中 id = listId 的列表中的所有项目以及该列表的名称。当 ShoppingListFragment 被调用时,名称会正确显示,但 recyclerview 永远不会显示。
ShoppingListActivity 的代码是:
class ShoppingListActivity : AppCompatActivity() {
private var _binding: ActivityShoppingListBinding? = null
private val binding get() = _binding!!
private lateinit var shoppingListViewModel: ShoppingListViewModel
private lateinit var shoppingListNameTextView: TextView
private lateinit var recyclerView: RecyclerView
private lateinit var shoppingList: ShoppingList
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_shopping_list)
var listId = intent.getIntExtra("ShoppingListId",0)
if(savedInstanceState == null)
{
supportFragmentManager.commit {
add(R.id.shopping_list_fragment, ShoppingListFragment(listId))
setReorderingAllowed(true)
}
}
}
}
ShoppingListActivity 的 .xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/activity_shopping_list"
tools:context=".Activities.ShoppingListActivity">
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shopping_list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ShoppingListFragment 代码:
class ShoppingListFragment(private val listId:Int) : Fragment() {
private var _binding: FragmentShoppingListBinding? = null
private val binding get() = _binding!!
private lateinit var shoppingListViewModel: ShoppingListViewModel
private lateinit var shoppingListNameTextView: TextView
private lateinit var recyclerView: RecyclerView
private lateinit var shoppingList: ShoppingList
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_shopping_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//DVATA VIEWMODELS, DA SE ZEMI LISTATA SO ITEMS I DA SE PUSTI NAPRED
_binding = FragmentShoppingListBinding.bind(view)
recyclerView = binding.shoppingListRecyclerView
var context = requireContext()
val shoppingListViewModelFactory = ShoppingListViewModelFactory(context)
shoppingListViewModel = ViewModelProvider(this,shoppingListViewModelFactory)[ShoppingListViewModel::class.java]
shoppingListNameTextView = binding.shoppingListName
shoppingList = shoppingListViewModel.getListById(listId)
shoppingListNameTextView.text = shoppingList.Name
var itemsInList = shoppingListViewModel.getItemsInShoppingList(shoppingList)
var adapter = SingleShoppingListAdapter(itemsInList)
recyclerView.adapter = adapter
}
}
ShoppingListFragment 的 .xml 文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/shopping_list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UI.Fragments.ShoppingListFragment">
<TextView
android:id="@+id/shopping_list_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:layout_marginTop="44dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="50dp"
android:id="@+id/shopping_list_recycler_view"
android:layout_height="150dp"
android:layout_marginTop="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shopping_list_name"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_item_to_shopping_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="60dp"
android:layout_marginBottom="76dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_menu_add" />
</androidx.constraintlayout.widget.ConstraintLayout>
recycler_view_shopping_list_item_element.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="10dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/shopping_list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="-16dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteY="1dp" />
<TextView
android:id="@+id/shopping_list_item_quantity"
android:layout_width="10dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/shopping_list_item_name"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
SingleShoppingListAdapter 的代码:
class SingleShoppingListAdapter(private val listItems: ArrayList<ShoppingListItem>) : RecyclerView.Adapter<SingleShoppingListAdapter.SingleShoppingListViewHolder>() {
inner class SingleShoppingListViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private var name: TextView = view.findViewById(R.id.shopping_list_item_name)
private var quantity: TextView = view.findViewById(R.id.shopping_list_item_quantity)
fun bind(item: ShoppingListItem)
{
name.text = item.Name
quantity.text = item.Quantity.toString()
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): SingleShoppingListViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_view_shopping_list_item_element,parent,false)
return SingleShoppingListViewHolder(view)
}
override fun getItemCount(): Int {
return listItems.size
}
override fun onBindViewHolder(holder: SingleShoppingListViewHolder, position: Int) {
holder.bind(listItems[position])
}
}
当我尝试调试它时,传递给适配器的 listItems 永远不会为空,并且适配器会被创建,但 onCreateViewHolder、getItemCount、onBindViewHolder 方法永远不会被调用。
您忘记在recyclerView中添加layoutManager,这似乎是您的内容不可见的原因。
您可以通过两种方式添加布局管理器。
类型1
<androidx.recyclerview.widget.RecyclerView
android:layout_width="50dp"
android:id="@+id/shopping_list_recycler_view"
android:layout_height="150dp"
android:layout_marginTop="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
//add this line. app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shopping_list_name"
app:layout_constraintVertical_bias="0.0" />