ListView Android不允许选择器工作,也不允许选择

时间:2011-03-31 14:07:22

标签: android listview highlight simplecursoradapter focusable

好吧我知道这可能听起来像关于ListView和ListActivity问题的任何其他问题但是这不是,我会发疯,因为我在过去一周继续搜索所有帖子找到任何解决方案这可能对我有所帮助。已经尝试ListView item background via custom selectorGetView Vs. BindView in a custom CursorAdapter?所有其他可能的博客网站...

好的问题是,我的listView for one 1不允许选择/火灾事件,2。点击项目时不会更改行突出显示。我有一个标准TestActivity延长ListActivity,我已实施自定义TestCursorAdapter extends SimpleCursorAdapter

现在我已经在test_list_view.xml和第二个row_view.xml的XML方面了。现在我按照上面提到的答案并将selector实现为row_selector.xml。但问题是一行不接受任何点击或焦点事件。我实施了一个基本的吐司,以表明是否有任何事情发生

@Override
protected void onListItemClick(ListView l, View v , int postion, long idontnow){
    Toast.makeText(getApplicationContext(), "Hello World",  Toast.LENGTH_SHORT ).show();
}

test_list_view.xml

的一部分
<ListView android:id="@android:id/list" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true" 
    android:cacheColorHint="#00000000"
    android:focusable="true" 
    android:listSelector="@color/row_selector" 
    android:background="@color/Transparent"/>

selector.xml的一部分(基本上是从上面提到的链接中复制并粘贴的)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" 
        android:drawable="@color/Transparent" />
    <item 
        android:state_focused="true"
        android:state_enabled="false"
        android:state_pressed="true" 
        android:drawable="@color/green" />
    <item 
        android:state_focused="true"
        android:state_enabled="false"      
        android:drawable="@color/green" />
    <item 
        android:state_focused="true"
        android:state_pressed="true" 
        android:drawable="@color/blue" />
    <item 
        android:state_focused="false"
        android:state_pressed="true" 
        android:drawable="@color/blue" />
    <item 
        android:state_focused="true" 
        android:drawable="@color/green" />
    <item 
        android:state_selected="true" 
        android:drawable="@color/blue" />
    <item 
        android:drawable="@color/Transparent" />
</selector>

我遵循了我所知道的所有选项,但仍然没有。 唯一的主要区别是我没有使用getView,而是使用bindView

任何帮助人员都将受到高度赞赏。

提前感谢。

Sanj

2 个答案:

答案 0 :(得分:1)

我建议您从android:focusable="true"删除ListView

此外,您可以使用selector作为ListView中显示的项目的背景颜色。另外,请确保这些元素未调用onClickonTouch或类似内容,并让ListView处理点击事件。

我希望这会有所帮助

修改

选择项目使用

的问题
lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            yourMethod();
        }
    });

答案 1 :(得分:1)

遵循raukodraug的建议和帖子 ListView OnItemClickListener Not Responding?

提到的另一个事实是使用bindView方法,我使用了

view.setClickable(true);
view.setFocusable(true); 

只有当您点击标有黄色突出显示的区域和ImageView时,项目才会点击作品类型。我应该能够理解这一点,谢谢,

enter image description here

<强>被修改

哦*%#$解决了它,需要设置RelativeLayouts Add State from Children = true;