2016-07-25 124 views
0

在我的應用程序中,我有一個MainActivity女巫包含容器的片段,一個片段 - CoordinatorFragment與CordinatoirLayout + RecycleView和CollapsingToolbarLayout和第二個SimpleFragment與一個TextView。在創建MainActivity我添加第一個片段CoordinatorLayout結合CollapsingToolbarLayout,RecyclerView和Fragments問題

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    Fragment fragment = new CoordinatorFragment(); 
    transaction.replace(R.id.fragmentContainer,fragment); 
    transaction.commit(); 
} 

CoordinatorFragnet的佈局:

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/main_appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFF" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/main.collapsing" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#0000" 
      android:fitsSystemWindows="true" 
      app:layout_scrollFlags="snap|scroll|enterAlways"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#0000" 
       android:fitsSystemWindows="true" 
       android:orientation="vertical" 
       app:layout_collapseMode="none"> 

       <ImageView 
        android:id="@+id/first" 
        android:layout_width="wrap_content" 
        android:layout_height="23dp" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:layout_marginTop="30dp" 
        android:adjustViewBounds="true" /> 

       <TextView 
        android:id="@+id/second" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:layout_marginTop="40dp" /> 

       <TextView 
        android:id="@+id/third" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" /> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="130dp" 
        android:layout_marginTop="20dp" 
        android:background="#fff" /> 
      </LinearLayout> 
     </android.support.design.widget.CollapsingToolbarLayout> 

     <View 
      android:id="@+id/fourth" 
      android:layout_width="match_parent" 
      android:layout_height="10dp" 
      android:background="#fff" 
      app:layout_collapseMode="pin" /> 

     <LinearLayout 
      android:id="@+id/fiveth_field" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:background="@color/colorPrimary" 
      app:layout_collapseMode="pin"> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:padding="20dp" 
       android:text="RIGHT" /> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:padding="20dp" 
       android:text="LEFT" /> 
     </LinearLayout> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 

在使用RecycleeView的適配器我代替第一片段與第二

@Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     holder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       getFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new SimpleFragment()).addToBackStack("TAG").commit(); 
      } 
     }); 
    } 

首先初始化後一切正常。我可以滾動RecyclerView內容和最後一項完全可見。

Scroll to last (first init)

但移動到第二個片段,並返回到第一個片段,單擊後退按鈕之後,RecyclerView的最後一個項目是在板缺的不是崩潰工具欄的零件尺寸。並且沒有能力完全查看此項目。所有其他功能都OK。

Scroll to last (after fragments replacement and click back button)

任何想法如何解決這一問題留下基於片段執行? 在此先感謝!

回答

0

將RecyclerView的高度更改爲match_parent

+0

不幸的是它沒有幫助 –

+0

它可能是一個與'fitsSystemWindows'有關的問題。您可以嘗試的事情:在CoordinatorLayout屬性中將其添加爲true,在活動的主佈局和第二個片段的佈局中檢查是否爲true,在兩個片段的根視圖上調用'setFitsSystemWindows(true)'。 – GPack

+0

或將您的FrameLayout替換爲[這裏](http://stackoverflow.com/a/34345286/4274296) – GPack

0

經過一番嘗試,我決定重構協調片段的佈局。因爲最後一個元素在LinearLayout的高度上用「LEFT」和「RIGHT」TextViews切割,我將它們從AppBarLayout中刪除,並添加到RecyclerView上方的RelativeLayout。這解決了我的問題。

CoordinatorFragment的重構後的佈局:

<android.support.design.widget.CoordinatorLayout 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:orientation="vertical"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/main_appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFF"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/main.collapsing" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#0000" 
      app:layout_scrollFlags="snap|scroll|enterAlways"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       android:background="#0000" 
       android:orientation="vertical" 
       app:layout_collapseMode="none"></LinearLayout> 
     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:id="@+id/fiveth_field" 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:background="@color/colorPrimary" 
      android:orientation="horizontal"> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:padding="20dp" 
       android:text="RIGHT" /> 

      <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="center_horizontal" 
       android:padding="20dp" 
       android:text="LEFT" /> 
     </LinearLayout> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/fiveth_field" /> 
    </RelativeLayout> 
</android.support.design.widget.CoordinatorLayout> 

我認爲這是在CoordinatorLayout + AppBarLayout實現問題。也許我的anwser會對某人有用。

相關問題