android 仿饿了么悬浮购物车按钮

上效果图,我这边是一个交易查询的按钮:

实现效果为,滑动屏幕时,按钮隐藏,停止之后显示,并且按钮固定在屏幕的垂直居中位置。

首先是布局文件:

<?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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值