请查看我的库:可收起的工具栏
您需要在build.gradle
中添加以下内容:
compile 'it.michelelacorte.retractabletoolbar:library:1.0.0'
然后在你的 MainActivity.java
中使用 RecyclerView
和以下代码:
RetractableToolbarUtil.ShowHideToolbarOnScrollingListener showHideToolbarListener;
recyclerView.addOnScrollListener(showHideToolbarListener = new RetractableToolbarUtil.ShowHideToolbarOnScrollingListener(toolbar));
if (savedInstanceState != null) {
showHideToolbarListener.onRestoreInstanceState((RetractableToolbarUtil.ShowHideToolbarOnScrollingListener.State) savedInstanceState
.getParcelable(RetractableToolbarUtil.ShowHideToolbarOnScrollingListener.SHOW_HIDE_TOOLBAR_LISTENER_STATE));
}
这是效果:
编辑:
自23.1.0设计库以来,您可以将|snap
属性添加到您的ToolBar布局中:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways|snap />
这应该就是您所寻找的内容。
compile "com.github.godness84:appbar-snap-behavior:0.1.1"
记得在你的根build.gradle文件的repositories末尾添加maven { url "https://jitpack.io" }
:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
然后:
app:layout_behavior="com.github.godness84.appbarsnapbehavior.AppBarSnapBehavior"
添加到您的 AppBarLayout 中app:layout_behavior="com.github.godness84.appbarsnapbehavior.ScrollingViewBehavior"
应用到您的滚动容器中。不幸的是,由于默认行为被替换,一些功能不再可用(例如 AppBarLayout.setExpanded()),但在正常情况下它可以工作!试试看,并让我知道效果如何。
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/status_bar_height" />
<FrameLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_app_bar"
android:background="@color/appbar"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="@dimen/status_bar_height"
android:background="@color/appbar" />
<android.support.design.widget.AppBarLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:background="@color/appbar"
/>
</FrameLayout>
</FrameLayout>
帧布局将成为您的“新”应用栏。
然后,您的片段(在viewpager中的那些)必须扩展此类:
public abstract class SnappableAppBarFragment extends Fragment {
public int scrollAttuale;
private boolean attivaSnap = true;
private boolean isTouching = false;
public void setSnapActivated(boolean state){attivaSnap = state;}
public void setUpSnappableAppbar(final View fragMainView, final NestedScrollView nestedScrollView, final FrameLayout appBar, final int actionBarHeight) {
nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (!attivaSnap)
return;
int scrollY = nestedScrollView.getScrollY();
int differenza = scrollAttuale - scrollY;
if (differenza > 0 && appBar.getY() < 0) {
//Esci
appBar.animate().translationYBy(differenza).setDuration(0).start();
if (appBar.getY() > 0) {
appBar.animate().translationY(0).setDuration(0).start();
}
}
if (differenza < 0 && appBar.getY() > -actionBarHeight) {
//Entra
appBar.animate().translationYBy(differenza).setDuration(0).start();
if (appBar.getY() < -actionBarHeight)
appBar.animate().translationY(-actionBarHeight).setDuration(0).start();
}
if (differenza >= -2 && differenza <= 2 && !isTouching ){
int spazioTot = actionBarHeight;
if ((Math.abs(appBar.getY()) < spazioTot / 2 || nestedScrollView.getScrollY() <= 200) && appBar.getY() != 0) {
//Espandi
appBar.animate().translationY(0).setDuration(270).start();
} else if (appBar.getY() != 0) {
//Chiudi
appBar.animate().translationY(-actionBarHeight).setDuration(270).start();
}
}
scrollAttuale = scrollY;
//Scrolling verso l'alto differenza è positiva
//Scrolling verso il basso differenza è negativa
}
});
fragMainView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!attivaSnap)
return false;
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
isTouching = false;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
int spazioTot = actionBarHeight;
// && nestedScrollView.getScrollY() <= 200 && nestedScrollView.getScrollY() <= 200 && nestedScrollView.getScrollY() <= 200
if ((Math.abs(appBar.getY()) < spazioTot / 2 || nestedScrollView.getScrollY() <= 200) && appBar.getY() != 0) {
//Espandi
appBar.animate().translationY(0).setDuration(270).start();
} else if (appBar.getY() != 0) {
//Chiudi
appBar.animate().translationY(-actionBarHeight).setDuration(270).start();
}
}
}, 0);
}
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN || motionEvent.getAction() == MotionEvent.ACTION_SCROLL) {
isTouching = true;
}
return false;
}
});
}
}
public class Fragment1 extends SnappableAppBarFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
viewPrincipale = inflater.inflate(R.layout.fragment_home, container, false);
mainActivity = (MainActivity) getActivity();
setUpSnappableAppbar(viewPrincipale, (NestedScrollView) viewPrincipale.findViewById(R.id.nestedScrollView), parentAppBar, actionBarHeight);
...
}
public void setParentAppBar(FrameLayout frameLayout) {
parentAppBar = frameLayout;
}
public void setActionBarHeight(int height) {
actionBarHeight = height;
}
...
}
fragment1.setParentAppBar((FrameLayout) findViewById(R.id.appBarLayout));
fragment1.setActionBarHeight(toolbar.getLayoutParams().height);
这就是全部内容了,如果有点长,但这是我找到的唯一方法来使它工作!
另外,抱歉我的英语不好,我是一名意大利开发人员:P 再见
编辑:重要!!在SNAPPABLEAPPBARFRAGMENT中更改此内容:final int actionBarHeight TO final float actionBarHeight !!! 它会更加流畅:D
嗨,使用以下布局,它将完全像谷歌Play商店应用程序一样运作,我已经测试过了。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
请注意我使用了以下设计支持库
compile 'com.android.support:design:23.1.1'
如果有任何问题,请告诉我,我一定会帮助你解决。