简体   繁体   中英

How to make LinearLayout's X axis move under my finger?

I need to know what i can do to make a LinearLayout move together with my finger but i want to move only the X axis and not the Y.

And after finished moving, when you "finger up" the LinearLayout back to original X position.

How i can do this?

assume ll is the name of your linear layout:

    ll.setOnTouchListener(new OnTouchListener() {

        float lastX;

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            View item = v;

            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                item.scrollBy((int) (event.getX()-lastX), 0);
                lastX = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                item.scrollTo(0, 0);

            }
            return false;
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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