【问题标题】:Design lib - CoordinatorLayout/CollapsingToolbarLayout with GridView/listView设计库 - 带有 GridView/listView 的 CoordinatorLayout/CollapsingToolbarLayout
【发布时间】:2015-08-22 04:18:44
【问题描述】:

这可能是个愚蠢的问题,但我不太了解设计库。我正在关注this reference 创建以下布局。当我滚动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>

任何帮助将不胜感激。

【问题讨论】:

    标签: java android android-6.0-marshmallow android-collapsingtoolbarlayout coordinator-layout


    【解决方案1】:

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         listView.setNestedScrollingEnabled(true);
    }
    

    我认为现在 CoordinatorLayout 仅适用于 RecyclerViewNestedScrollView

    编辑:

    使用 -

    ViewCompat.setNestedScrollingEnabled(listView/gridview,true); (add Android Support v4 Library 23.1 or +)
    

    【讨论】:

    • 非常有用的答案。非常感谢!
    【解决方案2】:

    在支持库中添加了一个简单的解决方案:

    ViewCompat.setNestedScrollingEnabled(listView,true);
    

    我已经用 Android Support v4 Library 23.1 对其进行了测试,效果很好。

    【讨论】:

      【解决方案3】:

      目前ListViewGridViewCoordinatorLayout 的预期行为仅适用于 API>21。

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

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {   
          setNestedScrollingEnabled(true);
       }
      

      仅实现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;
         }
      

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

      【讨论】:

        【解决方案4】:

        您必须像往常一样将 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>
        

        【讨论】:

        • ' int 高度 = 250; for (int i = 0; i
        【解决方案5】:

        这对我有用。

        https://gist.github.com/sakurabird/6868765

        我在 NestedScrollView 中使用 GridView

        【讨论】:

          【解决方案6】:

          为方便起见,我使用新的 ViewCompat 解决方案创建了一个子类:

          public class CoordinatedListView extends ListView {
          
              public CoordinatedListView(Context context) {
                  super(context);
                  init();
              }
          
              public CoordinatedListView(Context context, AttributeSet attrs) {
                  super(context, attrs);
                  init();
              }
          
              public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr) {
                  super(context, attrs, defStyleAttr);
                  init();
              }
          
              @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
              public CoordinatedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
                  super(context, attrs, defStyleAttr, defStyleRes);
                  init();
              }
          
              private void init() {
                  ViewCompat.setNestedScrollingEnabled(this, true);
              }
          }
          

          享受:)

          【讨论】:

            猜你喜欢
            • 2015-08-13
            • 2015-08-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-28
            • 1970-01-01
            • 1970-01-01
            • 2015-09-04
            相关资源
            最近更新 更多