Android - setOnItemClickListener()不在listview中

时间:2015-02-15 14:02:36

标签: android listview listener

我无法解决不使用setOnItemClickListener()的问题。  (API minSdkVersion 19,targetSdkVersion 21,compile' com.android.support:appcompat-v7:21.0.3')

我试图放在不同的地方:

  • 机器人:descendantFocusability =" blocksDescendants"
  • 机器人:可点击="假"
  • 机器人:可聚焦="假"
  • 机器人:focusableInTouchMode ="假"
  • 机器人:textIsSelectable ="假"
  • categoriesListView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
  • getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

我设置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;
}

带有listview的xml

<?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"-->

也许我尝试了太多东西并且使我的代码不清楚

2 个答案:

答案 0 :(得分:0)

您必须删除此内容。

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

//尝试使用此

<?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/>

如果您要指定事件发生在孩子们的列表视图,则不会点击项目

答案 1 :(得分:0)

我解决了这个问题。

我改变了这个片段:

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