【问题标题】:enable click event on parallax ListView with sticky header in android在android中使用带有粘性标题的视差ListView上启用点击事件
【发布时间】:2016-10-09 12:04:06
【问题描述】:

我在android中实现了一个带有视差效果的listView,带有一个sticky listview,我使用以下链接作为参考并成功实现了这个东西,但是我在点击listheader上方的背景图片时遇到了问题,可以任何人都可以帮我弄清楚,我可以找到列表项的子视图的点击事件

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

我无法发布我的代码,因为它太大而无法发布,但如果仍然需要它,我可以发布一些部分。

【问题讨论】:

  • 好的,但是这次点击有什么问题?
  • @ArturSzymański- 我无法点击子视图,因为它上面有空间视图,你通过链接了吗?会明白的。ImageView 不能在其中点击。
  • 你能把你的代码贴在你注册控件的地方并想处理它的点击事件吗? @JigarMakwana
  • @AnantShah - 转到链接,我的问题是我无法在此示例中单击图像。

标签: android listview parallax listitem


【解决方案1】:

我看到了@Jigar Makwana 发布的参考链接。您不会在 imageView 上获得点击事件,因为在 imageView 上有 "Space control" 在 listview 标题布局中使用,并且在其下方是 heroimageView(ImageView)。因此,当您单击不是 imageView 的标题部分时,它是 Space 小部件。即使空间控件无法获得点击事件,您也必须使用简单的 View 或在 listview 标题布局中使用 imageView 而不是空间。但这不是实现此设计和功能的正确方法。如果您想以正确的方式进行操作,请点击以下链接。

您的参考链接不是具有粘性布局的平行的好例子。 正确的例子是Handling Scrolls with CoordinatorLayout & Collapsing Toolbar Layout

您可以在 imageView 中使用 CoordinatorLayout,您希望单击事件来处理并制作一个包含您的标题文本的布局并将其添加为 listview 标题。就是这样并且完美运行,您将永远不会再看到错误。

代码很简单:

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

就是这样。

【讨论】:

  • 感谢 anant,所以在我目前的结构中不可能做到这一点?
  • 是的@JigarMakwana 我认为这个答案解决了你的问题,所以请接受我的回答。
  • @Anant-我确实无法解决,但您的参考很有帮助
  • @anant-谢谢兄弟,我正在努力,如果需要你的帮助,你能帮忙吗?
【解决方案2】:

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();
        }
    });

【讨论】:

    【解决方案3】:

    我使用与您相同的代码。您应该将触摸事件传递给标题图像

            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 是具有视差效果的图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多