问题
I have a ListView that I'm populating with values from my database. If the database is empty, I'm setting the first item of the ListView to "No data.". I want to disable clicking on this item. I've used ArrayAdapter. I tried making areAllItemsEnabled,isEnabled false, but it was of no use. Even if I set the ListView's isClickable and setEnabled to false, it is of no use. And I put the code for the OnItemClickListener in the else condition,even that doesn't stop the list item from being clickable. Does someone have an alternate solution? Thanks!
回答1:
In your custom ArrayAdapter use isEnabled function to return false:
@Override
public boolean isEnabled(int position) {
return false;
}
always works for me.
回答2:
all the easier! list.setEnabled(false)
回答3:
You can set the empty View as showed and it will be handled automatically:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
回答4:
create Adapter for that list, and there override this method
public boolean isEnabled(int position);
then return false
when you want to disable the click
回答5:
Perhaps you could use an if statement to check the contents of the listview entry in the OnClick, if it contains 'No data' do nothing, else do the usual
回答6:
I did like this according to my requirement hope it can help you some how
@Override
public boolean isEnabled(int position) {
if(data.get(position).isClickable==false)
{
return false;
}
return super.isEnabled(position);
}
回答7:
Try setting these two attributes on the ListView:
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
来源:https://stackoverflow.com/questions/13146652/how-to-disable-clicking-on-listview-in-android