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}@Overridepublic boolean onTouchEvent(MotionEvent ev) {// TODO Auto-generated method stub// 屏蔽touch事件,才能在监听其子控件的touch事件super.onTouchEvent(ev);return false;}@Overridepublic boolean onInterceptTouchEvent(MotionEvent event) {// 屏蔽touch事件传递,才能在监听其子控件的touch事件super.onInterceptTouchEvent(event);return false;}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {// TODO Auto-generated method stubsuper.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;@Overrideprotected 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() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubint 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() {@Overridepublic void onScrollChanged(int l, int t, int oldl, int oldt) {// TODO Auto-generated method stubLog.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

更多相关文章

  1. android获得控件大小,高度、宽度等
  2. Android(安卓)监听锁屏、解锁、开屏 功能代码
  3. Android(安卓)studio 单选按钮 的监听事件
  4. 百度地图locationClient.start()没反应解决办法
  5. android 监听app进入后台以及从后台进入前台
  6. Android(安卓)textview中某些字段设置点击监听
  7. Android(安卓)Gallery获取滑动停止的位置
  8. android中短信监听
  9. Android(安卓)Studio Button按钮点击事件

随机推荐

  1. 生成APK
  2. android socket通信 模拟器可以 真机不行
  3. 【Android】 Activity Lifecycle
  4. zz:Android(安卓)内存溢出(Out Of Memory
  5. Android(安卓)Studio 中创建Flutter Proj
  6. Android实现创建或升级数据库时执行语句
  7. RxJava的个人概述
  8. android sqlite使用之模糊查询数据库数据
  9. android实现点击按钮切换不同的fragment
  10. 给android imageView(图片) 添加超链接