android 高仿美团,Android 仿美团、大众点评团购详情UI

在scrollview 上滑固定某一控件(美团团购详情UI)文中介绍了怎么用touchlistener实现类似上滑停住的效果,但是这种方法存在一个明显的bug,就是在内容比较多的时候, 大部分人都是以滑动方式查看内容,而不是touch的方式,这就会导致最上面的滑块出现不及时,或者延后的现象,这里介绍一个全新的方法去实现类似效果,可以很好的解决以上问题.

目前在scrollview中没有onscrolllistener所以需要自己去实现,先复写一个scrollview:

package com.example.meituandemo;

import android.content.Context;

import android.util.AttributeSet;

import android.widget.ScrollView;

public class MyScrollView extends ScrollView {

private OnScrollListener onScrollListener;

public MyScrollView(Context context) {

this(context, null);

}

public MyScrollView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public MyScrollView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

/**

* 设置滚动接口

* @param onScrollListener

*/

public void setOnScrollListener(OnScrollListener onScrollListener) {

this.onScrollListener = onScrollListener;

}

@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt) {//滑动改变就会实时调用

super.onScrollChanged(l, t, oldl, oldt);

if(onScrollListener != null){

onScrollListener.onScroll(t);

}

}

/**

*

* 滚动的回调接口

*

*/

public interface OnScrollListener{

/**

* 回调方法, 返回MyScrollView滑动的Y方向距离

* @param scrollY

* 、

*/

public void onScroll(int scrollY);

}

}

然后就在mainactivity中调用:

package com.example.meituandemo;

import android.app.Activity;

import android.os.Bundle;

import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import android.widget.LinearLayout;

import com.example.meituandemo.MyScrollView.OnScrollListener;

public class MainActivity extends Activity implements OnScrollListener{//注意继承的是自定义的listener

/**

* 自定义的MyScrollView

*/

private MyScrollView myScrollView;

/**

* 在MyScrollView里面的购买布局

*/

private LinearLayout mBuyLayout;

/**

* 位于顶部的购买布局

*/

private LinearLayout mTopBuyLayout;

@SuppressWarnings("deprecation")

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

myScrollView = (MyScrollView) findViewById(R.id.scrollView);//你的scrollview

mBuyLayout = (LinearLayout) findViewById(R.id.buy);//滑动的购买布局

mTopBuyLayout = (LinearLayout) findViewById(R.id.top_buy_layout);//顶部出现的购买布局

myScrollView.setOnScrollListener(this);

//当布局的状态或者控件的可见性发生改变回调的接口

findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

//

@Override

public void onGlobalLayout() {

//这一步很重要,使得上面的购买布局和下面的购买布局重合

onScroll(myScrollView.getScrollY());

}

});

}

@Override

public void onScroll(int scrollY) {//这个是回调接口调用函数,layout函数是view重绘的方法,作用就是当下面的购买布局还没有滑到顶部时,把两个布局绘制在一起,当下面的购买布局滑出屏幕 ,就把顶部的购买布局绘制在顶部不动,就实现类似效果。

int mBuyLayout2ParentTop = Math.max(scrollY, mBuyLayout.getTop());

mTopBuyLayout.layout(0, mBuyLayout2ParentTop, mTopBuyLayout.getWidth(), mBuyLayout2ParentTop + mTopBuyLayout.getHeight());

}

}

如有问题请留言,转载注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值