import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ScrollView; public class MyScrollView extends ScrollView { // 滚动监听接口 private OnScrollChangedListeneer onScrollChangedListeneer; public MyScrollView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub // 屏蔽touch事件,才能在监听其子控件的touch事件 super.onTouchEvent(ev); return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { // 屏蔽touch事件传递,才能在监听其子控件的touch事件 super.onInterceptTouchEvent(event); return false; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { // TODO Auto-generated method stub super.onScrollChanged(l, t, oldl, oldt); if (onScrollChangedListeneer != null) { onScrollChangedListeneer.onScrollChanged(l, t, oldl, oldt); } } // 滚动事件监听,获取滚动的距离,用户处理一些其他事 public interface OnScrollChangedListeneer { public void onScrollChanged(int l, int t, int oldl, int oldt); } public void setOnScrollChangedListeneer( OnScrollChangedListeneer onScrollChangedListeneer) { this.onScrollChangedListeneer = onScrollChangedListeneer; } }
import android.app.Activity; import android.content.Context; import android.graphics.Point; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.WindowManager; import android.widget.RelativeLayout.LayoutParams; import com.mb.door.MyScrollView.OnScrollChangedListeneer; import com.ywl5320.scrollanima.R; public class MainActivity extends Activity { private View layout_content; private MyScrollView scrollView; private int offsetsum = 0;// 总的手指滑动距离 private Point point = new Point(); private View layout_sliding; private boolean isOpen = false; // true:显示详情 false 反之 private int screenHeight = 0; private int handlerHeight = 100;// 把手的高度,该高度最好动态获取 private int threshold=300; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; initViews(); } private void initViews(){ handlerHeight = dip2px(context, 100);// 把手的高度,该高度最好动态获取 int statusBarHeight=getStatusBarHeight(); scrollView = (MyScrollView) findViewById(R.id.scrollView); layout_content = findViewById(R.id.layout_content); layout_sliding = findViewById(R.id.layout_sliding); // 设置滑动层为屏幕高度 LayoutParams lp = (LayoutParams) layout_content.getLayoutParams(); screenHeight = getScreenHeight(); lp.height = screenHeight - statusBarHeight; layout_content.setLayoutParams(lp); // 设置详细层的高度:等于屏幕高度-状态栏高度-阴影提示高度 LayoutParams lp2 = (LayoutParams) layout_sliding.getLayoutParams(); lp2.height = screenHeight - statusBarHeight- handlerHeight; layout_sliding.setLayoutParams(lp2); // 为上层添加touch事件,控制详情页显示隐藏 layout_content.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); int offsety = 0; int y = 0; switch (action) { case MotionEvent.ACTION_DOWN: point.y = (int) event.getRawY(); offsetsum = 0; break; case MotionEvent.ACTION_MOVE: y = (int) event.getRawY(); offsety = y - point.y; offsetsum += offsety; point.y = (int) event.getRawY(); scrollView.scrollBy(0, -offsety); break; case MotionEvent.ACTION_UP: if(offsetsum==0){ return true; } if (offsetsum > 0) {// offsetsum大于0时是往下拉 if (offsetsum > threshold) { close(); } else { open(); } } else {// offsetsum小于0时是往上拉 if (offsetsum < -threshold) { open(); } else { close(); } } break; } return true; } }); scrollView.setOnScrollChangedListeneer(new OnScrollChangedListeneer() { @Override public void onScrollChanged(int l, int t, int oldl, int oldt) { // TODO Auto-generated method stub Log.i("tag", l + "--" + t + "--" + oldl + "--"+ oldt); } }); } private void open() { scrollView.smoothScrollTo(0, screenHeight - handlerHeight); isOpen = true; } private void close() { scrollView.smoothScrollTo(0, 0); isOpen = false; } public void toggle(){ if(isOpen){ close(); }else{ open(); } } /** * 获取屏幕高度 * * @return */ public int getScreenHeight() { WindowManager wManager = (WindowManager) getApplicationContext() .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wManager.getDefaultDisplay().getMetrics(dm); return dm.heightPixels; } /** * dip转换为px * * @param context * @param dipValue * @return */ public int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } /** * 获取状态栏高度 * * @return */ public int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <com.mb.door.MyScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/layout_content" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="@android:color/holo_blue_dark" > </FrameLayout> <TextView android:id="@+id/handler" android:layout_width="match_parent" android:layout_height="100dip" android:layout_gravity="bottom" android:background="#aa000000" android:gravity="center" android:text="上滑查看详情" android:textColor="#ffffff" /> </FrameLayout> <FrameLayout android:id="@+id/layout_sliding" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/layout_content" android:background="@android:color/white" > </FrameLayout> </RelativeLayout> </com.mb.door.MyScrollView> </RelativeLayout>
事实上,同样的效果可以使用 ViewDragHelper实现
http://gundumw100.iteye.com/blog/2114716