android 高仿美团,Android 仿美团、大众点评团购详情UI
在scrollview 上滑固定某一控件(美團團購詳情UI)文中介紹了怎么用touchlistener實現(xiàn)類似上滑停住的效果,但是這種方法存在一個明顯的bug,就是在內(nèi)容比較多的時候, 大部分人都是以滑動方式查看內(nèi)容,而不是touch的方式,這就會導(dǎo)致最上面的滑塊出現(xiàn)不及時,或者延后的現(xiàn)象,這里介紹一個全新的方法去實現(xiàn)類似效果,可以很好的解決以上問題.
目前在scrollview中沒有onscrolllistener所以需要自己去實現(xiàn),先復(fù)寫一個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);
}
/**
* 設(shè)置滾動接口
* @param onScrollListener
*/
public void setOnScrollListener(OnScrollListener onScrollListener) {
this.onScrollListener = onScrollListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {//滑動改變就會實時調(diào)用
super.onScrollChanged(l, t, oldl, oldt);
if(onScrollListener != null){
onScrollListener.onScroll(t);
}
}
/**
*
* 滾動的回調(diào)接口
*
*/
public interface OnScrollListener{
/**
* 回調(diào)方法, 返回MyScrollView滑動的Y方向距離
* @param scrollY
* 、
*/
public void onScroll(int scrollY);
}
}
然后就在mainactivity中調(diào)用:
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);//頂部出現(xiàn)的購買布局
myScrollView.setOnScrollListener(this);
//當(dāng)布局的狀態(tài)或者控件的可見性發(fā)生改變回調(diào)的接口
findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//
@Override
public void onGlobalLayout() {
//這一步很重要,使得上面的購買布局和下面的購買布局重合
onScroll(myScrollView.getScrollY());
}
});
}
@Override
public void onScroll(int scrollY) {//這個是回調(diào)接口調(diào)用函數(shù),layout函數(shù)是view重繪的方法,作用就是當(dāng)下面的購買布局還沒有滑到頂部時,把兩個布局繪制在一起,當(dāng)下面的購買布局滑出屏幕 ,就把頂部的購買布局繪制在頂部不動,就實現(xiàn)類似效果。
int mBuyLayout2ParentTop = Math.max(scrollY, mBuyLayout.getTop());
mTopBuyLayout.layout(0, mBuyLayout2ParentTop, mTopBuyLayout.getWidth(), mBuyLayout2ParentTop + mTopBuyLayout.getHeight());
}
}
如有問題請留言,轉(zhuǎn)載注明出處。
總結(jié)
以上是生活随笔為你收集整理的android 高仿美团,Android 仿美团、大众点评团购详情UI的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2003卡在应用计算机设置,计算机操作与
- 下一篇: C# GridView中DataForm