2017-02-20 109 views
3

我在我的Fragment中使用了下面的代碼。當我滾動appbarlayout時,工具欄會隱藏,當我滾動recyclerview時它不會隱藏。我在這裏做錯了什麼?使用Coordinator Layout滾動recyclerview時無法隱藏工具欄

我的代碼:

<?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:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/tabanim_appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <include 
     android:id="@+id/toolbars" 
     layout="@layout/custom_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_scrollFlags="scroll|enterAlways" /> 

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


<android.support.v7.widget.RecyclerView 
    android:id="@+id/lv_nearby" 
    android:clipToPadding="false" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/floatbutton_nearby" 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:layout_gravity="bottom|right" 
    android:layout_marginBottom="8dp" 
    android:layout_marginRight="8dp" 
    android:scaleType="center" 
    android:src="@drawable/filter" /> 


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

代碼custom_tooolbar佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="?attr/actionBarSize" 
app:layout_scrollFlags="scroll|enterAlways"> 

<GridView 
    android:id="@+id/grid_nearby" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dp" 
    android:gravity="center" 
    android:horizontalSpacing="10dp" 
    android:numColumns="4" 
    android:verticalSpacing="10dp"> 

</GridView> 

</android.support.v7.widget.Toolbar> 
+0

嘗試不用「」它可能是工作。 –

+0

@aksacha嘗試過,不起作用 – Anirudh

+0

什麼是你的targetSdkVersion? –

回答

2

添加FitSystemWindows雙方協調佈局和AppbarLayout:

android:fitsSystemWindows="true" 

希望它能幫助:

+0

嘗試過,不起作用 – Anirudh

+0

刪除此屬性:工具欄中的app:layout_scrollFlags =「scroll | enterAlways」 – tahsinRupam

+0

有必要滾動工作。無論如何,我試過了,不起作用。 – Anirudh

3

我實現其工作正常,請檢查: 我們的XML:

<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.support.design.widget.AppBarLayout 
     android:id="@+id/appBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="AppBarLayoutSnapBehavior"> 


      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       app:layout_scrollFlags="scroll|enterAlways" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:focusableInTouchMode="true"> 

      </android.support.v7.widget.Toolbar> 

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


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/message_list_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:focusableInTouchMode="false" 
     /> 

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

和項目爲ReclerView:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:id="@+id/textView" 
    android:layout_gravity="center_horizontal" /> 

然後AppBarLayoutSnapBehavior類在AppbarLayout使用:

public class AppBarLayoutSnapBehavior extends AppBarLayout.Behavior { 

private ValueAnimator mAnimator; 
private boolean mNestedScrollStarted = false; 

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

@Override 
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, 
            View directTargetChild, View target, int nestedScrollAxes) { 
    mNestedScrollStarted = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); 
    if (mNestedScrollStarted && mAnimator != null) { 
     mAnimator.cancel(); 
    } 
    return mNestedScrollStarted; 
} 

@Override 
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target) { 
    super.onStopNestedScroll(coordinatorLayout, child, target); 

    if (!mNestedScrollStarted) { 
     return; 
    } 

    mNestedScrollStarted = false; 

    int scrollRange = child.getTotalScrollRange(); 
    int topOffset = getTopAndBottomOffset(); 

    if (topOffset <= -scrollRange || topOffset >= 0) { 
     // Already fully visible or fully invisible 
     return; 
    } 

    if (topOffset < -(scrollRange/2f)) { 
     // Snap up (to fully invisible) 
     animateOffsetTo(-scrollRange); 
    } else { 
     // Snap down (to fully visible) 
     animateOffsetTo(0); 
    } 
} 

private void animateOffsetTo(int offset) { 
    if (mAnimator == null) { 
     mAnimator = new ValueAnimator(); 
     mAnimator.setInterpolator(new DecelerateInterpolator()); 
     mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator animation) { 
       setTopAndBottomOffset((int) animation.getAnimatedValue()); 
      } 
     }); 
    } else { 
     mAnimator.cancel(); 
    } 

    mAnimator.setIntValues(getTopAndBottomOffset(), offset); 
    mAnimator.start(); 
} 

和我們的動作類適配器:

public class Second extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.message_list_view); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    MyRecyclerViewAdapter recyclerAdapter = new MyRecyclerViewAdapter(createItemList(), this); 
    recyclerView.setAdapter(recyclerAdapter); 
} 


private ArrayList<String> createItemList() { 
    ArrayList<String> list = new ArrayList(); 
    for(int i = 0; i < 200; i++) { 
     list.add(new String("List Item " + i)); 
    } 
    return list; 
} 
@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
} 

public class MyRecyclerViewAdapter extends RecyclerView 
     .Adapter<MyRecyclerViewAdapter 
     .DataObjectHolder> { 
    private String LOG_TAG = "MyRecyclerViewAdapter"; 
    private ArrayList<String> mDataset; 

    public MyRecyclerViewAdapter(ArrayList<String>list, Context context) 
    { 
     this.mDataset=list; 
     notifyDataSetChanged(); 
    } 



    public class DataObjectHolder extends RecyclerView.ViewHolder 
      implements View 
      .OnClickListener { 
     TextView label; 
     TextView dateTime; 

     public DataObjectHolder(View itemView) { 
      super(itemView); 
      label = (TextView) itemView.findViewById(R.id.textView); 
     } 

     @Override 
     public void onClick(View v) { 
     } 
    } 



    @Override 
    public DataObjectHolder onCreateViewHolder(ViewGroup parent, 
               int viewType) { 
     View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.recyclerview_item, parent, false); 

     DataObjectHolder dataObjectHolder = new DataObjectHolder(view); 
     return dataObjectHolder; 
    } 

    @Override 
    public void onBindViewHolder(DataObjectHolder holder, int position) { 
     holder.label.setText(mDataset.get(position)); 
    } 


    @Override 
    public int getItemCount() { 
     return mDataset.size(); 
    } 

} 

} 試試這可能會有幫助。

1

你是對的,當滾動RecyclerView時,它不隱藏Toolbar

我已經測試的代碼,現在,它通過刪除Include工作,增加app:layout_scrollFlags="scroll|enterAlways"ToolbarAppBarLayout,此外,一些修補。

下面是代碼:

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/tabanim_appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_scrollFlags="scroll|enterAlways"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways"> 

      <GridView 
       android:id="@+id/grid_nearby" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:gravity="center" 
       android:horizontalSpacing="10dp" 
       android:numColumns="4" 
       android:verticalSpacing="10dp" /> 

     </android.support.v7.widget.Toolbar> 

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

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/floatbutton_nearby" 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:layout_gravity="bottom|right" 
     android:layout_marginBottom="8dp" 
     android:layout_marginRight="8dp" 
     android:scaleType="center" 
     android:src="@mipmap/ic_launcher" /> 

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

你需要把你RecycleViewNestedScrollView並設置佈局行爲NestedScrollView在不同的佈局,包括在主要佈局這樣

content_scrolling .xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.v7.widget.RecyclerView 
    android:id="@+id/lv_nearby" 
    android:clipToPadding="false" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</android.support.v4.widget.NestedScrollView> 

佈局

<?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:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/tabanim_appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <include 
     android:id="@+id/toolbars" 
     layout="@layout/custom_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_scrollFlags="scroll|enterAlways" /> 

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

    //Include your scroll layout here 
    <include layout="@layout/content_scrolling"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/floatbutton_nearby" 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:layout_gravity="bottom|right" 
    android:layout_marginBottom="8dp" 
    android:layout_marginRight="8dp" 
    android:scaleType="center" 
    android:src="@drawable/filter" /> 


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

這組嵌套滾動的RecycleView假像這樣經過:

yourrecyclerView.setNestedScrollingEnabled(false); 
1

你的代碼紫霞升級你gradle這個內部版本較新的一個,將正常工作。我在

compileSdkVersion 25 
buildToolsVersion "25.0.2" 

目標版本targetSdkVersion 25

和設計LIB compile 'com.android.support:design:25.2.0'和沃金正確測試此代碼。

相關問題