Search code examples
androidkotlinandroid-fragmentsandroid-fragmentactivity

How to display grid view inside fragment in kotlin?


Here is the piece of code that I have been trying. The adapter code is below. Made an array list for storing the data, and want to display it in the fragment using grid view. The app is running but its not showing the grid layout.


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

The code for the fragment is given below

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

How can I do it, I have done recycler view in the fragment but couldn't figure out the problem here.


Solution

  • Here is the list of changes that I have made and now its working fine.

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

    Added the Gallery adapter and then set the grid adapter. Its working fine.