【问题标题】:Prevent GridView from scrolling on onConfigurationChanged防止 GridView 在 onConfigurationChanged 上滚动
【发布时间】:2013-09-04 08:28:09
【问题描述】:

当活动配置发生变化时,我会应用此代码 -

public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        gridView.setNumColumns(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE ? 3:2);
        gridView.computeScroll();
    }

我希望如果GridView 在中途,它将仅从那里开始,但随着配置的更改,它会从顶部重新开始。

【问题讨论】:

  • 您的问题是(或现在是)使用gridView.setNumColumns() 重置滚动位置。我现在正在寻找解决方案。

标签: android scroll android-gridview android-scroll android-configchanges


【解决方案1】:

在清单文件中添加一行

无需重写 onConfigChanges() 方法

<activity
            android:name="yourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
 />

【讨论】:

    【解决方案2】:

    获取一个标志并将其设置为 false 覆盖方法名称 onConfigurationChanged 并在此方法中设置标志为 true 并检查标志是否为 true 然后禁用滚动,然后再次为 false 你还需要在清单中设置它 代码片段如下

    private boolean flag=false;
    
    
    
          @Override
            public void onConfigurationChanged(Configuration newConfig) {
                // TODO Auto-generated method stub
                Log.d("ONCONFIGCHANGE", "CALLED" );
                flag = true;
    disableScroll();
                super.onConfigurationChanged(newConfig);
            }
    

    对于清单,请执行以下操作

    android:configChanges="orientation"
    

    并在 onconfigchange 中使用以下方法

    private void disableScroll()
    {
    gridview.setOnTouchListener(new OnTouchListener(){
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_MOVE){
                return true;
            }
            return false;
        }
    
    });
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多