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

后端 未结 12 2483
天涯浪人
天涯浪人 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条回答
  •  梦毁少年i
    2020-12-23 11:46

    As a follow-up I'll share my pain as well. In my app I have a RecyclerView inside a NestedScrollView inside a CoordinatorLayout:

    
      
    
         .......
    
         
    
                
    
      
    
    

    Of course upon opening the activity, the page scrolled to include the recyclerView in the middle. None of the answers above worked, so I came up with the following solution:

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
      ....
      content.setOnScrollChangeListener( new NestedScrollView.OnScrollChangeListener() {
        @Override
        public void onScrollChange( NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY ) {
          Rect scrollBounds = new Rect();
          v.getHitRect( scrollBounds );
          if( header.getLocalVisibleRect( scrollBounds ) ){
            if( View.VISIBLE != recyclerView.getVisibility() ){
              recyclerView.setVisibility( View.VISIBLE );
              fillRecyclerViewSomehow();
            }
          }
        }
      }
    }
    
    @Override
    protected void onResume() {
      ...
      recyclerView.setVisibility( View.GONE ); // this effectively suppresses the focusability
    }
    

    HTH

提交回复
热议问题