2016-07-25 3 views
0

В моем приложении у меня есть один объект MainActivity содержит контейнер для фрагментов, один фрагмент - координаторфрагмент с CordinatoirLayout + RecycleView и CollapsingToolbarLayout и второй - SimpleFragment с одним TextView. На создание MainActivity добавить первый фрагментКоординаторLayout в сочетании с CollapsingToolbarLayout, RecyclerView и Fragments issue

@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(); 
      } 
     }); 
    } 

После первой инициализации все Ok. Я могу прокрутить содержимое RecyclerView, а последний элемент полностью виден.

Scroll to last (first init)

Но после переезда второго фрагмента и вернуться к первому фрагменту, нажав кнопку назад, последний элемент из RecyclerView является вырезанным по размеру не разрушится частью панели. И нет возможности полностью просмотреть этот предмет. Все остальные функции - Ок.

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

Любые идеи, как решить эту проблему, оставляя реализацию на основе фрагмента? Спасибо заранее!

ответ

0

Измените высоту RecyclerView на match_parent.

+0

К сожалению, это не помогло –

+0

это может быть проблема, связанная с 'fitsSystemWindows'. Что вы можете попробовать: добавьте его как true в свойствах CoordinatorLayout, проверьте, истинно ли в главном макете действия и в макете второго фрагмента, вызовите 'setFitsSystemWindows (true)' в корневом представлении обоих фрагментов. – GPack

+0

Или замените FrameLayout как [здесь] (http://stackoverflow.com/a/34345286/4274296) – GPack

0

После некоторых попыток я решил перестроить компоновку координатора Фрагмента. Поскольку последний элемент, отрезанный точно на высотах LinearLayout, с «LEFT» и «RIGHT» TextViews, я удалил их из AppBarLayout и добавил RelativeLayout выше RecyclerView. Это исправить мою проблему.

макет 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. Может быть, мой собеседник будет полезен кому-то.

Смежные вопросы