Fau*_*eca -1 android android-layout android-fragments kotlin android-recyclerview
我的 recyclerview 仅显示 ArrayList 中的第一个项目,当我调试适配器时,它显示 getItemCount() 为 3,看起来不错,据我所知没有运行错误,请帮助:
\n\n这是我的适配器
\n\nclass MyAdapter(private val myDataset: ArrayList<String>) :\n RecyclerView.Adapter<MyAdapter.Holder>() {\n\n\n override fun onCreateViewHolder(parent: ViewGroup,\n viewType: Int): Holder {\n\n val view = LayoutInflater.from(parent.context)\n .inflate(R.layout.list_item, parent, false)\n\n return Holder(view)\n }\n\n\n override fun onBindViewHolder(holder: Holder, position: Int) {\n holder.textView?.text = myDataset[position]\n }\n\n\n override fun getItemCount() = myDataset.size\n\n\n class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {\n var textView:TextView? = null\n\n init {\n textView = itemView.findViewById(R.id.name)\n }\n\n }\n\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是片段:
\n\nclass FragmentList1 : Fragment() {\n\n var myArrayList: ArrayList <String> = arrayListOf("Fausto", "F\xc3\xa9lix", "Nacho")\n\n\n override fun onCreateView(\n inflater: LayoutInflater, container: ViewGroup?,\n savedInstanceState: Bundle?\n ): View? {\n // Inflate the layout for this fragment\n\n var view = inflater.inflate(R.layout.fragment_list1,\n container, false)\n\n var recycler: RecyclerView = view.findViewById(R.id.recyclerView)\n\n recycler.setHasFixedSize(true)\n recycler.layoutManager = LinearLayoutManager(context)\n recycler.adapter = MyAdapter(myArrayList)\n\n return view\n }\nRun Code Online (Sandbox Code Playgroud)\n\n这是fragment_list1.xml:
\n\n <?xml version="1.0" encoding="utf-8"?>\n<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n android:orientation="vertical"\n tools:context=".FragmentList1">\n\n\n <androidx.recyclerview.widget.RecyclerView\n android:id="@+id/recyclerView"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n\n />\n\n</LinearLayout>\nRun Code Online (Sandbox Code Playgroud)\n\n这是list_item.xml:
\n\n <?xml version="1.0" encoding="utf-8"?>\n<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n xmlns:tools="http://schemas.android.com/tools"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n android:orientation="horizontal">\n\n <TextView\n android:id="@+id/name"\n android:layout_width="wrap_content"\n android:layout_height="wrap_content"\n android:layout_marginStart="4dp"\n android:layout_marginLeft="4dp"\n android:layout_marginTop="28dp"\n android:text="Aqui va el nombre"\n />\n\n\n\n</LinearLayout>\nRun Code Online (Sandbox Code Playgroud)\n\n还有一个 MainActivity 托管fragment_list1.xml,还有 nav_graph.xml
\n改变list_item.xml的LinearLayout属性:
android:layout_height="match_parent"
Run Code Online (Sandbox Code Playgroud)
到:
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)
因为第一个项目占用了所有可用空间,并且您看不到其余的项目。
| 归档时间: |
|
| 查看次数: |
1762 次 |
| 最近记录: |