简体   繁体   English

使用android中的粘性标题在视差ListView上启用click事件

[英]enable click event on parallax ListView with sticky header in android

I have implemented a listView with parallax effect with a sticky listview in android,I have used the following link for the reference and have implemented successfully this thing,But I am facing an issue on clicking the background images above the listheader,Can anybody help me to figure it out and can I find the click events of childviews of listitems 我在android中实现了一个带有视差效果的listView和一个粘性列表视图,我使用了以下链接作为参考并成功实现了这个东西,但是我在点击listheader上方的背景图片时面临一个问题,任何人都可以帮助我找出它,我可以找到listitems的子视图的点击事件

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

i can't post my code as its too large to post,But if still it is needed i can post some parts. 我不能发布我的代码,因为它太大而无法发布,但如果仍然需要我可以发布一些部分。

I saw reference link @Jigar Makwana posted. 我看到参考链接@Jigar Makwana发布了。 You will not get click event on imageView because on imageView there is "Space control" used inside the listview header layout, And below it that heroimageView (ImageView). 你不会在imageView上获得click事件,因为在imageView上有listview标题布局中使用的“Space control” ,而在它下面是heroimageView (ImageView)。 So when you click on that header part that is not imageView it is Space widget. 因此,当您单击不是imageView的标题部分时,它就是Space小部件。 Even though space control cannot get click event you have to use simple View or use that imageView inside listview header Layout instead of space . 即使空间控制无法获得点击事件,您也必须使用简单的View在listview标题Layout而不是space中使用imageView But it is not proper way to implement this design & functionality. 但这不是实现此设计和功能的正确方法。 If you want to do it in proper way follow below links. 如果您想以正确的方式进行,请按照以下链接进行操作。

Your reference link is not good example of parallex with sticky layout. 您的参考链接不是具有粘性布局的parallex的好例子。 Proper example are Handling Scrolls with CoordinatorLayout & Collapsing Toolbar Layout 正确的示例是使用CoordinatorLayoutCollapsing工具栏布局 处理滚动

In that you can use CoordinatorLayout inside imageView you want click event to handle and make one layout containing your headingText and add it as listview header.That's it and works perfectly and you will never see bug again. 你可以在imageView中使用CoordinatorLayout,你想要点击事件来处理并制作一个包含你的headingText的布局,并将其添加为listview标题。这就是它并且完美地工作,你永远不会再看到bug。

Code is simple: 代码很简单:

    <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>

That's it. 而已。

add this code after listView.setAdapter(adapter); listView.setAdapter(adapter);之后添加此代码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();
        }
    });

I use the same code as you do. 我使用与您相同的代码。 You should pass touch event to the header Image 您应该将touch事件传递给标题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;
        }
    });

the heroImageView is the image has parallax effect. heroImageView是具有视差效果的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM