网格视图项不hightlight时preSS [英] Grid view item not hightlight when press
问题描述
我的应用程序有一个网格视图,在默认情况下,它的项目不突出时,我一下就可以了(我不知道为什么?)。我尝试将其添加列表中选择如下,但它不工作了,
My app has a grid view, By default, Its item does not highlight when I click on it (I don't know why? ). I try to add it a list selector as below, But it does not work too,
<GridView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@+id/load_more_process_id"
android:layout_alignParentTop="true"
android:drawSelectorOnTop="false"
android:listSelector="@drawable/grid_selector"
>
</GridView>
下面我选择:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@android:color/black"></item>
<item android:state_pressed="false" android:drawable="@android:color/white"></item>
推荐答案
如果您在列表项内容(或者每个网格项意见)一个聚焦元素,选择不绘制
If you have a focusable element in the list item contents (or the views for each grid item), the selector isn't drawn
例如,如果你的列表项的模板是:
For example, if the template for your list items is:
<LinearLayout android:orientation="horizontal" ... >
<CheckBox android:id="@+id/checkBox" ... />
<TextView android:id+"@+id/textView" ... />
</LinearLayout>
那么的ListView不会画一个选择器为每个ListView项,因为复选框,在默认情况下,可获得焦点。
then ListView will not draw a selector for each ListView item, because the CheckBox is, by default, focusable.
您可以提供在所有可聚焦元素的背景上每一个以选择状态发生变化的项目,或禁用焦点(这反过来要求你写一个童话看中适配器检查复选框时,选择状态的变化。
You can either provide a background on each item that changes with selection state, or disable focus on all focusable elements (which in turn requires you to write a fairy fancy adapter to check the checkboxes when selection state changes.
例如:
<CheckBox android:id="@+id/checkBox" android:focusable="false" ... />
会引起一个封闭的ListView重新开始绘制的选择。
will cause an enclosing ListView to start drawing the selector again.
有状态绘制的例子:
绘制/ my_list_selector_bg.xml:
drawable/my_list_selector_bg.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_background_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/list_background_focused"
android:state_focused="true" />
<item android:drawable="@android:color/transparent" />
</selector>
您再申请,要通过适配器返回的每个视图的背景:
You then apply that to the background of each view returned by your adapter:
<LinearLayout android:orientation="horizontal"
android:background="@drawable/my_list_selector_bg"
... >
无论哪种方法会奏效。
Either approach will work.
列表adpater类(GridView控件,ListView控件,和C)调用hasFocusable()由适配器返回的每个观点,并禁止选择绘图如果hasFocusable()返回true。幸运的是,他们还复制pssed /有效状态到目前的工作重点或所选适配器项目选择/对焦/ $ P $,所以你可以自己绘制它,如果你想要的。
The list adpater classes (GridView, ListView, &c) call hasFocusable() on each view returned by the adapter, and disable selection drawing if hasFocusable() returns true. Fortunately, they also replicate the selection/focus/pressed/active state to the currently focused or selected adapter item, so you can draw it yourself if you want.
这篇关于网格视图项不hightlight时preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!