【问题标题】:How to make listviews onitemclicklistener work?如何使 listview onitemclicklistener 工作?
【发布时间】:2017-03-14 08:45:12
【问题描述】:

我一直在寻找使我的列表视图工作的方法。我几乎使用了所有我知道的东西和我在这里看到的东西。我使用了代码和 XML 标记,但它仍然不起作用。我一直在更改将那些块后裔、可聚焦、可点击标签放在 xml 中的位置。

这是我的相关代码:

lstMeals = (ListView)findViewById(R.id.lstMeals);

lstMeals.setItemsCanFocus(true);
lstMeals.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

这是带有列表视图的 xml:

<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout
    android:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ProgressBar
            android:id="@+id/pgMeals"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:layout_centerInParent="true" />

        <TextView
            android:id="@+id/txtMNoRecord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            android:layout_centerInParent="true" />

        <ListView
            android:id="@+id/lstMeals"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"/>

    </RelativeLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>

这是适配器的布局:

<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout
    android:id="@+id/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/mealInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true">

                <ImageView
                    android:id="@+id/imgMeal"
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="8dp"
                    android:clickable="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/>

                <RelativeLayout
                    android:id="@+id/picLayout"
                    android:layout_width="200dp"
                    android:layout_height="70dp"
                    android:layout_alignBottom="@+id/imgMeal"
                    android:layout_alignParentBottom="false"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true">

                    <TextView
                        android:id="@+id/txtWatermark"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentStart="true"
                        android:layout_centerVertical="true"
                        android:gravity="center"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:text="Large Text"
                        android:textColor="#ffffff"
                        android:textSize="25sp"
                        android:textStyle="bold"
                        android:textIsSelectable="false"/>

                    <TextView
                        android:id="@+id/txtWatermark2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="false"
                        android:layout_centerVertical="true"
                        android:gravity="center_vertical|center_horizontal"
                        android:text="Large Text"
                        android:textSize="30sp"
                        android:textStyle="bold"
                        android:visibility="gone"
                        android:textIsSelectable="false"/>
                </RelativeLayout>
            </RelativeLayout>

            <TextView
                android:id="@+id/adptrMealName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="8dp"
                android:layout_toEndOf="@+id/relativeLayout"
                android:layout_toRightOf="@+id/relativeLayout"
                android:text="TextView"
                android:textColor="@color/black80"
                android:textSize="24sp"
                android:textStyle="bold"
                android:textIsSelectable="false"/>

            <TextView
                android:id="@+id/txtMealDesc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/txtMealPrice"
                android:layout_marginBottom="10dp"
                android:layout_toEndOf="@+id/relativeLayout"
                android:layout_toRightOf="@+id/relativeLayout"
                android:text="TextView"
                android:textColor="@color/black70"
                android:textSize="24sp"
                android:textIsSelectable="false"/>

            <Button
                android:id="@+id/btnViewIngredients"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/txtMealDesc"
                android:paddingBottom="15dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:paddingTop="15dp"
                android:text="View Ingredients"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

            <TextView
                android:id="@+id/txtMealPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/adptrMealName"
                android:layout_marginBottom="8dp"
                android:layout_toEndOf="@+id/relativeLayout"
                android:layout_toRightOf="@+id/relativeLayout"
                android:text="TextView"
                android:textColor="@color/black70"
                android:textSize="24sp"
                android:textIsSelectable="false"/>
        </RelativeLayout>

    </RelativeLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>

【问题讨论】:

  • 写在你的适配器类的点击监听器底页
  • 我为什么要放一个 onclicklistener?不能正常工作的是列表视图。

标签: android xml listview


【解决方案1】:

这可能有用.....

    clubList = (ListView) dialog.findViewById(R.id.club_listview_dialog);
    clubList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //postion is the position of item clicked in the list
        }
    });

【讨论】:

  • 嗨 AwaisMajeed,感谢您的帮助。我试过了,但还是不行
【解决方案2】:

将此添加到您的 onCreate。

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

                   Toast.makeText(getApplicationContext(), "Position->" + position + "is selected.", Toast.LENGTH_SHORT).show();

            }
        });

希望这会有所帮助。

【讨论】:

  • 感谢 tahsinRupam,但它只允许 adapterview.onitemclicklistener
  • @RickyManalo,ListView 扩展了AdapterView,所以每个ListView 也是一个AdapterView
  • 这个答案解决了你的问题吗?如果它对您有帮助,您可以将其标记为已接受,以后其他人也会因此而得到帮助。
【解决方案3】:

非常感谢您的帮助。我已经找到了答案。问题是由 bottomsheet 引起的。适配器没有使用它,因为该活动是显示底页的活动。这段代码是我朋友的代码,所以我对此一无所知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多