2015-07-11 42 views
1

我已經在片段中使用了recyclerview。如果活動的configChanges包含定位,則在電話方向從橫向更改爲縱向後,回收站視圖會出現在屏幕中間。然而它總是應該在最前面。如果configChanges不包含方向,它總是出現在方向更改的頂部。RecyclerView在方向從橫向變爲縱向後在中間而不是頂部

你有什麼想法是什麼原因?

這是活動佈局

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.butterfly.LoginActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     android:layout_height="wrap_content"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    </android.support.design.widget.AppBarLayout> 


    <FrameLayout 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/butterflypi_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top|start" /> 


    </android.support.design.widget.CoordinatorLayout> 

這片段佈局替換上述的FrameLayout。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="8dp" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    tools:context="com.butterfly.fragment.ButterflyPIsFragment"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/my_pi_recycler_view" 
     android:scrollbars="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 

     /> 
</RelativeLayout> 

回答

4

我找到了解決方案here

基本上你創建你自己的行爲類:

public class PatchedScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior { 

    public PatchedScrollingViewBehavior() { 
     super(); 
    } 

    public PatchedScrollingViewBehavior(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) { 
     if (child.getLayoutParams().height == -1) { 
      List dependencies = parent.getDependencies(child); 
      if (dependencies.isEmpty()) { 
       return false; 
      } 

      AppBarLayout appBar = findFirstAppBarLayout(dependencies); 
      if (appBar != null && ViewCompat.isLaidOut(appBar)) { 
       if (ViewCompat.getFitsSystemWindows(appBar)) { 
        ViewCompat.setFitsSystemWindows(child, true); 
       } 

       int scrollRange = appBar.getTotalScrollRange(); 
//    int height = parent.getHeight() - appBar.getMeasuredHeight() + scrollRange; 
       int parentHeight = View.MeasureSpec.getSize(parentHeightMeasureSpec); 
       int height = parentHeight - appBar.getMeasuredHeight() + scrollRange; 
       int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST); 
       parent.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed); 
       return true; 
      } 
     } 

     return false; 
    } 


    private static AppBarLayout findFirstAppBarLayout(List<View> views) { 
     int i = 0; 

     for (int z = views.size(); i < z; ++i) { 
      View view = (View) views.get(i); 
      if (view instanceof AppBarLayout) { 
       return (AppBarLayout) view; 
      } 
     } 

     return null; 
    } 
} 

和不斷變化的應用程序:layout_behavior在你的FrameLayout:

<FrameLayout 
     app:layout_behavior="your_package.PatchedScrollingViewBehavior" 
     android:id="@+id/butterflypi_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="top|start" /> 

UPDATE:正如我剛纔檢查 - 這一問題已修復22.2.1庫,所以請更新庫,如果你仍在使用22.2.0

+0

感謝這對我工作.. – TdSoft

0

你也許能夠實現desi通過創建肖像和風景佈局的紅色效果,就像在demo here中一樣。如果這是你想要的行爲,請看附加的剪輯。

相關問題