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

发布于 2024-10-28 09:20:14 字数 2647 浏览 5 评论 0原文

好吧,我知道这听起来可能像有关 ListView 和 ListActivity 问题的任何其他问题,但这不是,我会发疯的,因为我在过去的一周中一直在搜索所有帖子以找到可能有帮助的解决方案我。已经尝试过通过自定义选择器的ListView项目背景GetView 与。自定义 CursorAdapter 中的 BindView? 所有其他可能的博客站点...

好吧,问题是,我的 listView 1 不允许选择/触发事件,2. 单击某个项目时不会更改行突出显示。我有一个标准的TestActivity扩展ListActivity并且我已经实现了一个自定义TestCursorAdapter扩展SimpleCursorAdapter

现在在我在test_list_view上的XML方面.xml 和第二个 row_view.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();
}

一部分

<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"/>

我实现了一个基本的 toast 来指示selecter.xml 的 test_list_view.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

任何帮助人员将不胜感激。

提前致谢。

桑吉

Okay I know this may sound like any other issues regarding the ListView and ListActivity issues but this is not, I am going to go crazy as I have in the past week gone on and on searching all post to find any solution to this that may help me. Already tried ListView item background via custom selector and GetView Vs. BindView in a custom CursorAdapter? every other possible blogs sites...

Okay the issue, My listView for one 1 will not allows selection/fire events and 2. will not change the row highlight when an item is clicked. I have a standard TestActivity extending ListActivity and I have implemented a Custom TestCursorAdapter extends SimpleCursorAdapter

Now in XML side of things I have on test_list_view.xml and second one row_view.xml Now I followed the above mentions answers and implement selector as row_selector.xml. But the issue is for one the rows does not accept any click or focus events. I implemented a basic toast to indicate if there is anything happening

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

part of 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"/>

part of selector.xml (basically copied and pasted from one of the above mentioned links)

<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>

I followed all options that I am aware of and still nothing. The only major difference is guys I am not using getView instead I am using bindView.

Any help guys will be highly appreciated.

thanks in advance.

Sanj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2024-11-04 09:20:14

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

此外,您可以使用选择器作为ListView中显示的项目的背景颜色。还要确保这些元素没有调用 onClickonTouch 或类似的东西,并让 ListView 处理点击事件。

我希望这有助于

编辑:

解决您选择项目使用的问题

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

I suggest that you remove the android:focusable="true" from the ListView.

Additionally, you can use the selector as the background color of the items that are displayed in the ListView. Also be sure that those elements are not calling onClick or onTouch or something like that, and let the ListView handle the click events.

I hope this helps

EDIT:

for your problem with selecting the item use

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            yourMethod();
        }
    });
前事休说 2024-11-04 09:20:14

遵循 raukodraug 的建议和帖子
ListView OnItemClickListener 没有响应?

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

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

仅当您单击以黄色突出显示的区域和 ImageView 时, 项目单击类型的作品。我应该能够弄清楚这一点,谢谢,

在此处输入图像描述

已编辑

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

following raukodraug's suggestion and post
ListView OnItemClickListener Not Responding?

Another fact as mentioned use bindView method, I used

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

the item click kind of works, only if you click on the region marked highlighted in Yellow and the ImageView. I should be able to figure this out thanks,

enter image description here

Edited

Oh *%#$ Solved it, Needed to set the RelativeLayouts Add State from Children = true;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文