我有这个屏幕,除了标题之外,所有项目都封装在RecyclerView
内。现在的问题是,RecyclerView
设置为特定宽度,而标头设置为match_parent
。我希望能够使用屏幕上的其余项目滚动标题(因此它看起来像RecyclerView
的一部分)而不更改宽度。那是可能的吗?
我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/activity_funky_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some random header"
android:background="@color/colorAccent"
android:gravity="center"
android:textSize="24sp"
android:paddingTop="8dp"
android:paddingBottom="8dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="512dp"
android:layout_height="match_parent"/>
</LinearLayout>
它看起来像什么:
提前致谢。
答案 0 :(得分:1)
<ScrollView
android:id="@+id/activity_funky_recycler_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some random header"
android:background="@color/colorAccent"
android:gravity="center"
android:textSize="24sp"
android:paddingTop="8dp"
android:paddingBottom="8dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="512dp"
android:layout_height="match_parent"/>
</LinearLayout>
您应该在RecyclerView中添加以下行:
app:layout_behavior="@string/appbar_scrolling_view_behavior"
答案 1 :(得分:1)
试试这个
<android.support.v4.widget.NestedScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/card_margin">
<LinearLayout
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info CardView1"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cheese_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/cardview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/cardview1"
android:layout_margin="@dimen/card_margin">
<LinearLayout
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info CardView2"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cheese_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/cardview2"
android:clipToPadding="false"
android:paddingTop="0dp"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
活动
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(true); // true: with header
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
final MyLinearLayoutManager layoutManager = new MyLinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false, getScreenHeight(this)); // final CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewAdapter); // recyclerView.setNestedScrollingEnabled(false); // Disables scrolling for RecyclerView, however, CustomLinearLayoutManager used instead of MyLinearLayoutManager
答案 2 :(得分:0)
考虑使用CollapsingToolbarLayout。它实现了您想要的功能。