【问题标题】:setOnItemClickListener is not working with listViewsetOnItemClickListener 不适用于 listView
【发布时间】:2021-08-11 04:40:44
【问题描述】:

我在 LinearLayout 中有一个嵌套的 ListView。因为我想在 ListView 的顶部添加一个滑块。

下面是我的 XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/listViewLinearLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <!--    SliderView-->
    <com.smarteist.autoimageslider.SliderView
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_centerInParent="true"
        app:sliderAnimationDuration="600"
        app:sliderAutoCycleDirection="back_and_forth"
        app:sliderIndicatorAnimationDuration="600"
        app:sliderIndicatorEnabled="true"
        app:sliderIndicatorGravity="center_horizontal|bottom"
        app:sliderIndicatorMargin="15dp"
        app:sliderIndicatorOrientation="horizontal"
        app:sliderIndicatorPadding="3dp"
        app:sliderIndicatorRadius="2dp"
        app:sliderIndicatorSelectedColor="#5A5A5A"
        app:sliderIndicatorUnselectedColor="#FFF"
        app:sliderScrollTimeInSec="1" />

<ListView
     android:id="@+id/list"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>
</LinearLayout>

现在我想在 ListView 上设置 OnItemClickListener。这样当我们点击任何 listItem 时,我们就可以显示另一个活动。

为此,我编写了这段 JAVA 代码:

CardAdapter cardAdapter = new CardAdapter(this,cards);

        ListView listView = findViewById(R.id.list);

        listView.setAdapter(cardAdapter);

        // Setting listener to ListView
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.v("GetIntent","PLace Name =");

                Card card = cards.get(position);
                Intent intent = new Intent(HistoricActivity.this,HistoricDetailsActivity.class);
                intent.putExtra("MyClass", (Serializable) card);
                startActivity(intent);
            }
        });

出于调试目的,我还在 OnItemClickListener 中打印了一些日志。

所以问题是它不起作用。

在我之前做的项目中。它看起来像这样: here new AdapterView.OnItemClickListener() is highlighted.

但现在在这个: new AdapterView.OnItemClickListener() is not getting highlighted by Android Studio.

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    尝试像这样设置您的按钮(或您想要处理单击列表项内的任何其他视图):

    android:focusable="false"
    android:focusableInTouchMode="false"
    

    【讨论】:

    • 我更新了问题,请检查以下照片。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    相关资源
    最近更新 更多