上效果图,我这边是一个交易查询的按钮:
实现效果为,滑动屏幕时,按钮隐藏,停止之后显示,并且按钮固定在屏幕的垂直居中位置。
首先是布局文件:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl_cj_home_f1"
>
<app.view.ObservableScrollView
android:id="@+id/sv_cjf1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</RelativeLayout>
</app.view.ObservableScrollView>
<RelativeLayout
android:id="@+id/rl_jycx"
android:layout_width="@dimen/dp60"
android:layout_height="@dimen/dp65"
android:background="@drawable/round_button_bg_jianbian_blue_r5_cj"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp10"
android:layout_alignParentRight="true"
android:visibility="gone"
>
<ImageView
android:id="@+id/iv_icon_jycx"
android:layout_width="@dimen/dp32"
android:layout_height="@dimen/dp32"
android:src="@mipmap/icon_jycx"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="交易查询"
android:textSize="@dimen/sp12"
android:textColor="#fff"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/dp5"
android:layout_below="@+id/iv_icon_jycx"/>
</RelativeLayout>
</RelativeLayout>
我这边是在相对布局里面放了一个滑动控件,然后再单独把“交易查询”的按钮放出来,设置最外层的垂直居中。
因为我的按钮是在fragment里面的,然而fragment中没有dispatchTouchEvent方法,所以我选择在这个fragment中定义隐藏显示按钮的方法,然后在activity中调用这个fragment中的方法:
fragment中定义方法:
/**
* 显示交易查询按钮
*/
boolean isShowJycx = true; //是否显示
public void showJycx(){
if (!isShowJycx){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
textView_jycx.setVisibility(View.VISIBLE);
//向左边移入
textView_jycx.setAnimation(AnimationUtils.makeInAnimation(getActivity(), false));
isShowJycx = true;
}
}, 1000);
}
}
/**
* 隐藏交易查询按钮
*/
public void hideJycx(){
if (isShowJycx){
textView_jycx.setVisibility(View.GONE);
// 向右边移出
textView_jycx.setAnimation(AnimationUtils.makeOutAnimation(getActivity(), true));
isShowJycx = false;
}
}
}
在activity中调用fragment中的方法:
/**
* 获取首页的事件分发
* 在按下 滑动时隐藏 交易查询按钮
* 在抬起手指时 显示 交易查询按钮
* @param ev
* @return
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//通过ID获取fragment
F1Cj f1Cj = (F1Cj) getSupportFragmentManager().findFragmentById(R.id.home_pager);
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
f1Cj.hideJycx();
break;
case MotionEvent.ACTION_MOVE:
f1Cj.hideJycx();
break;
case MotionEvent.ACTION_UP:
f1Cj.showJycx();
break;
}
return super.dispatchTouchEvent(ev);
}
这个比较简单 就不上传代码了0.0