Android ScrollView doesn't start at top, but at the beginning of the GridView

后端 未结 12 2486
天涯浪人
天涯浪人 2020-12-23 11:19

I have a problem with a ScrollView that has inside of it a personalized GridView and other tipe of views.The first time I start the Activity, the ScrollView starts at its to

12条回答
  •  旧巷少年郎
    2020-12-23 11:44

    I found a way to give the GridView a fixed size inside ScrollView, and enable scrolling it. That allows you to see the entire ScrollView without having to scroll all elements of the GridView, and it makes more sense to me that using an ExpandableHeightGridView.

    To do so, you would have to implement a new class extending GridView and override onTouchEvent() to call requestDisallowInterceptTouchEvent(true). Thus, the parent view will leave the Grid intercept touch events.

    GridViewScrollable.java:

    package com.example;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.widget.GridView;
    
    public class GridViewScrollable extends GridView {
    
        public GridViewAdjuntos(Context context) {
            super(context);
        }
    
        public GridViewAdjuntos(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public GridViewAdjuntos(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev){
            // Called when a child does not want this parent and its ancestors to intercept touch events.
            requestDisallowInterceptTouchEvent(true);
            return super.onTouchEvent(ev);
        }
    }
    

    Add it in your layout with the characteristics and margins you want, inside a ScrollView:

    
    
        
    
    
    

    And just get it in your activity:

    GridViewScrollable myGridView = (GridViewScrollable) findViewById(R.id.myGVS);
    

    I hope it helps =)

提交回复
热议问题