简体   繁体   中英

Trying to make listview items non-clickable

I have listview that I have populated into a alertDialog When the alertDialog displays the user are able to click on these items in the listview and that is when the problem comes in. I do not want the user to be able to click on the items in the listview and I have already tried adding this to my xml layout android:clickable="false" and android:focusable="false" but I am still getting the same results. Can somebody assist me with this issue that having.

Here is my base adapter class:

    public class MyAdapter extends BaseAdapter {

        final String[] listItemsFirstRow = {"Item1","Item2"};
        final String[] listItemSecondRow = {"Item1", "Item2"};

        @Override
        public int getCount() {     
            return listItemsFirstRow.length;
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return position;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null)
            {
                LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.twolinelistview, null);
            }

            ((TextView)convertView.findViewById(R.id.text1)).setText( listItemsFirstRow[position]);
            ((TextView)convertView.findViewById(R.id.text2)).setText( listItemSecondRow[position]);

            return convertView;

        }

   }

And here is my xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:paddingLeft="15dip"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <TextView
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false" />
    <TextView
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="?android:attr/textColorSecondary" 
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false" />
</LinearLayout>

Modify your BaseAdapter to override isEnabled to tell the ListView that the items aren't clickable.

Example:

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

Note that the documentation says this will do exactly what you want:

Returns true if the item at the specified position is not a separator. (A separator is a non-selectable, non-clickable item)

Try setting android:clickable="false" and android:longClickable="false" for your ListView in XML, or you can set it before you set the adapter as well. That should be enough to disable it, but if not android:focusable="false" and android:enabled="false" should definitely do it.

If you are just trying to prevent items from being highlighted another option is to change how it displays selected items and ignoring the events. One way that was suggested in an older question is:

android:listSelector="@android:color/transparent"               
android:cacheColorHint="@android:color/transparent"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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