无法在使用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>

网格视图项目

尝试过其他方法

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

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

也就是说,在我的项目中,一次只能看到一个视图。循环视图中的按钮是可点击的,一切都很好,但在网格视图中不是。

在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的顶部有一个空的(因此不可见(recyclerView。

请检查一下。

最新更新