ListView items are not clickable. why?

北城余情 提交于 2019-11-26 22:44:09

try this to get the focus: View.getFocus();

Android doesn't allow to select list items that have focusable elements (buttons). Modify the button's xml attribute to:

android:focusable="false"

It should still be clickable, just won't gain focus...

c0de

I had the same issue with a ListView which contained only a RadioButton:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RadioButton
    android:id="@+id/userNameRadioButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

I used the RadioButton in each row to display the default item selection, to make ListView handle clicks I had to use:

radioButton.setFocusableInTouchMode(false);
radioButton.setFocusable(false);

or in the XML-file:

android:focusable="false" 
android:focusableInTouchMode="false"

So it is a focus related issue... With the above modifiers the focus is directed to ListView on click.

fpr0001

This answer here worked for me: https://stackoverflow.com/a/16536355/5112161

Mainly he ADDED in the LinearLayout or RelativeLayout the following:

android:descendantFocusability="blocksDescendants"

You also need to REMOVE from all your xml the following:

android:focusable="false"
android:focusable="true"
android:clickable="true"
android:clickable="false"

Consider making the text value selectable by specifying android:textIsSelectable="true"

Don't listen to Google. In the rows' layout, set

textIsSelectable="false"

Thank you Lint (not!)

John Sirach

I wanted to add a comment to rupps answer, but I do not have enough reputation.

If you are using a custom adapter extending the ArrayAdapter you can overwrite with areAllItemsEnabled() and isEnabled(int position) in your class:

@Override
public boolean areAllItemsEnabled() {
    return true;
}

@Override
public boolean isEnabled(int position) {
    return true;
}

The above fixed my non clickable list view. Maybe this comment also helps others as "android list not clickable" search term is quite high in Google.

Mitt

List view items are clickable. To use it, you have to set item click listener on your list view. Then it will work.

I'm pretty late but discovered something I think it's interesting about this:

if your adapter descends from ArrayAdapter, as much as I have tried, onItemClickListener is not called.

However, if your adapter descends from BaseAdapter (so you have to implement getCount() and getItem(), trivial for an array) it IS always called.

Vinod Joshi

I was using a view and button inside the list view so I have used:

android:focusable="false"

for <button> and <view> ...

After that I used following code for my list view and it worked

// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.shipping_item_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(listPairedClickItem);

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

    Toast.makeText(getBaseContext(), "Item ", Toast.LENGTH_LONG).show();
    }
};

For me, the problem was that my items within my ListView had clickable set to true.

set android:clickable="false" android:focusable="fasle"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!