【问题标题】:pull to refresh in scroll view android在滚动视图android中拉刷新
【发布时间】:2017-06-19 08:56:03
【问题描述】:

我试图在从图库中选择一些照片并在片段中显示带有 recyclerview 的列表时显示一个网格视图。如果我不使用滑动刷新布局,我的布局运行良好。你能解决这个为什么在使用滑动刷新布局时这个布局不起作用的原因吗?

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.systechdigital.webadeal.NewsfeedFragment"
    android:orientation="vertical"
    android:background="#969696"

    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#023956"
        >

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:src="@drawable/globe"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/notificationButtonId"
            android:background="?android:attr/selectableItemBackground"
            android:paddingRight="10dp"
            />

    </RelativeLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:paddingBottom="10dp"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true"
            >

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Share you thoughts here..."
                android:paddingLeft="10dp"
                android:background="@drawable/edittext_status_background"
                android:padding="20dp"
                android:elevation="5dp"
                android:id="@+id/editTextWritePostId"

                />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:fitsSystemWindows="true"
                android:background="#f9f9f9"
                >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Post"
                    android:textColor="#ffffff"
                    android:background="#1470a6"
                    android:id="@+id/postButtonId"
                    />

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/checkInButtonId"
                    android:layout_toLeftOf="@+id/smileyButtonId"
                    android:layout_centerInParent="true"
                    android:src="@drawable/ic_checkin"
                    android:background="@android:color/transparent"
                    android:layout_marginRight="3dp"
                    />

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/smiley"
                    android:layout_toLeftOf="@+id/imageUploadButtonId"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="10dp"
                    android:id="@+id/smileyButtonId"
                    />

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/camera"
                    android:layout_toLeftOf="@+id/videoUploadButtonId"
                    android:layout_centerInParent="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginRight="10dp"
                    android:id="@+id/imageUploadButtonId"
                    />
            </RelativeLayout>

            <GridView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/gridViewId"
                android:layout_below="@+id/postButtonId"
                android:numColumns="auto_fit"
                android:columnWidth="100dp"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:layout_marginTop="5dp"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/underline"
                android:minHeight="0dp"
                />

            **<android.support.v4.widget.SwipeRefreshLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/swipeRefreshLayoutId"
                >**

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/recyclerviewId_newsFeed"
                    android:nestedScrollingEnabled="true"
                    />

            **</android.support.v4.widget.SwipeRefreshLayout>**

        </LinearLayout>

</ScrollView>
</LinearLayout>

【问题讨论】:

  • 看到这个answer,这一定会对你有所帮助。
  • 不工作。,,.,
  • 使用 NestedScrollView 而不是 ScrollView。
  • 不工作。,.,.

标签: android android-layout


【解决方案1】:

1. 不要在ScrollView 中使用GridViewListViewRecyclerView。使用NestedScrollView 而不是你的ScrollView

NestedScrollViewScrollView 一样,但它支持充当 新旧版本中的嵌套滚动父项和子项 安卓系统。

2. 使用SwipeRefreshLayout 作为NestedScrollView 的父级。

3. 使用属性android:descendantFocusability="blocksDescendants"LinearLayout 而不是android:descendantFocusability="beforeDescendants"

4. 使用属性android:nestedScrollingEnabled="false"RecyclerView

如下更新您的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#969696">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#023956">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:src="@drawable/ic_group"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/notificationButtonId"
            android:background="?android:attr/selectableItemBackground"
            android:paddingRight="10dp" />

    </RelativeLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipeRefreshLayoutId">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:paddingBottom="10dp"
                android:descendantFocusability="blocksDescendants">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Share you thoughts here..."
                    android:paddingLeft="10dp"
                    android:padding="20dp"
                    android:elevation="5dp"
                    android:id="@+id/editTextWritePostId" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:fitsSystemWindows="true"
                    android:background="#f9f9f9">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Post"
                        android:textColor="#ffffff"
                        android:background="#1470a6"
                        android:id="@+id/postButtonId" />

                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/checkInButtonId"
                        android:layout_toLeftOf="@+id/smileyButtonId"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_action_cart"
                        android:background="@android:color/transparent"
                        android:layout_marginRight="3dp" />

                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/ic_play"
                        android:layout_toLeftOf="@+id/imageUploadButtonId"
                        android:layout_centerInParent="true"
                        android:layout_marginRight="10dp"
                        android:id="@+id/smileyButtonId" />

                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/ic_camera"
                        android:layout_centerInParent="true"
                        android:layout_alignParentEnd="true"
                        android:layout_marginRight="10dp"
                        android:id="@+id/imageUploadButtonId" />
                </RelativeLayout>

                <GridView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/gridViewId"
                    android:layout_below="@+id/postButtonId"
                    android:numColumns="auto_fit"
                    android:columnWidth="100dp"
                    android:verticalSpacing="10dp"
                    android:horizontalSpacing="10dp"
                    android:layout_marginTop="5dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:minHeight="0dp" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerviewId_newsFeed"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:nestedScrollingEnabled="false" />

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

希望对你有所帮助~

【讨论】:

  • 应用我的更改后的当前状态是什么?
  • 和以前一样的输出。
  • 你能再帮我解决一个问题,显示“OutOfMemoryError: 无法分配 20155404 字节分配,8428224 个可用字节和 8MB 直到 OOM”
  • 您能否尝试将您的GridView 替换为RecyclerView(with GridLayoutManager)。建议在NestedScrollView内部使用时使用RecyclerView
  • "OutOfMemoryError: 无法分配 20155404 字节分配,其中 8428224 个可用字节和 8MB 直到 OOM">> 它的发生是由于您的图像网格。使用 3rd 方库 Glide 或 Picasso 来有效地加载图像。
【解决方案2】:

只需像这样替换布局顶部的滑动视图

    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayoutId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#969696"
        android:orientation="vertical"
        tools:context="com.systechdigital.webadeal.NewsfeedFragment"

        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#023956">

            <ImageButton
                android:id="@+id/notificationButtonId"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentTop="true"
                android:background="?android:attr/selectableItemBackground"
                android:paddingRight="10dp"
                android:src="@drawable/globe" />

        </RelativeLayout>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true"
                android:orientation="vertical"
                android:paddingBottom="10dp">

                <EditText
                    android:id="@+id/editTextWritePostId"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edittext_status_background"
                    android:elevation="5dp"
                    android:hint="Share you thoughts here..."
                    android:padding="20dp"
                    android:paddingLeft="10dp"

                    />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="#f9f9f9"
                    android:fitsSystemWindows="true">

                    <Button
                        android:id="@+id/postButtonId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="#1470a6"
                        android:text="Post"
                        android:textColor="#ffffff" />

                    <ImageButton
                        android:id="@+id/checkInButtonId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:layout_marginRight="3dp"
                        android:layout_toLeftOf="@+id/smileyButtonId"
                        android:background="@android:color/transparent"
                        android:src="@drawable/ic_checkin" />

                    <ImageButton
                        android:id="@+id/smileyButtonId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:layout_marginRight="10dp"
                        android:layout_toLeftOf="@+id/imageUploadButtonId"
                        android:background="@drawable/smiley" />

                    <ImageButton
                        android:id="@+id/imageUploadButtonId"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_centerInParent="true"
                        android:layout_marginRight="10dp"
                        android:layout_toLeftOf="@+id/videoUploadButtonId"
                        android:background="@drawable/camera" />
                </RelativeLayout>

                <GridView
                    android:id="@+id/gridViewId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/postButtonId"
                    android:layout_marginTop="5dp"
                    android:columnWidth="100dp"
                    android:horizontalSpacing="10dp"
                    android:numColumns="auto_fit"
                    android:verticalSpacing="10dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/underline"
                    android:minHeight="0dp" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerviewId_newsFeed"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:nestedScrollingEnabled="true" />


            </LinearLayout>

        </ScrollView>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

【讨论】:

  • 表示它不工作。,我希望当我滚动时,所有内容都会向上/向下滚动但不工作
猜你喜欢
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多