在很多的APP设置中都会有:根据用户的手势上推或下拉页面列表,然后隐藏或显示ActionBar。这样子可以提供用户一个更加友好的界面,增强用户体验。
实践界面效果图:
这里刚刚开始的时候actionbar是全部显示的,随着列表慢慢往上推,actionbar渐渐隐藏,下拉的时候渐渐显示出来。给用户更加友好的视觉效果。
自定义ScrollView
MyScrollView
public class MyScrollView extends ScrollView {
private OnTransListener mTransListener;
public void setTransListener(OnTransListener transListener) {
mTransListener = transListener;
}
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mTransListener != null) {
int scrollY = getScrollY();
int height = getContext().getResources().getDisplayMetrics().heightPixels;
Log.i("tags", "scrollY:" + scrollY);
Log.i("tags", "height:" + height);
if (scrollY < height / 3f) {
//alpha=滑出去的高度/(screen_height/3f)
mTransListener.trans(1 - scrollY / (height / 4f));
}
}
}
public interface OnTransListener {
void trans(float alpha);
}
}
复制代码
自定义一个ScrollView,在ScrollView滑动的时候会不停的回调onScrollChanged方法,我们看可以在onScrollChanged里面获取滑动的高度,然后根据高度来设置alpha值,通过alpha来改变actionbar的alpha。这里还需要设置回调OnTransListener,使用时只需要设置自定义的ScrollView的OnTransListener回调即可。
xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.main.toolbar.ToolBarActivity">
<com.main.toolbar.MyScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button0"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button2"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button3"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button4"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button5"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button6"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button7"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button8"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button8"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button9"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button10"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button11"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button12"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button13"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button14"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="Button15"/>
</LinearLayout>
</com.main.toolbar.MyScrollView>
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="标题">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
复制代码
这里布局需要注意的是:
1)Toolbar需要盖在自定义ScrollView上面。
2)自定义ScrollView需要设置一下属性
android:clipChildren="false"
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize"
复制代码
设置ActionBar的alpha值
public class ToolBarActivity extends AppCompatActivity {
private Toolbar mToolbar;
private MyScrollView mScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tool_bar);
mToolbar = (Toolbar)this.findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
mScrollView = (MyScrollView)this.findViewById(R.id.scrollView);
mScrollView.setTransListener(new MyScrollView.OnTransListener() {
@Override
public void trans(float alpha) {
Log.i("tag","alpha:"+alpha);
mToolbar.setAlpha(alpha);
}
});
}
}
复制代码
在activity中直接设置MyScrollView的回调方法setTransListener,在回调里面设置ActionBar的alpha值即可。
这样就可以完成根据上推或下拉列表来隐藏或显示ActionBar了。