【发布时间】:2016-10-13 15:02:24
【问题描述】:
我已经尝试了很多以使事情正常进行,但没有任何成功。
我已经实现了一个显示 3 个文本视图的自定义列表视图,添加了我手的自定义适配器并且一切正常,我已经完成了 registerForContextMenu(列表视图的视图),当我按下显示的项目时,它显示一个完美的蓝色突出显示在我的项目周围,当我长按它时,会发生同样的情况,然后它会向我显示菜单。好的。然后我在我的自定义列表视图中添加了一个按钮,如果发生某些事情,则显示一种颜色,反之亦然显示另一种颜色。在我修改了我的自定义适配器以包含我的按钮并设置更改颜色逻辑后,如果我长按我的项目,我周围不再有突出显示,但上下文菜单被保留。
为什么会这样?
我在 Stack 和 Google 上尝试了很多搜索,但我找到的每个解决方案都不适用于我的应用程序。我还尝试插入一个自定义选择器,当我从列表视图设计中排除按钮时它工作正常,我想按钮是罪魁祸首,但我找不到解决这个问题的方法。我想这与按钮的“背景”-“可绘制”有关,但我在这里向您寻求帮助。
提前致谢。
一些代码,如果你感兴趣的话:
listviewdesign.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:gravity="center"
/>
<TextView
android:id="@+id/text2"
android:singleLine="false"
android:maxEms="8"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_toLeftOf="@+id/text3"
android:layout_toRightOf="@+id/text1"
android:layout_centerVertical="true"
android:gravity="center"
/>
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/btn1"
/>
<Button
android:id="@+id/btn1"
android:layout_width="10px"
android:layout_height="10px"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
/>
</RelativeLayout>
mainactivitylayout.xml:
<LinearLayout ...>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
(when I add selector: android:listSelector="@xml/myselector")
android:clickable="false"
/>
</LinearLayout>
以及我的完整性选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
class="class of project">
<item android:state_pressed="true"
android:drawable="@drawable/pressed"/>
<item android:state_focused="true"
android:drawable="@drawable/focused"/>
<item android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/normal"/>
<item android:drawable="@drawable/normal"/>
</selector>
正确设置这些可绘制项目的位置,例如:
<resources>
<drawable name="pressed">#FF00A5FF</drawable>
<drawable name="focused">#FF0000FF</drawable>
<drawable name="normal">#00000000</drawable>
</resources>
谢谢!
【问题讨论】:
标签: android listview customization