Scrollview vertical and horizontal in android

前端 未结 11 765
爱一瞬间的悲伤
爱一瞬间的悲伤 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:47

    since the Two Dimensional Scrollview link is dead I could not get it so I created my own component. It handles flinging and works properly for me. The only restriction is that wrap_content might not work properly for that component.

    https://gist.github.com/androidseb/9902093

    0 讨论(0)
  • 2020-11-22 05:51

    I use it and works fine:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView android:id="@+id/ScrollView02" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                xmlns:android="http://schemas.android.com/apk/res/android">
    <HorizontalScrollView android:id="@+id/HorizontalScrollView01" 
                          android:layout_width="wrap_content" 
                          android:layout_height="wrap_content">
    <ImageView android:id="@+id/ImageView01"
               android:src="@drawable/pic" 
               android:isScrollContainer="true" 
               android:layout_height="fill_parent" 
               android:layout_width="fill_parent" 
               android:adjustViewBounds="true">
    </ImageView>
    </HorizontalScrollView>
    </ScrollView>
    

    The source link is here: Android-spa

    0 讨论(0)
  • 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:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <com.scrollable.view.VScroll android:layout_height="fill_parent"
            android:layout_width="fill_parent" android:id="@+id/vScroll">
            <com.scrollable.view.HScroll android:id="@+id/hScroll"
                android:layout_width="fill_parent" android:layout_height="fill_parent">
                <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/bg"></ImageView>
            </com.scrollable.view.HScroll>
        </com.scrollable.view.VScroll>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 05:57

    use this way I tried this I fixed it

    Put All your XML layout inside

    <android.support.v4.widget.NestedScrollView 
    

    I explained this in this link vertical recyclerView and Horizontal recyclerview scrolling together

    0 讨论(0)
  • 2020-11-22 05:58

    Try this

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView android:id="@+id/Sview" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android">
    <HorizontalScrollView 
       android:id="@+id/hview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    <ImageView .......
     [here xml code for image]
    </ImageView>
    </HorizontalScrollView>
    </ScrollView>
    
    0 讨论(0)
  • 2020-11-22 06:01

    Since this seems to be the first search result in Google for "Android vertical+horizontal ScrollView", I thought I should add this here. Matt Clark has built a custom view based on the Android source, and it seems to work perfectly: Two Dimensional ScrollView

    Beware that the class in that page has a bug calculating the view's horizonal width. A fix by Manuel Hilty is in the comments:

    Solution: Replace the statement on line 808 by the following:

    final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
    


    Edit: The Link doesn't work anymore but here is a link to an old version of the blogpost.

    0 讨论(0)
提交回复
热议问题