用户滑动时展开/折叠布局

时间:2017-11-03 10:26:17

标签: android android-layout android-recyclerview

在Android项目中,我有一个活动,其中包含(从上到下)标题,RecyclerViewRelativeLayout

代码示例:

<android.support.design.widget.CoordinatorLayout ...>
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        ...
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView 
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

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

这是我想要的行为:RelativeLayout默认为“折叠”,即在RecyclerView下。当用户向上滑动时,RelativeLayout会展开以使用整个屏幕,并在向下滑动时再次折叠。

这样做的正确方法是什么?我希望RelativeLayout在折叠时低于RecyclerView,在展开时超过它。我尝试动态设置元素之间的依赖关系但无法设法这样做。

感谢您提供有关如何组织活动以获得此类行为的任何帮助或建议。

修改

This is the XML structure :
<CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </AppBarLayout>

    <RecyclerView
        android:id="@+id/top_list"   
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/app_bar"
        android:layout_above="@+id/relativelayout_footer" />

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true">
        <TextView ...>
        <RecyclerView 
            android:id="@+id/included_list"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:visibility="gone"/>
    </RelativeLayout>

</RelativeLayout>

</CoordinatorLayout>

听众:

    top_list.setVisibility(playlistOpened ? View.VISIBLE : View.GONE);
    app_bar.setVisibility(playlistOpened ? View.VISIBLE : View.GONE);
    included_list.setVisibility(playlistOpened ? View.GONE : View.VISIBLE);

    Transition cb = new ChangeBounds();
    cb.setDuration(1000);
    TransitionManager.beginDelayedTransition(slider, cb);

    playlistOpened = ! playlistOpened;

如您所见,我切换元素的可见性以重新组织视图。当included_list设置为VISIBLE时,它会推升TextView

唯一的问题是top_listapp_bar在被RelativeLayout覆盖之前消失了。我尝试向Listener添加Transition并在转换结束时执行这些操作,但它不起作用(我猜是因为它们在开头没有设置为GONE,所以RelativeLayout无法向上移动)。知道怎么做吗?也许我应该为它开另一个问题?

1 个答案:

答案 0 :(得分:1)

好的,最后一次尝试。我回到RelativeLayout并在RecyclerView周围添加了另一个来修复其滚动问题。然而,转换变得棘手(并且听众变得复杂)。为了防止RecyclerViewRelativeLayout内部完全覆盖后者TextView之前弹出,我必须包含一个向RecyclerView过渡的渐变过渡。希望这适合你。

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/rlwithrecyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/rlwithtextview"
            android:layout_alignParentTop="true">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rlwithtextview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:id="@+id/textview"
                android:text="Just a test" />
        </RelativeLayout>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

onClick()

@Override
public void onClick(View v) {
    RelativeLayout rlWithRecyclerView = findViewById(R.id.rlwithrecyclerview);
    RelativeLayout.LayoutParams layoutParamsRlWithRecyclerView;

    RelativeLayout rlWithTextView = findViewById(R.id.rlwithtextview);
    RelativeLayout.LayoutParams layoutParamsRlWithTextView;

    if (rlWithTextView.getLayoutParams().height == MATCH_PARENT) {
        layoutParamsRlWithTextView = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        layoutParamsRlWithRecyclerView = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        layoutParamsRlWithRecyclerView.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParamsRlWithRecyclerView.addRule(RelativeLayout.ABOVE, R.id.rlwithtextview);
    } else {
        layoutParamsRlWithRecyclerView = new RelativeLayout.LayoutParams(MATCH_PARENT, 0);
        layoutParamsRlWithTextView = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
    }
    layoutParamsRlWithTextView.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    Transition changeBounds = new AutoTransition();
    changeBounds.setDuration(500);
    TransitionManager.beginDelayedTransition(rlWithRecyclerView, changeBounds);
    TransitionManager.beginDelayedTransition(rlWithTextView, changeBounds);
    rlWithRecyclerView.setLayoutParams(layoutParamsRlWithRecyclerView);
    rlWithTextView.setLayoutParams(layoutParamsRlWithTextView);
}

虽然我得到了其他两个答案的功劳,但我最终会删除它们,因为它们无法解决问题。