简体   繁体   中英

Android - setOnItemClickListener() doesn't in listview

I can not solve the problem with not working setOnItemClickListener(). (API minSdkVersion 19, targetSdkVersion 21, compile 'com.android.support:appcompat-v7:21.0.3')

I tried to put in different places:

  • android:descendantFocusability="blocksDescendants"
  • android:clickable="false"
  • android:focusable="false"
  • android:focusableInTouchMode="false"
  • android:textIsSelectable="false"
  • categoriesListView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
  • getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

my fragment where I set listview

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.gallery_tab_categories, container, false);
    super.onCreate(savedInstanceState);
    categoriesListView = (ListView) view.findViewById(R.id.listViewCategories);

    categoriesListView.setAdapter(  new GalleryCategoriesListAdapter( getActivity(), CATEGORIES_DATA ) );
    //dbHandler = new ShoppingListDatabaseHandler(getActivity());
    categoriesListView.setLongClickable(true);

    final GalleryCategoriesListAdapter itemAdapter = new GalleryCategoriesListAdapter(getActivity(), CATEGORIES_DATA);

    registerForContextMenu(categoriesListView);
    categoriesListView.setAdapter(itemAdapter);

    //categoriesListView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);      //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    categoriesListView.setOnItemClickListener(new AdapterView.setOnItemClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            clickedItemIndex = position;

            return false;
        }
    });

    //if (dbHandler.getItemsCount() != 0){CATEGORIES_DATA.addAll(dbHandler.getAllItems());

    return view;
}

my xml with listview

<?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="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
<ListView
    android:id="@+id/listViewCategories"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:focusable="false"
    android:focusableInTouchMode="false" />
<!--android:focusable="false"-->
<!--android:clickable="false"-->

xml with single item

<?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="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
<!--android:autoLink="all"-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemCategoryName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />
<!--android:descendantFocusability="blocksDescendants"-->
<!--android:clickable="false"-->
<!--android:focusable="false"-->
<!--android:focusableInTouchMode="false"-->
<!--android:textIsSelectable="false"-->

Maybe I tried too many things and make my code not clear

You must delete this.

  android: enfocable = "false"
  android: focusableInTouchMode = "false"

//try with this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center_vertical" >

<ListView
    android:id="@+id/listViewCategories"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
 />

<LinearLayout/>

if you will specify the listview that events happen to the children, you will not click on item

I solved this.

I changed this fragment:

       categoriesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        }
    });

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