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

标签 android android-layout android-recyclerview

在一个 Android 项目中,我有一个包含(从上到下)标题、RecyclerViewRelativeLayout 的 Activity。

代码示例:

<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 下方,在展开时位于其上方。我尝试动态设置元素之间的依赖关系,但无法做到。

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

编辑

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;

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

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

最佳答案

好的,最后的尝试。我回到 RelativeLayout 并在 RecyclerView 周围添加了另一个来解决它的滚动问题。然而,过渡变得棘手(并且听众变得复杂)。为了防止 RecyclerView 在带有 TextViewRelativeLayout 完全覆盖后者之前弹出,我必须包含一个淡入淡出的过渡到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);
}

虽然其他两个答案是我的功劳,但我最终会删除它们,因为它们不能解决问题。

关于android - 用户滑动时展开/折叠布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47093476/

相关文章:

android - 从我的 Android 应用打开 Chromecast YouTube 视频

java - 为什么 RecyclerView.OnScrollListener() 只影响 recyclerview 的最后一项

android - Realm 数据库类表有限制吗?

java - 如何在 Activity 中连续放置 6 个图像按钮

android - Android 中打开对话框时拦截 HOME 键

android - 我应该使用哪些 RelativeLayout 属性?

android - Kotlin RecyclerView 无法正常工作

java - 直接在指定位图下绘制文本

android - 如何使用 OpenCV + Qt5 在 Android 上设置相机的使用

java - 设置长点击选择?