OnClick事件在coverflow中不起作用

时间:2017-08-17 04:42:02

标签: android onclick android-viewpager adapter coverflow

我使用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>

1 个答案:

答案 0 :(得分: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);
            }
        });