如何在kotlin的片段内显示网格视图?
Posted
技术标签:
【中文标题】如何在kotlin的片段内显示网格视图?【英文标题】:How to display grid view inside fragment in kotlin? 【发布时间】:2021-11-26 03:31:50 【问题描述】:这是我一直在尝试的一段代码。 适配器代码如下。 制作了一个用于存储数据的数组列表,并希望使用网格视图将其显示在片段中。应用程序正在运行,但未显示网格布局。
import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import com.shyptsolution.nitrr.R
class GalleryAdapter(gallerylist:ArrayList<GalleryDataModel>):BaseAdapter()
var galleryList=gallerylist
var image:ImageView?=null
override fun getCount(): Int
return galleryList.size
override fun getItem(position: Int): Any
return galleryList[position]
override fun getItemId(position: Int): Long
return position.toLong()
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View
var inflator=LayoutInflater.from(parent?.context).inflate(R.layout.gallery_ticket,null)
image=inflator.findViewById(R.id.gallery)
image?.setImageResource(galleryList[position].image)
return inflator
片段代码如下
package com.shyptsolution.nitrr.gallery
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import com.shyptsolution.nitrr.R
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [Gallery.newInstance] factory method to
* create an instance of this fragment.
*/
class Gallery : Fragment()
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
lateinit var adapter:GalleryAdapter
var gallerygrid=activity?.findViewById<GridView>(R.id.galllerygrid)
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
arguments?.let
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View?
// Inflate the layout for this fragment
adapter= GalleryAdapter(GalleryData.galleryphoto)
gallerygrid?.adapter=adapter
return inflater.inflate(R.layout.fragment_gallery, container, false)
companion object
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Gallery.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
Gallery().apply
arguments = Bundle().apply
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
我该怎么做,我已经在片段中完成了回收器视图,但无法解决这里的问题。
【问题讨论】:
你试过用GridLayoutManager
吗? developer.android.com/reference/kotlin/androidx/recyclerview/…
我的问题解决了,谢谢。
【参考方案1】:
这是我所做的更改列表,现在它工作正常。
package com.shyptsolution.nitrr.gallery
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridLayout
import android.widget.GridView
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.GridLayoutManager
import com.shyptsolution.nitrr.R
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [Gallery.newInstance] factory method to
* create an instance of this fragment.
*/
class Gallery : Fragment()
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
var listofphoto=GalleryData.galleryphoto
lateinit var adapter:GalleryAdapter
var gallerygrid=activity?.findViewById<GridView>(R.id.galllerygrid)
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
arguments?.let
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
// adapter=GalleryAdapter(listofphoto)
// var galleryview=activity?.findViewById<GridView>(R.id.galllerygrid)
// galleryview?.adapter=adapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View?
// Inflate the layout for this fragment
// adapter= GalleryAdapter(GalleryData.galleryphoto)
// gallerygrid?.adapter=adapter
var view:View=inflater.inflate(R.layout.fragment_gallery, container, false)
gallerygrid=view.findViewById(R.id.galllerygrid)
adapter= GalleryAdapter(listofphoto)
gallerygrid?.adapter=adapter
return view
companion object
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Gallery.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
Gallery().apply
arguments = Bundle().apply
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
添加图库适配器,然后设置网格适配器。它工作正常。
【讨论】:
以上是关于如何在kotlin的片段内显示网格视图?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 jetpack compose 中使用改造 API 创建列表视图/网格视图/回收器视图
在网格视图中的 ASCX 控件内的控件上使用 Javascript 显示隐藏。 (ASP.NET + Javascript)