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

如何解决无法使用 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 的顶部。

请检查一下。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res