简体   繁体   English

带有 OnItemClickListener 的 ListView

[英]ListView with OnItemClickListener

I am using a custom ListView with RatingBar and ImageButton .我正在使用带有RatingBarImageButton的自定义ListView Here is my problem: When I click on my ListView , my OnItemClickListener is not working.这是我的问题:当我点击我的ListView ,我的OnItemClickListener不工作。 Please can any one help me.请任何人都可以帮助我。 Code:代码:

ListView lv = getListView();
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});

Though a very old question, but I am still posting an answer to it so that it may help some one.虽然是一个非常古老的问题,但我仍然发布了一个答案,以便它可以帮助某人。 If you are using any layout inside the list view then use ...如果您在列表视图中使用任何布局,请使用 ...

android:descendantFocusability="blocksDescendants"    

... on the first parent layout inside the list. ... 在列表中的第一个父布局上。 This works as magic the click will not be consumed by any element inside the list but will directly go to the list item.这很神奇,单击不会被列表内的任何元素消耗,而是直接转到列表项。

I have an Activity that extends ListActivity.我有一个扩展 ListActivity 的活动。

I tried doing something like this in onCreate:我尝试在 onCreate 中做这样的事情:

ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        Log.i("Hello!", "Y u no see me?");

    }

});

But that didn't work.但这没有用。

Instead I simply needed to override onListItemClick:相反,我只需要覆盖 onListItemClick:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    Log.i("Hello!", "Clicked! YAY!");

}

Hey check this, works for me... hope it work for u too嘿检查这个,对我有用......希望它也对你有用

If list item contains ImageButton如果列表项包含ImageButton

Problem: OnItemClickListener just doesn't repond any at all!问题: OnItemClickListener根本不OnItemClickListener

Reason: No idea理由:不知道

Solution: in code, set ImageButton 's focus to “false”解决方法:在代码中,将ImageButton的焦点设置为“false”

1: ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton); 1: ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton);

2: button.setFocusable(false); 2: button.setFocusable(false);

If you want to enable item click in list view use如果要在列表视图中启用项目单击使用

listitem.setClickable(false);

this may seem wrong at first glance but it works!乍一看这似乎是错误的,但它确实有效!

if list item view contains button or checkbox or imagebutton, the onitemclicklistener and onitemlongclicklistener not working due to it has own onclick listener.如果列表项视图包含按钮或复选框或图像按钮,则 onitemclicklistener 和 onitemlongclicklistener 不工作,因为它有自己的 onclick 侦听器。

set focusable as false将可聚焦设置为 false

holder.button.setFocusable(false);

1) Check if you are using OnItemClickListener or OnClickListener (which is not supported for ListView) 1) 检查您是否使用 OnItemClickListener 或 OnClickListener(ListView 不支持)
Documentation Android Developers ListView 文档 Android 开发人员 ListView

2) Check if you added Listener to your ListView properly. 2) 检查您是否将 Listener 正确添加到 ListView。 It's hooked on ListView not on ListAdapter!它是在 ListView 上而不是在 ListAdapter 上!

ListView.setOnItemClickListener(listener);

3) If you need to use OnClickListener, check if you do use DialogInterface.OnClickListener or View.OnClickListener (they can be easily exchanged if not validated or if using both of them) 3) 如果您需要使用 OnClickListener,请检查您是否使用DialogInterface.OnClickListenerView.OnClickListener (如果未验证或同时使用它们,它们可以轻松交换)

listPaired = (ListView) findViewById( R.id.listView1 );
listPairedData = new ArrayList < String >();
araPaired = new ArrayAdapter( this, android.R.layout.simple_list_item_1, listPairedData );
listPaired.setAdapter( araPaired );
listPaired.setOnItemClickListener( listPairedClickItem );

private OnItemClickListener listPairedClickItem = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView < ? > arg0, View arg1, int arg2, long arg3) {

        String info = ( (TextView) arg1 ).getText().toString();
        Toast.makeText( getBaseContext(), "Item " + info, Toast.LENGTH_LONG ).show();
    }
};

You can also use lambda .您也可以使用lambda Lambda syntax is not supported under Java 1.7 or earlier JVMs. Java 1.7 或更早版本的 JVM 不支持 Lambda 语法。

listView.setOnItemClickListener((parent, view, position, id) -> {
    ...
});

如果您以编程方式定义 ListView:

mListView.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);

setClickable as false to ImageButton like this像这样将ImageButton设置为false

imagebutton.setClickable(false);

and then perform OnItemClickListener to listview.然后对列表视图执行OnItemClickListener

In Java as other suggest在 Java 中作为其他建议

listitem.setClickable(false);

Or in xml:或者在 xml 中:

android:clickable="false"

It works very fine它工作得很好

Just insert the line into RatingBar:只需将行插入 RatingBar:

android:isIndicator="true"

In XML the ratingbar look like this.在 XML 中,评级栏看起来像这样。

<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dp"
    android:isIndicator="true"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false" />
lv.setOnItemClickListener( new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView < ? > parent, View view,
                            int position, long id) {
        // TODO Auto-generated method stub

    }
} );

Android OnItemClickLIstener conflicts with the OnClickListener of items of row of listview in Adapter. Android OnItemClickLIstener 与Adapter 中listview 行的items 的OnClickListener 冲突。 You just have to make sure your code is well managed and properly written with standards.您只需要确保您的代码得到良好管理并按照标准正确编写。

Check the answer in the link given below:检查下面给出的链接中的答案:

Make list clickable 使列表可点击

Asked by many, The childs in list must not have width "match_parent" if you are looking for listview click only.许多人问,如果您只查找列表视图点击,列表中的孩子不能有宽度“match_parent”。

Even if you set the "Focusable" to false it wont work.即使您将“Focusable”设置为 false,它也不会起作用。 Set the child's Width to wrap_content将孩子的宽度设置为 wrap_content

<TextView
    android:id="@+id/itemchild"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...

Is there and image in the list view that you are using> then follow the link: http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/您正在使用的列表视图中是否有图像> 然后点击链接: http : //vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-多选/

I think when you work out on the link that I have provided first every thing will work fine, I have tried that.我认为当你在我首先提供的链接上工作时,一切都会正常工作,我已经尝试过了。 If you want a refined answer please elaborate the question with code and description.如果你想要一个精致的答案,请用代码和描述详细说明问题。

The most simple solution and the easiest way I can provide is this.我能提供的最简单的解决方案和最简单的方法就是这个。 You just need to do these two steps.你只需要完成这两个步骤。

1.add this in your XML file. 1. 在您的 XML 文件中添加此内容。

 <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:id="@+id/list_view" android:layout_below="@+id/info_text" />

  1. Add this in your MainActivity file.将此添加到您的 MainActivity 文件中。

 ListView listView; listView = findViewById(R.id.list_view); String[] fruits = new String[5]; fruits[0]="hello"; fruits[1]="hello"; fruits[2]="hello"; fruits[3]="hello"; fruits[4]="hello"; List newList = new ArrayList(Arrays.asList(fruits)); ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, newList); listView.setAdapter(arrayAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { String selectedItem = (String) parent.getItemAtPosition(position); Toast.makeText(DisplayPannel.this, selectedItem, Toast.LENGTH_SHORT).show(); } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM