【问题标题】:OnClick event is not working in coverflowOnClick 事件在 Coverflow 中不起作用
【发布时间】:2023-06-21 21:13:01
【问题描述】:

我已经使用 https://github.com/moondroid/CoverFlow 实现了 Coverflow ViewPager,它运行良好,但 OnClick 事件无法从适配器运行。我尝试了很多但没有成功,有人知道吗?

我的代码如下所示

适配器点击监听器

viewHolder.txtBookNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(activity,ActivityDetail.class);
                activity.startActivity(mIntent);
            }
        });

适配器 xml 是

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="@drawable/cover_selector">

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

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@mipmap/simple_banner" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="15dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtPackage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:text="Package"
                    android:textColor="#0288D1"
                    android:textSize="12sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/txtBookNow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="5dp"
                    android:background="@drawable/rounded_corner"
                    android:padding="5dp"
                    android:text="Book Now"
                    android:textColor="#ffffff"
                    android:textSize="10sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </RelativeLayout>
    </FrameLayout>

【问题讨论】:

    标签: android onclick android-viewpager adapter coverflow


    【解决方案1】:

    在您的活动中实现 clicklistner 并尝试此操作

    mCoverFlow = (FeatureCoverFlow) findViewById(R.id.coverflow);
            mCoverFlow.setAdapter(mAdapter);
    
            mCoverFlow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // Do what you want
                    // list.get(position).titleResId
                }
            });
    

    或在适配器中

    holder.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    System.out.println("=============== " + mData.get(position).titleResId);
                }
            });
    

    【讨论】:

    • 我已经在 CoverFlow 上使用过这个 OnClick,但我想在 TextView 表单适配器上执行点击。
    • okey.. 比你尝试删除 setOnItemClickListener 比点击 textview 吗??
    • 是的,我已经删除了 CoverFlow 的 setOnItemClickListener 并在 Adapter 中添加了 OnClickListener。
    • 你的意思是我必须将 setOnItemClickListener 设置为 CoverFlow 并在适配器中设置 setOnClickListener ,对吗?
    • @Komal 您只需将 mCoverFlow.setOnItemClickListener(..) 设置为 CoverFlow。根据 Upendra 的回答,这很好。