2016-07-25 130 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会对某人有用。

相关问题