【问题标题】:Listview with edittext inside scrollview is not clickable滚动视图内带有edittext的Listview不可点击
【发布时间】:2025-12-25 15:20:08
【问题描述】:

我有一个带有edittext作为滚动视图内的行项目的列表视图。我在列表视图上使用了触摸侦听器,因此我可以滚动滚动视图内的列表。但无法调用onItemClickListenerOnItemSelectedListener。 我试过 android:windowSoftInputMode="adjustPan"

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <RelativeLayout
                android:id="@+id/rlHamburger"
                android:layout_width="50dp"
                android:layout_height="match_parent" >

                <Button
                    android:id="@+id/btnHamburger"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_centerInParent="true"
                    android:background="@drawable/images" />
            </RelativeLayout>

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/rlHamburger" >

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <RelativeLayout
                        android:id="@+id/rlTitle"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:layout_margin="5dp"
                        android:background="@drawable/relative_background" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:layout_margin="10dp"
                            android:text="Add Quote"
                            android:textColor="@color/black"
                            android:textSize="20sp"
                            android:textStyle="bold" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/rlButtons"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:layout_below="@id/rlTitle"
                        android:layout_margin="5dp"
                        android:orientation="horizontal" >

                        <Button
                            android:id="@+id/btnBack"
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:layout_alignParentLeft="true"
                            android:layout_gravity="center_vertical"
                            android:layout_margin="5dp"
                            android:layout_weight="1"
                            android:background="@drawable/button"
                            android:gravity="center"
                            android:text="Back"
                            android:textColor="@drawable/text_color" />

                        <Button
                            android:id="@+id/btnAddPhoto"
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:layout_gravity="center_vertical"
                            android:layout_margin="5dp"
                            android:layout_toLeftOf="@+id/btnAddNote"
                            android:layout_weight="1"
                            android:background="@drawable/button"
                            android:gravity="center"
                            android:text="Add Photo"
                            android:textColor="@drawable/text_color" />

                        <Button
                            android:id="@+id/btnAddNote"
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:layout_gravity="center_vertical"
                            android:layout_margin="5dp"
                            android:layout_toLeftOf="@+id/btnSave"
                            android:layout_weight="1"
                            android:background="@drawable/button"
                            android:gravity="center"
                            android:text="Add Note"
                            android:textColor="@drawable/text_color" />

                        <Button
                            android:id="@+id/btnSave"
                            android:layout_width="wrap_content"
                            android:layout_height="40dp"
                            android:layout_alignParentRight="true"
                            android:layout_gravity="center_vertical"
                            android:layout_margin="5dp"
                            android:background="@drawable/button"
                            android:gravity="center"
                            android:text="Save"
                            android:textColor="@drawable/text_color" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/rlInformation"
                        android:layout_width="match_parent"
                        android:layout_height="180dp"
                        android:layout_below="@id/rlButtons"
                        android:layout_margin="5dp"
                        android:background="@drawable/relative_background"
                        android:orientation="horizontal"
                        android:padding="5dp" >

                        <RelativeLayout
                            android:id="@+id/rlInfo1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="2dp" >

                            <TextView
                                android:id="@+id/title"
                                android:layout_width="120dp"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:text="Title" />

                            <EditText
                                android:id="@+id/tvTitle"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_toRightOf="@id/title"
                                android:background="@drawable/relative_background"
                                android:hint="Title"
                                android:padding="5dp" />
                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/rlInfo2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/rlInfo1"
                            android:layout_margin="2dp" >

                            <TextView
                                android:id="@+id/owner"
                                android:layout_width="120dp"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:text="Owner" />

                            <EditText
                                android:id="@+id/tvOwner"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_toRightOf="@id/owner"
                                android:background="@drawable/relative_background"
                                android:hint="Owner"
                                android:padding="5dp" />
                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/rlInfo3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/rlInfo2"
                            android:layout_margin="2dp" >

                            <TextView
                                android:id="@+id/address"
                                android:layout_width="120dp"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:text="Address" />

                            <EditText
                                android:id="@+id/tvAddress"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_toRightOf="@id/address"
                                android:background="@drawable/relative_background"
                                android:hint="Address"
                                android:padding="5dp" />
                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/rlInfo4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/rlInfo3"
                            android:layout_margin="2dp" >

                            <TextView
                                android:id="@+id/status"
                                android:layout_width="120dp"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:text="Status" />

                            <TextView
                                android:id="@+id/tvStatus"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toRightOf="@id/status"
                                android:text="Status" />
                        </RelativeLayout>

                        <RelativeLayout
                            android:id="@+id/rlInfo5"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/rlInfo4"
                            android:layout_margin="2dp" >

                            <TextView
                                android:id="@+id/description"
                                android:layout_width="120dp"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:text="Description" />

                            <EditText
                                android:id="@+id/tvDescription"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_toRightOf="@id/description"
                                android:background="@drawable/relative_background"
                                android:hint="Description"
                                android:padding="5dp" />
                        </RelativeLayout>
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/rlList"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/rlInformation"
                        android:layout_margin="10dp" >

                        <TextView
                            android:id="@+id/tvProductName"
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:text="Product Name" />

                        <TextView
                            android:id="@+id/tvShortDescription"
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:layout_toRightOf="@id/tvProductName"
                            android:gravity="center"
                            android:text="Short Description" />

                        <TextView
                            android:id="@+id/tvQuantity"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:layout_toRightOf="@id/tvShortDescription"
                            android:gravity="center"
                            android:text="Qty" />

                        <TextView
                            android:id="@+id/tvUnits"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:layout_toRightOf="@id/tvQuantity"
                            android:gravity="center"
                            android:text="units" />

                        <TextView
                            android:id="@+id/tvPrice"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:layout_toRightOf="@id/tvUnits"
                            android:gravity="center"
                            android:text="Price" />

                        <TextView
                            android:id="@+id/tvTotal"
                            android:layout_width="200dp"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:gravity="center"
                            android:text="Total" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/rlList1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/rlList"
                        android:layout_margin="10dp" >

                        <ListView
                            android:id="@+id/lvItems"
                            android:layout_width="match_parent"
                            android:layout_height="300dp" >
                        </ListView>

                        <Button
                            android:id="@+id/btnAddItem"
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/lvItems"
                            android:background="@drawable/button"
                            android:text="Add item"
                            android:textColor="@drawable/text_color" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/rlList1"
                        android:layout_margin="20dp" >

                        <TextView
                            android:id="@+id/tvTotalEstimation"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:gravity="center"
                            android:text="Total estimation"
                            android:textSize="20sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/tvEstimationValue"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_below="@id/tvTotalEstimation"
                            android:gravity="center"
                            android:text="$0.0"
                            android:textSize="20sp"
                            android:textStyle="bold" />
                    </RelativeLayout>
                </RelativeLayout>
            </ScrollView>
        </RelativeLayout>

这是我的布局截图:

请帮我解决一下。谢谢。

【问题讨论】:

  • adjustPan 是自动停止让键盘。

标签: android listview android-listview android-edittext android-scrollview


【解决方案1】:

转到您的清单文件并使用您的类的活动标签添加这些步骤

android:configChanges="keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"

【讨论】:

  • 感谢 Anshul 的解决方案,但我的问题是我无法让任何 onItemClick 或 OnItemSelected 侦听器在我的列表项上工作。这可能是因为我在滚动视图中使用列表视图以及以 Edittext 作为行项。请为我提供上述问题的解决方案。
  • ScrollView 内部的 ListView 没有任何解决方案。但这并不意味着你不能使用OnItemClickListener