简体   繁体   English

Android Textview 慢,甚至根本不工作

[英]Android Textview slow and even doesn't work at all

I made a very simple app which displays current date in day of month inside a Textview at 120dp text size.我制作了一个非常简单的应用程序,它在Textview中以 120dp 的文本大小显示当前日期。 I want to increment and decrement the date by one day every swipe right or swipe left gesture.我想在每次向右滑动或向左滑动手势时将日期递增和递减一天。

My code worked but it worked very slowly especially the swipe left gesture to decrement the date.我的代码工作,但工作非常缓慢,尤其是向左滑动手势来减少日期。 Sometimes it refused to work.有时它拒绝工作。

Here is my code:这是我的代码:

Calendar c;
Int day,month,year;

public abstract class OnSwipeTouchListener implements View.OnTouchListener {

        public final GestureDetector gestureDetector;

        public OnSwipeTouchListener (Context ctx){
            gestureDetector = new GestureDetector(ctx, new GestureListener());
        }

        private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;

            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                boolean result = false;
                try {
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                        }
                        result = true;
                    }
                    else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                    result = true;

                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
        }

        public void onSwipeRight() {
        }

        public void onSwipeLeft() {
        }

        public void onSwipeTop() {
        }

        public void onSwipeBottom() {
        }
    }




textView.setOnTouchListener(new OnSwipeTouchListener(this) {
            public void onSwipeTop() {}

            public void onSwipeRight() {
                c.add(Calendar.DATE, 1);

                day = c.get(Calendar.DAY_OF_MONTH);
                month = c.get(Calendar.MONTH) +1;
                year = c.get(Calendar.YEAR);
                textView.setText(Integer.toString(day));



            }
            public void onSwipeLeft() {
                c.add(Calendar.DATE, -1);

                day = c.get(Calendar.DAY_OF_MONTH);
                month = c.get(Calendar.MONTH) +1;
                year = c.get(Calendar.YEAR);
                textView.setText(Integer.toString(day));

            }
            public void onSwipeBottom() {
                textView.setText("DOWN");
            }

            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        }); 

Why is the performance of this code so poor?为什么这段代码的性能这么差?

Use RecyclerView with horizontal orientation instead TextView...may be it will give much smooth & great performance then applying Gesture tracker on TextView..使用具有水平方向的 RecyclerView 代替 TextView ... 可能会提供更流畅和出色的性能,然后在 TextView 上应用手势跟踪器。

Thank you guys.感谢你们。 I managed to solve this issue myself, just set listener to the layout and everything works like a charm.我自己设法解决了这个问题,只需将监听器设置为布局,一切都像魅力一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM