如何 hide/show 布局响应滚动
How to hide/show layout in response to scrolling
这是我的 activity 布局
<?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 的列表。
因为 RecyclerView
不支持像 ListView 添加 Header/Footer 你可以像下面这样实现:
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);
}
这是我的 activity 布局
<?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 的列表。
因为 RecyclerView
不支持像 ListView 添加 Header/Footer 你可以像下面这样实现:
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);
}