在滚动视图android中刷新

时间:2017-06-19 08:56:03

标签: android android-layout

我正在尝试显示一个gridview,同时从库中选择一些照片并在片段中显示带有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>

2 个答案:

答案 0 :(得分:0)

只需将布局顶部的滑动视图替换为

    <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>

答案 1 :(得分:0)

1。请勿在{{1​​}}内使用GridViewListViewRecyclerView。使用ScrollView代替NestedScrollView

  

ScrollView就像NestedScrollView一样,但它支持充当   新旧版本上的嵌套滚动父级和子级   Android版。

2。使用ScrollView作为SwipeRefreshLayout的父级。

3。使用属性NestedScrollViewandroid:descendantFocusability="blocksDescendants"代替LinearLayout

4。将属性android:descendantFocusability="beforeDescendants"用于android:nestedScrollingEnabled="false"

更新您的布局,如下所示:

RecyclerView

希望这会有所帮助〜