如何在 kotlin 中的片段内显示网格视图?

How to display grid view inside fragment in kotlin?

这是我一直在尝试的一段代码。 适配器代码如下。 制作了一个用于存储数据的数组列表,并希望使用网格视图将其显示在片段中。该应用 运行 但未显示网格布局。


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)
                }
            }
    }
}

怎么办,我在fragment里做了recycler view,但是没弄清楚这里的问题

这是我所做的更改列表,现在可以正常工作了。

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)
                }
            }
    }
}

添加了图库适配器,然后设置网格适配器。它工作正常。