在 android 中使用粘性 header 在视差 ListView 上启用点击事件

enable click event on parallax ListView with sticky header in android

我在android实现了一个带视差效果的listView和sticky listview,我参考了下面的link并且成功实现了这个东西,但是我遇到了一个问题点击列表头上方的背景图片,谁能帮我弄清楚,我能找到列表项子视图的点击事件

ref Link : http://javatechig.com/android/listview-header-parallax-with-sticky-view-in-android

我不能 post 我的代码,因为它太大 post,但如果仍然需要它,我可以 post 一些部分。

listView.setAdapter(adapter);

之后添加此代码
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "item " + position + 1 + " clicked!", Toast.LENGTH_LONG).show();
        }
    });

我看到了参考 link @Jigar Makwana 的帖子。您不会在 imageView 上获得点击事件,因为在 imageView 上,列表视图 header 布局中使用了 "Space control",在其下方 heroimageView(ImageView)。因此,当您单击不是 imageView 的 header 部分时,它是 Space 小部件。即使 space 控件无法获得点击事件,您也必须使用简单的 View 或使用 imageView inside listview header Layout 而不是 space。但这不是实现此设计和功能的正确方法。如果您想以正确的方式进行操作,请遵循以下 links。

您的参考 link 不是粘性布局视差的好例子。 正确的例子是 Handling Scrolls with CoordinatorLayout & Collapsing Toolbar Layout

因为您可以在 imageView 中使用 CoordinatorLayout,您希望点击事件来处理并制作一个包含您的 headingText 的布局,并将其添加为列表视图 header.That 并且工作完美,您再也不会看到错误。

代码很简单:

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:minHeight="?attr/actionBarSize"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:id="@+id/relativeLayout_main_profile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="7dp"
                android:animateLayoutChanges="true"
                app:layout_collapseParallaxMultiplier="0.7"
                app:layout_collapseMode="parallax"
                tools:context="com.mazkara.user.activity.ProfileActivity">
               <ImageView
                android:id="@+id/heroImageView"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/wallpaper"
                android:scaleType="fitCenter" />
            </RelativeLayout>
// only if you use toolbar then specify here your toolbar start
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_main_transparent"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/transparent"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:titleTextAppearance="@color/white">
            </android.support.v7.widget.Toolbar>    //toolbar end 
        </android.support.design.widget.CollapsingToolbarLayout>
       <TextView
        android:id="@+id/stickyView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#222"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:text="Heading1"
        android:textColor="#fff"
        android:textSize="20sp"
        android:layout_gravity="bottom"
        app:layout_collapseMode="pin"
        android:textStyle="bold" />

    </android.support.design.widget.AppBarLayout>


     <android.support.v7.widget.RecyclerView
    android:id="@+id/design_bottom_sheet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/bottom_sheet_behavior">
</android.support.design.widget.CoordinatorLayout>

就是这样。

我使用的代码与您相同。您应该将触摸事件传递给 header Image

        listHeader = getLayoutInflater().inflate(R.layout.list_header, mListView, false);
        mListView.addHeaderView(listHeader, null, true);
        listHeader.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            heroImageView.onTouchEvent(event);
            return true;
        }
    });

heroImageView是有视差效果的图片。