码迷,mamicode.com
首页 > 移动开发 > 详细

Android 讲义第二版 ViewSwitcher + GestureDetector 练习

时间:2015-07-29 14:17:28      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

添加手势监听:

GestureDetector detector;// 手势侦听
detector = new GestureDetector(getApplicationContext(),new myGestureListener());

手势侦听 在 onFling 中指定屏幕滑动效果和展示图像:

class myGestureListener implements OnGestureListener {
        // 当触碰事件按下时触发该方法
        @Override
        public boolean onDown(MotionEvent event) {
            return false;
        }

        // 用户在触摸屏上拖过时触发该方法
        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2,
                float velocityX, float velocityY) {
            ((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
            if (event2.getX() - event1.getX() > FLIP_DISTANCE) {
                if (screenNo > 0) {
                    screenNo--;
                    // 添加屏幕动画切换效果
                    viewSwitcher.setInAnimation(animations[0]);
                    viewSwitcher.setInAnimation(animations[3]);
                    viewSwitcher.showPrevious();
                    return true;
                } else {
                    Toast.makeText(getApplicationContext(), "已无更多页面",
                            Toast.LENGTH_LONG).show();
                }
            } else {
                if (screenNo < screenCount - 1) {
                    screenNo++;
                    viewSwitcher.setInAnimation(animations[1]);
                    viewSwitcher.setInAnimation(animations[2]);
                    viewSwitcher.showNext();
                    return true;
                } else {
                    Toast.makeText(getApplicationContext(), "已无更多页面",
                            Toast.LENGTH_LONG).show();
                }
            }
            return false;
        }

        // 用户在屏幕上长按时触发该方法
        @Override
        public void onLongPress(MotionEvent event) {

        }

        // 用户在屏幕上滚动时触发该方法
        @Override
        public boolean onScroll(MotionEvent event1, MotionEvent event2,
                float velocityX, float velocityY) {
            return false;
        }

        // 用户在触摸屏上按下,而且还未移动和松开时触发该方法
        @Override
        public void onShowPress(MotionEvent arg0) {

        }

        // 用户在触屏上的轻击事件将触发该方法
        @Override
        public boolean onSingleTapUp(MotionEvent arg0) {
            return false;
        }

    }

进入的第一个界面展示 代码:

public void next(View v) {
        if (screenNo == -1) {
            screenNo++;
            ((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
            viewSwitcher.showNext();
        } else {
            Toast.makeText(getApplicationContext(), "已无更多页面", Toast.LENGTH_LONG)
                    .show();
        }
    }

定义BaseAdapter:

private BaseAdapter adapter = new BaseAdapter() {
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public DataItem getItem(int position) {
            return total.get(screenNo * Number_Per_Screen + position);
        }

        @Override
        public int getCount() {
            if (screenNo == screenCount - 1
                    && total.size() % Number_Per_Screen != 0) {
                return total.size() % Number_Per_Screen;
            }
            return Number_Per_Screen;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (convertView == null) {
                view = layoutInflater.inflate(R.layout.labelicon, null);
            }
            ImageView imageview = (ImageView) view.findViewById(R.id.imageview);
            imageview.setImageDrawable(getItem(position).drawable);
            TextView textView = (TextView) view.findViewById(R.id.textview);
            textView.setText(getItem(position).DataName);
            return view;
        }
    };

此处只是对 疯狂Android 讲义 中ViewSwitcher 的 手势 添加 做的一个小demo,本人处于初学阶段,如有不对的地方,请各位多多指正!!


Android 讲义第二版 ViewSwitcher + GestureDetector 练习

标签:

原文地址:http://my.oschina.net/leov1/blog/485101

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!