【问题标题】:How to hide/show layout in response to scrolling如何隐藏/显示布局以响应滚动
【发布时间】:2015-12-24 16:50:48
【问题描述】:

这是我的活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
    <include
        layout="@layout/signs"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"/>

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

</LinearLayout>

signs 包括:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:elevation="4dp">


    <ImageView
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/navi_door"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:paddingTop="5dp"
            android:src="@drawable/door_24"/>

</RelativeLayout>

当我滚动RecyclerView 的项目列表时,标志布局当然会停留在它的位置。这就是为什么我的问题是如何让这个布局也被滚动然后 RecyclerView 的列表被滚动。

【问题讨论】:

    标签: android android-layout scrollview show-hide


    【解决方案1】:

    因为RecyclerView 不支持添加像 ListView 这样的页眉/页脚,所以您可以像下面这样实现:

    private class ViewType {
            public static final int Signs = 1;
            public static final int Normal = 2;
    }
    

    然后覆盖getItemViewType:

    @Override
    public int getItemViewType(int position) {
    
        if(items.get(position).isHeader)
            return ViewType.Signs;
        else
            return ViewType.Normal;
    
    }
    

    最后覆盖onCreateViewHolder:

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    
        View rowView;
    
        switch (i)
        {       
            case ViewType.Signs:
                rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.signs, viewGroup, false);
                break;
    
            default:
                rowView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.normal, viewGroup, false);
                break;
        }
        return new ViewHolder (rowView);
    }
    

    【讨论】:

    • 我必须创建一个适配器类是你所说的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2012-08-03
    • 2015-10-25
    • 1970-01-01
    • 2018-04-27
    相关资源
    最近更新 更多