Android - 自定义列表视图和突出显示

时间:2016-10-13 15:02:25

标签: android listview customization

我已经尝试了许多工作,但没有取得任何成功。

我已经实现了一个自定义列表视图,显示3个文本视图,添加了我手中的自定义适配器和工作正常,我已经完成了registerForContextMenu(列表视图的视图),当我按下显示的项目时,它显示了一个完美的我的项目周围有蓝色突出显示,当我按下它时会发生相同的情况,然后它会显示菜单。好的。然后我在自定义列表视图中添加了一个按钮,如果发生某些事情则显示一种颜色,显示另一个反之亦然。在我修改我的自定义适配器以包含我的按钮并设置更改颜色逻辑后,如果我长按我的项目,我没有更多突出显示,但保留上下文菜单。

为什么会这样?

我在Stack和Google上尝试了很多搜索,但我找到的每个解决方案都不适用于我的应用。我也尝试插入自定义选择器,当我从listview设计中排除按钮时它工作正常,我认为按钮是罪魁祸首,但我无法找到解决此问题的方法。我想是与"背景"相关的事情。 - " drawable"按钮,但我在这里向你求助。

提前致谢。

一些代码,如果您感兴趣的话:

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>

谢谢!

1 个答案:

答案 0 :(得分:1)

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

    android:listSelector="@xml/myselector" 

这一行应该是

    android:listSelector="@drawable/myselector"
相关问题