如何在使用自定义适配器实现的网格视图中添加 onItemClickListener()(这里我命名为 CategoryAdapter)

How to add an onItemClickListener() in grid View implemented using custom adapter (here i named CategoryAdapter)

MainActivity.kt 文件 - 这是主要的 activity.

如何在使用自定义适配器实现的gridview中添加一个onItemClickListener()(这里我命名为CategoryAdapter)?我喜欢添加它来设置图像中的点击监听器。

MainActivity.kt

 package com.hari.rideit.Controller

 import android.content.Intent
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.MenuItem
 import android.view.View
 import android.widget.AdapterView
 import android.widget.GridView
 import android.widget.Toast
 import androidx.appcompat.app.ActionBarDrawerToggle
 import androidx.appcompat.widget.Toolbar
 import androidx.core.view.GravityCompat
 import androidx.drawerlayout.widget.DrawerLayout
 import androidx.viewpager.widget.ViewPager
 import com.google.android.material.navigation.NavigationView
 import com.hari.rideit.Adapters.CategoryAdapter
 import com.hari.rideit.Adapters.ViewPageAdapter
 import com.hari.rideit.R
 import com.hari.rideit.Services.DataService
 import kotlinx.android.synthetic.main.content_main.*
 import java.util.*

 class MainActivity : AppCompatActivity() , NavigationView.OnNavigationItemSelectedListener{

 lateinit var adapter:CategoryAdapter
 lateinit var toolbar: Toolbar
 lateinit var drawerLayout: DrawerLayout
 lateinit var navView: NavigationView
 lateinit var gridview:GridView
 internal lateinit var viewPager:ViewPager
 override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)
     toolbar = findViewById(R.id.toolbar)
     setSupportActionBar(toolbar)

     drawerLayout = findViewById(R.id.drawer_layout)
     navView = findViewById(R.id.nav_view)
     gridview= findViewById(R.id.contentGridView)

     val toggle = ActionBarDrawerToggle(
             this, drawerLayout, toolbar, 0, 0
     )
     drawerLayout.addDrawerListener(toggle)
     toggle.syncState()
     navView.setNavigationItemSelectedListener(this)
     adapter= CategoryAdapter(this,DataService.categories)
     gridview.adapter= adapter


     viewPager=findViewById<View>(R.id.viewPager) as ViewPager
     val adapter= ViewPageAdapter(this)
     viewPager.adapter=adapter
 }
 

类别适配器: 这是用于获取gridview中的图像和文本的自定义适配器



    package com.hari.rideit.Adapters

    import android.content.Context
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import android.widget.BaseAdapter
    import android.widget.ImageView
    import android.widget.TextView
    import com.hari.rideit.R
    import com.hari.rideit.model.Category

    class CategoryAdapter(context:Context,categories:List<Category>):BaseAdapter(){
    val context= context
    val categories= categories
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val categoryView: View
        categoryView = LayoutInflater.from(context).inflate(R.layout.category_layout, null)
        val categoryImage: ImageView = categoryView.findViewById(R.id.categoryImage)
        val categoryname: TextView = categoryView.findViewById(R.id.categoryText)
        val category = categories[position]
        categoryname.text = category.title
        val resourceId =
            context.resources.getIdentifier(category.image, "drawable", context.packageName)
        categoryImage.setImageResource(resourceId)
        return categoryView
    }

    override fun getItem(position: Int): Any {
        return categories[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getCount(): Int {
       return categories.count()
    }

}

您可以为GridView的每一项添加onClickListener。在 returning categoryView 之前,在其上设置 onClickListener。

categoryView.setOnClickListener { 
        //put your code here
    }

如果您想访问您MainActivity.kt中的点击位置。在 CategoryAdapter class 中创建一个接口。请参阅下面的代码。

 interface OnItemClickListener{
        fun onClick(position:Int)
    }

现在将一个变量传递给类型为 OnItemClickListener 的 CategoryAdapter。

class CategoryAdapter(context:Context,categories:List<Category>, var listener:OnItemClickListener):BaseAdapter(){
 // rest of the code

现在 returning 之前。在return categoryView之前,在其上设置onClickListener并将位置传递给listener

categoryView.setOnClickListener { 
            listener.onClick(position)
        }

现在在您的 MainActivity.kt 文件中实现 OnItemClickListener 并覆盖 OnClick 函数。

class MainActivity : AppCompatActivity() , NavigationView.OnNavigationItemSelectedListener, CategoryAdapter .OnItemClickListener {

     //rest of the code

   override fun onClick(position:Int){
       // perform the code.
   }


}

您需要将适配器构造函数更改为:

class CategoryAdapter(context:Context,categories:List<Category>, val listener: View.OnClickListener)

然后从您的 Activity 中这样调用它:

   adapter= CategoryAdapter(this,DataService.categories, object: View.OnClickListener{
        override fun onClick(v: View?) {
            Toast.makeText(context, "Click", Toast.LENGTH_LONG).show()
        }
    })

在您的适配器中,在 "categoryImage.setImageResource(resourceId)" 之后添加此代码:

categoryImage.setOnClickListener {
            listener.onClick(categoryImage)
        }

convertView.setOnClickListener(){}