Android 无法在使用Kotlin的网格视图中使用的卡片视图中单击任何内容(如按钮)
这是MainActivity.kt的代码Android 无法在使用Kotlin的网格视图中使用的卡片视图中单击任何内容(如按钮),android,kotlin,button,gridview,cardview,Android,Kotlin,Button,Gridview,Cardview,这是MainActivity.kt的代码 //Declarations lateinit var recyclerView: RecyclerView lateinit var gridView: GridView lateinit var gridAdapter: ShopListGridAdapter lateinit var recyclerAdapter: ShopListRecyclerViewAdapter val items = ArrayList<ShopList>
//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)
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
}
}
但这对我也不起作用。我无法单击任何项目上的任何内容。为什么按钮不可点击?当我触摸按钮时,它无法按下。
如果按钮修好了,我可以继续,但现在卡住了。提前感谢在mainActivityXML文件的以下部分中,您有两个基本上相互重叠的视图,几乎覆盖了整个屏幕
<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文件中都是不可见的,这意味着您将它们在代码中的某个地方变成可见的
现在我猜您在代码中同时打开了两个视图可见
,但没有为recyclerView分配适配器,这实际上意味着您在GridView
的顶部有一个空的(因此不可见)recycler视图
请检查一下。你能分享
活动\u main.xml
layout@Zain我已经更新了XML代码,请检查并共享完整的grid\u content\u item.XML
文件。按钮可能有重叠。我怀疑从主布局到行布局是否有重叠。。这一点无法从报告中得到证实layouts@Zain这就是为什么为两个视图共享整个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>
gridView.setOnItemClickListener { adapterView, view, pos, id ->
when(view.id) {
R.id.grid_item_status_btn -> Toast.makeText(...).show()
}
}
<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>