数据集更改后,如何在默认情况下阻止gridview滚动

hgu*_*ser 2 android gridview

我有一个活动,其中包含一个ScrollView,我也有一个GridView内部ScrollView的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/root">

    <RelativeLayout
            android:padding="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        .........
        <com.test.android.view.ScrollableGridView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:numColumns="auto_fit"
                android:columnWidth="100dp"
                android:verticalSpacing="2dp"
                android:focusable="false"
                android:clickable="false">
        </com.test.android.view.ScrollableGridView>
    </RelativeLayout>
</ScrollView>

public class ScrollableGridView extends GridView {
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我使用custome gridview是为了确保gridview可以扩展到它的最大高度(检查这个).

现在加载活动后,我将从服务器加载数据,然后调用:

gridAdapter.notifyDatasetChanged();
Run Code Online (Sandbox Code Playgroud)

然后活动将滚动到网格视图,这意味着用户无法看到gridview上方的内容.

我试过了:

mScrollView.scrollTo(0,mScrollView.getBottom());
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

有什么想法解决它吗?

Meh*_*sar 6

问题:

GridView 正在自动滚动.

原因:

加载屏幕时,它会the first focusable View从中找到ViewHierarchy并将焦点设置为该屏幕View.

解:

您可以将焦点设置为其他焦点,View以便Android GridView首先不对焦,因此它不会自动滚动.

例:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/root">

<RelativeLayout
        android:padding="10dp"
        android:id="@+id/rlContainer"           //you may set id and requestfocus from java code as well
        android:focusable="true"                //add this 
        android:focusableInTouchMode="true"     //add this
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    .........                                     //you can also add focusable,focusableInTouchMode to any other view before the GridView
    <com.test.android.view.ScrollableGridView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="auto_fit"
            android:columnWidth="100dp"
            android:verticalSpacing="2dp"
            android:focusable="false"
            android:clickable="false">
    </com.test.android.view.ScrollableGridView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

从java代码,

在Activity的onCreate()方法中

RelativeLayout rlContainer = (RelativeLayout) findViewById(R.id.rlContainer);
rlContainer.requestFocus();
Run Code Online (Sandbox Code Playgroud)