How set ListView not clickable

女生的网名这么多〃 提交于 2019-11-28 22:31:45

Here it is, following the comment:

One way to do so is: ListView.setOnClickListener(null); OR

You can add android:focusable="false" android:focusableInTouchMode="false" OR

another way in the layout android:listSelector="@android:color/transparent"

Cheers.

Arun C

Just override isEnabled(position) in your adapter and return false

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

override the below method. Return false to say all the items are not clickable.

 @override
  public boolean areAllItemsEnabled() {
    return false;
  }

And override below method to return which item is not clickable

public boolean isEnabled(int position) {
    if(position == your_item_pos) {
          return false;
    }
    return true;
}
android:listSelector="@android:color/transparent"

it changes the on-clicked color to same as when it is not clicked! so it appears as if it is plain text...it's a work around.

Just copy this method in your adapter class. Nothing to do .........

@Override
public boolean isEnabled(int position) {

    return false;
}

You may set any condition to make some selected item unclickable.

@Override
public boolean isEnabled(int position) {
    //Y for non clickable item
      if(data_list.get(position).equalsIgnoreCase("Y"))
        {
            return false;
        }
    return true;
}
android:focusable="true"

to any of the item inside listview now listview wont clickable

You could use

yourListView.setOnItemClickListener(null);

Easy way:

listView.setSelector(android.R.color.transparent);

Try this code

In Adapter.getView()

i.setOnClickListener( null );
i.setLongClickable( false );
i.setClickable( false );

Just set android:enabled="false" to the listview and it would do the trick..why to unneccesary keep overriding methods..

In fact, @override isEnabled() and setOnClickListener(null) setClickable(false) setLongClickable(false) There are not working for me. I solve it by override performItemClick

@Override
public boolean performItemClick(View view, int position, long id) {
 if(!clickAble){
     return false;
 }
    return super.performItemClick(view,position,id);
}

I have this ListView that just needs to show data. So I don't want to make it clickable.

I have done this in one of my projects, and the sample bellow works fine.

Basically, you just need:

  • a data source (ArrayList, for example)
  • a ListView Widget
  • an ArrayAdapter, in order to bind the data source with the ListView


MainActivity.java

package com.sample.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ListView;

    public class SecondActivity extends AppCompatActivity {

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

         // data source
         String[] arrayData = {"Debian", "Ubuntu", "Kali", "Mint"};

         // ListView Widget from a Layout
         ListView listView = (ListView) findViewById(R.id.mainListView);

         // an ArrayAdapter (using a layout as model for displaying the array items)
         ArrayAdapter aa = new ArrayAdapter<String>(this, R.layout.main_list_item_layout, arrayData);

         // binding the ArrayAdapter with the ListView Widget
         listView.setAdapter(aa);
    }
}


activity_main.xml
--->Make sure that the ListView Widget is using wrap_content for layout_width and layout_height

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sample.listview.MainActivity">


    <ListView
        android:id="@+id/mainListView"
        android:layout_width="wrap_content"  <---- HERE
        android:layout_height="wrap_content" <---- HERE
        />

</LinearLayout>


main_list_item_layout.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainListItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#ff0000"
    android:textSize="20sp"
    >

</TextView>



That's it: a ListView not clickable, just presenting data on the screen.

This is because your ListView item may have a focusable or clickable element, please try the following approach either in XML Layout file or in code:

  1. Add this attribute to the root ViewGroup element of your ListItem XML file (i.e. LinearLayout, RelativeLayout, ...) :

    android:descendantFocusability="blocksDescendants"

  2. Add this line to your List Adapter method which is responsible for creating/recycling the list item (most probably getView() or newView()), while parent is the ViewGroup of list item:

    parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

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