簡體   English   中英

使用GridView / listView設計lib - CoordinatorLayout / CollapsingToolbarLayout

[英]Design lib - CoordinatorLayout/CollapsingToolbarLayout with GridView/listView

這可能是一個愚蠢的問題,但我不太了解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>

任何幫助,將不勝感激。

使用ListView / GridView,它只能通過以下代碼在Lollipop上運行 -

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

我認為Now CoordinatorLayout僅適用於RecyclerViewNestedScrollView

編輯:

使用 -

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

一個簡單的解決方案添加到支持庫:

ViewCompat.setNestedScrollingEnabled(listView,true);

我已經使用Android Support v4 Library 23.1進行了測試,效果很好。

目前, ListViewGridView僅在API> 21時具有CoordinatorLayout的預期行為。

要獲得此行為,您必須設置:

 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,則不會找到任何嵌套的滾動引用。

您必須像往常一樣將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>

這對我有用。

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

我在NestedScrollView中使用GridView

為方便起見,我使用新的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);
    }
}

請享用 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM