使用GridView/listView设计lib - CoordinatorLayout/CollapsingToolbarLayout

Tar*_*ney 27 java android coordinator-layout android-collapsingtoolbarlayout android-6.0-marshmallow

这可能是一个愚蠢的问题,但我不太了解Design lib.我遵循此参考创建下面的布局.当我滚动时,蓝色区域应该作为视差GridView.但是当我滚动网格时,AppBarLayout中没有任何反应.

但这适用于NestedScrollViewRecyclerView

布局

以下是我的布局文件 -

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:background="#500403"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#122453"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">


        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />
        <ImageView
            android:id="@+id/backdrop1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:fitsSystemWindows="true"
            android:layout_gravity="center"
            android:src="@drawable/bar_offline"
            app:layout_collapseMode="parallax" />


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#982223"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

  <GridView
      android:id="@+id/grid"
      android:numColumns="4"
      android:background="#367723"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      />
<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    android:src="@drawable/bar_offline"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

Tar*_*ney 39

使用ListView/GridView,它只能通过以下代码在Lollipop上运行 -

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     listView.setNestedScrollingEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)

我认为Now CoordinatorLayout只适用于RecyclerViewNestedScrollView

编辑:

使用 -

ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)
Run Code Online (Sandbox Code Playgroud)


dae*_*mie 14

一个简单的解决方案添加到支持库:

ViewCompat.setNestedScrollingEnabled(listView,true);
Run Code Online (Sandbox Code Playgroud)

我已经使用Android Support v4 Library 23.1进行了测试 ,效果很好.


Gab*_*tti 7

目前,ListViewGridView具有与预期的行为CoordinatorLayout只能用API> 21.

要获得此行为,您必须设置:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   
    setNestedScrollingEnabled(true);
 }
Run Code Online (Sandbox Code Playgroud)

实施这个是不够的NestedScrollingChild.在AbsListView未部署与支持库,那就要看通过在设备中的SO运行.

您必须覆盖AbsListView中的某些方法.例如,您可以检查onInterceptTouchEvent方法.

在此代码中,您可以看到:

  case MotionEvent.ACTION_DOWN: {
    //......
    startNestedScroll(SCROLL_AXIS_VERTICAL);
    //....
  }

  case MotionEvent.ACTION_MOVE: {
    //.....
     if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, null)) {
    //....     
   }
   case MotionEvent.ACTION_CANCEL:
   case MotionEvent.ACTION_UP: {
          //..
         stopNestedScroll();
            break;
   }
Run Code Online (Sandbox Code Playgroud)

此代码仅在AbsListView v21 +的实现中.如果使用API 20或更低版本检查AbsListView ,则不会找到任何嵌套的滚动引用.


Ana*_*abu 5

您必须像往常一样将gridview放在NestedScrollview中,然后您必须动态添加gridview高度.那么一切都会好起来的.!!!

 <android.support.v4.widget.NestedScrollView
       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:layout_gravity="fill_vertical"
               android:fillViewport="true">


               <LinearLayout
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:orientation="vertical">

              <GridView
               android:id="@+id/camp_list"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_below="@id/toolbar"
               android:layout_margin="10dp"
               android:gravity="center"
               android:horizontalSpacing="10dp"
               android:numColumns="3"
               android:stretchMode="columnWidth"
               android:verticalSpacing="10dp"
               android:visibility="visible" >
           </GridView>



               </LinearLayout>
           </android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)