android - 数据集更改后如何默认防止 gridview 滚动

标签 android gridview

我有一个包含 ScrollView 的 Activity ,并且在 ScrollView 中还有一个 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: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();
    }
}

为什么我使用自定义 gridview 是为了确保 gridview 可以扩展到它的最大高度(检查 this )。

现在一旦加载了 Activity ,我将从服务器加载数据,然后调用:

gridAdapter.notifyDatasetChanged();

然后 Activity 将滚动到 GridView ,这意味着用户看不到 GridView 上方的内容。

我试过:

mScrollView.scrollTo(0,mScrollView.getBottom());

但它不起作用。

有解决办法吗?

最佳答案

问题:

GridView 正在自动滚动。

原因:

加载屏幕时,它会从其 ViewHierarchy 中找到第一个可聚焦 View ,并将焦点设置到该 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>

来自java代码,

在你的 Activity 的 onCreate() 方法中

RelativeLayout rlContainer = (RelativeLayout) findViewById(R.id.rlContainer);
rlContainer.requestFocus();

关于android - 数据集更改后如何默认防止 gridview 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25357568/

相关文章:

android - 检索使用push()保存的Firebase数据

android - 以编程方式启用/禁用 Activity

java - MapView占据了我的整个屏幕

java - 将 XML 与 Activity 的 setContentView() 一起使用

java - GridView android suffle 项目滚动时

java - fragment 上的自定义 GridView 上的 NullPointerException

java - 在android编码中覆盖com.android.View的onScroll方法?

gridview - 如何在 Yii CGridView 中处理选定的行?

android - 安卓双向滚动

android - 使用 Spinner 处理 GridView 单元格中的点击事件