热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ListViewOnItemClickListener并不总是触发-ListViewOnItemClickListenerisnotalwaysfiring

ItseemslikemyListViewOnItemClickListenerdoesnotalwaysseemtobecalled.TheListViewusese

It seems like my ListView OnItemClickListener does not always seem to be called. The ListView uses embedded TextViews.

似乎我的ListView OnItemClickListener似乎并不总是被调用。 ListView使用嵌入式TextView。

If you click at the top or bottom 3px of each item then the listener does fire.

如果单击每个项目的顶部或底部3px,则侦听器会触发。

If you click on the text of the TextView then the listener does not fire as though it is consuming/blocking the click.

如果单击TextView的文本,则侦听器不会像消耗/阻止单击一样触发。

I have tried many combinations of adding focusable="false", clickable="false" and focusableInTouchMode="false" to the TextView and adding android:descendantFocusability="beforeDescendants" to the root view.

我尝试了很多组合,将focusable =“false”,clickable =“false”和focusableInTouchMode =“false”添加到TextView,并将android:descendantFocusability =“beforeDescendants”添加到根视图中。

activity_local_explorer.xml









Activity

public class LocalExplorer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_explorer);

    String[] values = { "Hello", "My", "Friend"};

    ListView listView = (ListView) findViewById(R.id.local_file_view);
    listView.setAdapter(new ArrayAdapter<>(this, R.layout.activity_local_explorer, R.id.local_file_view_text, values));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView adapterView, View view, int position, long id) {

            String selectedValue = getItemAtPosition(position);
            Snackbar.make(listView, selectedValue, 2).show();
        }    
    });
}

Any help would be much appreciated as I have been spending hours on this simple activity!

任何帮助都会非常感激,因为我花了几个小时来完成这个简单的活动!

1 个解决方案

#1


0  

First I'd like to thank those who tried to help me answer my question, your time was much appreciated.

首先,我要感谢那些试图帮助我回答我的问题的人,非常感谢您的时间。

OK I got this working at long last by slightly changing the approach. I moved the TextView out of the activity_local_explorer.xml into its own xml layout file and used a different constructor when creating the adapter.

好的,我通过稍微改变方法,最终得到了这个工作。我将TextView从activity_local_explorer.xml移到了自己的xml布局文件中,并在创建适配器时使用了不同的构造函数。

activity_local_explorer.xml




    


local_explorer_row.xml



Activity

public class LocalExplorer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_explorer);

    String[] values = { "Hello", "My", "Friend"};

    ListView listView = (ListView) findViewById(R.id.local_file_view);
    listView.setAdapter(new ArrayAdapter<>(this, R.layout.local_explorer_row, values));
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView adapterView, View view, int position, long id) {

            String selectedValue = getItemAtPosition(position);
            Snackbar.make(listView, selectedValue, 2).show();
        }    
    });
}

推荐阅读
author-avatar
mobiledu2502875577
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有