在CoordinatorLayout中的RelativeLayout无法滚动。

3

我在我的CoordinatorLayout中有一个RelativeLayout(如下所示)。请注意,在RelativeLayout中,我有一个RecyclerView

然而,当我滚动时,我只能在RecyclerView上滚动,并且仅屏幕上的RecyclerView部分会实际滚动。如果我尝试在RecyclerView上面的布局上滚动,则根本不会滚动屏幕。下面是我制作的说明图:

Demo

所以我的问题是,为什么我不能在内部RelativeLayout上滚动?我应该以不同的方式组织我的布局吗?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <RelativeLayout
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="250dp">

                // some imageviews and textviews

            </RelativeLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/header" />

        </RelativeLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_drawer"
        app:menu="@menu/menu_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

我该怎么修复这个问题?
2个回答

0

RelativeLayout如果不在可滚动视图中,则不能滚动。
尝试将你的RelativeLayout作为RecyclerView的头部。

在这个例子中,你将能够滚动它的内容。

<ScrollView 
     ...
     ...>
         <!-- Scrollable Contents -->
</ScrollView>

在你的情况下,我认为最好的方法是让你的RecyclerView占据整个屏幕,然后分配它的标题。请参考此链接这个链接。有很多类似的资源,你可以通过谷歌搜索来找到它们。

0

好的,可以使用单个recyclerview,并将您的标题relativelayout放置在recyclerview的一个项目中。

也许您熟悉getItemViewType方法,该方法用于确定recyclerview项目的项目类型。

使用您将要决定项类型的数据集添加数据。请考虑以下示例...

    public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int ITEM_ROW = 1;
    private final int ITEM_HEADER = 2;

    // Will be your dataset which will be containing first item as relativeLayout payload.
    private ArrayList<Object> mData;

    public MyRecyclerViewAdapter(ArrayList<Object> mData) {
        this.mData = mData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh = null;
        if (viewType == ITEM_ROW) {
            // Inflate your row item of recyclerview here...

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_checkout_review_cart_item, parent, false);
            vh = new ItemHolder(view);
        } else if (viewType == ITEM_HEADER) {

            // Inflate your relativeLayout here...

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_order_detail_grand_total_item, parent, false);
            vh = new RelativeLayoutHolder(view);
        }
        return vh;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemHolder) {

            // Bind item of recyclerview here...

        } else if (holder instanceof RelativeLayoutHolder) {

            // Bind data of your relativeLayout

        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        // Decide type of an item using payload instances..
        Object obj = mData.get(position);
        if (obj instanceof ItemInfo) {
            return ITEM_ROW;
        } else if (obj instanceof RelativeLayoutInfo) {
            return ITEM_HEADER;
        } else {
            return -1;
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {

        ItemHolder(View itemView) {
            super(itemView);
        }
    }

    class RelativeLayoutHolder extends RecyclerView.ViewHolder {

        RelativeLayoutHolder(View itemView) {
            super(itemView);
        }
    }
}

您的数据集负载将决定itemType。将插入两种类型的Pojo到您的数据集中 - RelativeLayoutInfo和ItemInfo。例如:

public class ItemInfo implements Serializable {

     // Consider adding object and getter setter here
}

并且

public class RelativeLayoutInfo implements Serializable {

   // Consider adding object and getter setter here
}

完成啦!你的RelativeLayout已经被添加到RecyclerView里面了。 祝编程愉快!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接