【问题标题】:Android - custom listview and highlightAndroid - 自定义列表视图和突出显示
【发布时间】: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


    【解决方案1】:

    您需要将“myselector.xml”放在drawable文件夹中。

        android:listSelector="@xml/myselector" 
    

    这一行应该是

        android:listSelector="@drawable/myselector"
    

    【讨论】:

    • 我试过了,但没用...为什么可绘制文件夹不正确地存储我的自定义选择器?
    • 您应该将 myselector.xml 放在您的可绘制文件夹中。然后使用上面的代码就可以了。
    • 我试过了,但它不起作用......我认为对可点击区域进行了修改:仅在某些点上它会突出显示整个项目,但如果我在行上按“随便” ,它不起作用(除了在某些方面:我特别注意到一个项目和另一个项目之间的边界行)
    • 它也适用于我的 xml 文件夹中的 myselector.xml 文件,这对我的项目来说不是问题。如果我从视图中排除按钮,它可以很好地与所有 Textviews 配合使用,你想要多少都没关系。但是如果我插入一个按钮,所有的视图控件都会改变,它不再突出显示。我认为问题出在按钮的视图属性内,但我不知道是哪一个导致了问题。有什么建议吗?
    • 将您的可绘制选择器更改为颜色选择器。您正在使用颜色资源作为可绘制资源。
    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2012-06-10
    • 2011-08-28
    • 1970-01-01
    • 2012-11-21
    • 2012-06-02
    • 2019-09-03
    相关资源
    最近更新 更多