无法使用 Kotlin 在网格视图中使用的卡片视图中单击任何内容如按钮

问题描述

这是MainActivity.kt的代码

//Declarations
lateinit var recyclerView: RecyclerView
lateinit var gridView: GridView
lateinit var gridAdapter: ShopListGridAdapter
lateinit var recyclerAdapter: ShopListRecyclerViewAdapter

val items = ArrayList<ShopList>()

//Initialization in onCreate() method
recyclerView = findViewById(R.id.recycler_view)
gridView = findViewById(R.id.grid_view)
recyclerAdapter = ShopListRecyclerViewAdapter(applicationContext,items)
gridAdapter = ShopListGridAdapter(applicationContext,items)

//After adding some items in **items array list**
gridView.adapter = gridAdapter
recyclerView.adapter = recyclerAdapter
recyclerView.layoutManager = linearlayoutmanager(this@MainActivity)

这是 ShopListGridAdapter.kt 的代码

class ShopListGridAdapter(var context: Context,var itemList: ArrayList<ShopList>) : BaseAdapter() {

    override fun getCount(): Int = itemList.size
    override fun getItem(position: Int): Any = itemList[position]
    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int,convertView: View?,parent: ViewGroup?): View {

        val view: View = View.inflate(context,R.layout.grid_content_item,null)

        ...
        val itemStatusBtn: Button = view.findViewById(R.id.grid_item_status_btn)

        val item: ShopList = itemList[position]
        ...

        itemStatusBtn.setonClickListener {
            //do stuff
            ...
        }

        return view
    }
}

这里是网格视图的 XML 代码 (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_wish_list_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:orientation="horizontal"
            android:paddingVertical="10dp"
            android:paddingHorizontal="20dp"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/nav_drawer_opener_iv"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:src="@drawable/nav_bar_icon"
                android:contentDescription="@string/nav_bar_icon"/>

            <TextView
                android:id="@+id/top_bar_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/app_name"
                android:textStyle="bold"
                android:textSize="22sp"
                android:textAlignment="center"
                android:textColor="@color/text_color"/>

            <ImageView
                android:id="@+id/three_dots_menu_btn"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/more_dots"
                app:tint="@color/text_color"
                android:onClick="openThreeDotsMenu"
                android:contentDescription="@string/change_the_layout"/>

        </LinearLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@drawable/top_curve_rectangle"
            android:paddingTop="25dp">

            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/grid_view_swipe_refresh"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <GridView
                    android:id="@+id/grid_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal"
                    android:paddingBottom="100dp"
                    android:horizontalSpacing="10dp"
                    android:verticalSpacing="10dp"
                    android:numColumns="2"
                    android:visibility="invisible"/>

            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/recycler_view_swipe_refresh"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingBottom="100dp"
                    android:clipToPadding="false"
                    android:visibility="invisible"/>

            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

            <ProgressBar
                android:id="@+id/progress_bar_loading"
                android:layout_width="50dp"
                android:layout_height="50dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@id/add_item_activity_btn"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:visibility="visible"/>

            <LinearLayout
                android:id="@+id/empty_indicator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/add_item_activity_btn"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:visibility="invisible"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/shopping_bag"
                    android:contentDescription="@string/empty_shopping_bag"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/your_list_is_empty"
                    android:textSize="30sp"
                    android:textColor="@color/green_color_1"
                    android:fontFamily="@font/roboto_medium"
                    android:textAlignment="center"/>

            </LinearLayout>

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/add_item_activity_btn"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginVertical="20dp"
                android:contentDescription="@string/add_item"
                android:src="@drawable/add_icon"
                app:borderWidth="0dp"
                app:maxImageSize="40dp"
                app:tint="@color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:onClick="openAddItemActivity"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:itemIconTint="@color/green_color_1"
        app:itemTextColor="#D93B4043"
        app:itemIconPadding="20dp"
        app:menu="@menu/nav_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

这里是网格项的代码(grid_content_item.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:paddingHorizontal="25dp"
    android:paddingVertical="15dp"
    android:gravity="center_horizontal">

        <TextView
            android:id="@+id/item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/item_name"
            android:textAlignment="center"
            android:textColor="#3FC6BA"
            android:textSize="20sp"
            android:fontFamily="@font/roboto_bold"/>

        <TextView
            android:id="@+id/quantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginVertical="5dp"
            android:text="@string/quantity"
            android:textSize="12sp"
            android:fontFamily="@font/roboto_bold"
            android:textAlignment="center" />

        <TextView
            android:id="@+id/store_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/buy_from_anywhere"
            android:textSize="12sp"
            android:textAlignment="center"
            android:fontFamily="@font/roboto_medium"/>

        <Button
            android:id="@+id/grid_item_status_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:backgroundTint="@color/pending_color"
            android:fontFamily="@font/roboto_bold"
            android:text="@string/pending"
            android:textAllCaps="false"
            android:minHeight="0dip"
            android:textColor="@color/white" />

        <LinearLayout
            android:id="@+id/grid_speak_item_ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:clickable="true"
            android:focusable="true"
            android:background="?android:attr/selectableItemBackground">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/speak_out_icon"
                app:tint="#AB0C5FE8"
                android:layout_marginEnd="5dp"
                android:contentDescription="@string/speak_the_item"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/listen"
                android:fontFamily="@font/roboto_bold"
                android:textColor="#AB0C5FE8"/>

        </LinearLayout>
    </LinearLayout>

Grid View Item

尝试过其他方法

我也在 MainActivity.kt 的 onCreate() 函数中尝试过这种方法

gridView.setonItemClickListener { adapterView,view,pos,id ->
      when(view.id) {
           R.id.grid_item_status_btn -> Toast.makeText(...).show()
      }
}

但它也不适合我。我无法单击任何项​​目上的任何内容。为什么按钮不可点击?当我触摸按钮时,它是不可按下的。 如果按钮被修复,那么我可以继续,但现在卡住了。提前谢谢?

代码中更改视图布局

在 onCreate() 方法

sharedPref = getSharedPreferences("settings",MODE_PRIVATE)
if(sharedPref.getInt("view_mode",0) == 1) gridView.visibility = ViewGroup.VISIBLE
else recyclerView.visibility = ViewGroup.VISIBLE

添加一个菜单,从中可以更改视图布局:

when (it.itemId) {
      R.id.change_to_grid_view -> changeViewLayout(gridView)
      R.id.change_to_recycler_view -> changeViewLayout(recyclerView)
      ...
}

这是处理可见性的函数

    private fun changeViewLayout(view: View) {
        if(view == gridView) {
            gridView.visibility = ViewGroup.VISIBLE
            recyclerView.visibility = ViewGroup.INVISIBLE
            gridAdapter.notifyDataSetChanged()
        } else {
            recyclerView.visibility = ViewGroup.VISIBLE
            gridView.visibility = ViewGroup.INVISIBLE
            recyclerAdapter.notifyDataSetChanged()
        }
    }

意味着,在我的项目中,一次只能看到一个视图。 Recycler View 中的按钮可点击并且一切正常,但在 Grid View 中则不然。

解决方法

在 mainActivity xml 文件的以下部分,您有两个视图,它们基本上彼此重叠,覆盖(几乎)整个屏幕。

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/grid_view_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:paddingBottom="100dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:numColumns="2"
        android:visibility="invisible"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/recycler_view_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="100dp"
        android:clipToPadding="false"
        android:visibility="invisible"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

由于它们都在 xml 文件中不可见,这意味着您将它们在代码中的某处可见。

现在我的猜测是您在代码中同时打开两个视图 Visible 但您没有为 recyclerView 分配适配器,这实际上意味着您有一个空的(因此不可见的)回收器视图GridView 的顶部。

请检查一下。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...