重写viewgroup实现左右滑动(修改)
来源:互联网 发布:js select选中的事件 编辑:程序博客网 时间:2024/04/29 20:08
这段代码是具有以前写过的代码修改的(其实是全部更改了,不过思路是一样的),更多的解析在我的那篇上下滑动view里,原理都是一样的
package com.java.zh_app.myviews;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.Scroller;import com.java.zh_app.R;public class MyLRSlideViewGroup extends ViewGroup {private Scroller scroller;private final static int canTragviewDp = 60;private static int curr_view = 2, can_tragview = 0;private void Int_paras(Context context) {scroller = new Scroller(context);setBackgroundColor(context.getResources().getColor(R.color.app_orange_shallow_color));curr_view = 2;can_tragview = 0;}public MyLRSlideViewGroup(Context context) {super(context);Int_paras(context);}public MyLRSlideViewGroup(Context context, AttributeSet attrs) {super(context, attrs);Int_paras(context);}public MyLRSlideViewGroup(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);Int_paras(context);}@Overridepublic void computeScroll() {if (scroller.computeScrollOffset()) {getChildAt(1).layout(scroller.getCurrX(), 0,scroller.getCurrX() + (getChildAt(1).getWidth()),getHeight());getChildAt(0).layout(scroller.getCurrX() - (getChildAt(1).getWidth()), 0,scroller.getCurrX(), getHeight());postInvalidate();}super.computeScroll();}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int viewcount = getChildCount();for (int count = 0; count < viewcount; count++) {View child = getChildAt(count);int height = child.getMeasuredHeight();int wdith = child.getMeasuredWidth();child.layout(0, 0, wdith, height);}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);setMeasuredDimension(width, height);for (int i = 0; i < getChildCount(); i++) {getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);}}public void ShowMenu(float currN, int dis) {scroller.startScroll((int) currN, 0, dis, 0);invalidate();}public void OpenMenu(float currN, float currX) {scroller.startScroll((int) currN, 0,(getWidth() - (int) currN), 0);invalidate();}public void CloseMenu(float currN, float currX) {scroller.startScroll((int) currN, 0, -(int) currN, 0);invalidate();}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {super.dispatchTouchEvent(ev);switch (ev.getAction()) {case MotionEvent.ACTION_MOVE:if (can_tragview == 1) {ShowMenu(ev.getX(), 2);} else if (can_tragview == 2) {ShowMenu(ev.getX(), -2);}break;case MotionEvent.ACTION_UP:if (can_tragview != 0 && ev.getX() < getWidth() / 2) {CloseMenu(getX(), 0);can_tragview = 2;curr_view = 2;} else if (can_tragview != 0 && ev.getX() >= getWidth() / 2) {OpenMenu(ev.getX(), 0);can_tragview = 1;curr_view = 1;}break;default:break;}return true;}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {super.onInterceptTouchEvent(ev);if (ev.getX() < canTragviewDp && curr_view == 2) {can_tragview = 1;return true;} else if (ev.getX() > (getWidth() - canTragviewDp) && curr_view == 1) {can_tragview = 2;return true;} else {can_tragview = 0;}return false;}}
0 0
- 重写viewgroup实现左右滑动(修改)
- 重写viewgroup现实上下view滑动(修改版)
- android-使用ViewGroup实现左右滑动切换界面的效果
- 重写ViewPager禁止左右滑动
- android viewGroup左右滑动方法1
- 用ViewGroup实现左右滚动
- 用ViewGroup实现左右滚动
- android-使用ViewGroup实现左右滑动切换界面以及界面裂开的效果
- Android-使用ViewGroup实现左右滑动切换界面以及界面裂开的效果
- viewpager 实现左右滑动
- android 实现左右滑动
- 左右滑动---viewflipper实现
- ViewFliper实现左右滑动
- 重写ViewPager,禁止其左右滑动
- 禁止ViewPager左右滑动重写类
- 自定义ViewGroup实现水平滑动
- android 左右滑动效果实现
- Launcher实现左右循环滑动
- NSArray技巧两则
- POJ 1013 1029 判断假币
- [LeetCode] 001. Two Sum (Medium) (C++/Java/Python)
- 互联网创业创意养成记6 - 别把创意故事编成虚幻小说
- override和overload
- 重写viewgroup实现左右滑动(修改)
- 从源代码到输出
- KVC Collection Operators
- 欢迎使用CSDN-markdown编辑器
- Linux 查看脚本的执行过程
- php中heredoc的使用方法
- C/C++ 动态存储分配 malloc calloc realloc函数的用法与区别
- EPSON 机器人多任务下的互锁处理
- CodeForces 200B Drinks