Scrollview vertical and horizontal in android

前端 未结 11 760
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 05:30

I\'m really tired looking for a solution for vertical and horizontal Scrollview.

I read that there are not any views/layouts in the framework which implement this fe

11条回答
  •  盖世英雄少女心
    2020-11-22 05:52

    Mixing some of the suggestions above, and was able to get a good solution:

    Custom ScrollView:

    package com.scrollable.view;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.widget.ScrollView;
    
    public class VScroll extends ScrollView {
    
        public VScroll(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public VScroll(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public VScroll(Context context) {
            super(context);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return false;
        }
    }
    

    Custom HorizontalScrollView:

    package com.scrollable.view;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.widget.HorizontalScrollView;
    
    public class HScroll extends HorizontalScrollView {
    
        public HScroll(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public HScroll(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public HScroll(Context context) {
            super(context);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return false;
        }
    }
    

    the ScrollableImageActivity:

    package com.scrollable.view;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.HorizontalScrollView;
    import android.widget.ScrollView;
    
    public class ScrollableImageActivity extends Activity {
    
        private float mx, my;
        private float curX, curY;
    
        private ScrollView vScroll;
        private HorizontalScrollView hScroll;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            vScroll = (ScrollView) findViewById(R.id.vScroll);
            hScroll = (HorizontalScrollView) findViewById(R.id.hScroll);
    
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float curX, curY;
    
            switch (event.getAction()) {
    
                case MotionEvent.ACTION_DOWN:
                    mx = event.getX();
                    my = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    curX = event.getX();
                    curY = event.getY();
                    vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    mx = curX;
                    my = curY;
                    break;
                case MotionEvent.ACTION_UP:
                    curX = event.getX();
                    curY = event.getY();
                    vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                    break;
            }
    
            return true;
        }
    
    }
    

    the layout:

    
    
        
            
                
            
        
    
    
    

提交回复
热议问题