一个布局中的两个 RecyclerView 位于彼此下方
Two RecyclerViews under each other in one layout
如何在一个布局中让两个 RecyclerView 相互重叠?我不希望所有项目都只有一个 RecyclerView。
我的代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/main__item_background"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:text="@string/find_friends__already_playing"
android:background="@color/header"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/in_app_friends"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="@string/find_friends__invite_friends"
android:background="@color/find_friends__header"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="@dimen/list_header" />
<android.support.v7.widget.RecyclerView
android:id="@+id/friends_to_invite"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
您可以给每个 RecycleView
高度等于 0dp 和重量等于 1:
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
我自己找到了答案。
您需要将 LinearLayout 放入 ScrollView 并使用 wrap_content
作为 RecyclerView 的 layout_height
.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:background="@color/header"
android:gravity="center"
android:text="@string/find_friends__already_playing"
android:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/in_app_friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="@color/white"/>
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:background="@color/find_friends__header"
android:gravity="center"
android:text="@string/find_friends__invite_friends" />
<android.support.v7.widget.RecyclerView
android:id="@+id/friends_to_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
</LinearLayout>
</ScrollView>
RecyclerView 和 wrap_content
也存在错误,因此您必须使用 自定义布局管理器 。看看这个 post:How do I make WRAP_CONTENT work on a RecyclerView
我也遇到了同样的问题并编写了一个库,它通过连接适配器和布局来帮助实现这一点。
Gradle 尝试它的依赖项(需要 jcenter repo):
compile 'su.j2e:rv-joiner:1.0.3'//latest version by now
Thea 更改 xml 以使用与父级匹配的单个 RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/joined_friends_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:background="@color/white"/>
然后在代码中初始化 RecyclerView:
//init your RecyclerView as usual
RecyclerView rv = (RecyclerView) findViewById(R.id.joined_friends_rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//construct a joiner
RvJoiner rvJoiner = new RvJoiner();
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_in_app));
rvJoiner.add(new JoinableAdapter(new YourInAppRvAdapter()));
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_invite));
rvJoiner.add(new JoinableAdapter(new YourInviteRvAdapter()));
//set join adapter to your RecyclerView
rv.setAdapter(rvJoiner.getAdapter());
您可以查看 this link 了解更多库的详细信息和说明。希望对你有帮助。
只需使用:
<android.support.v7.widget.RecyclerView
android:id="@+id/card_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:scrollbars="vertical"
android:layout_below="@+id/your_first_recycler"/>
最后一行是给你的problem.use吧。
使用NestedScrollView
作为父布局,它应该有
android:weightSum="2"
并给予
android:layout_weight="1"
yours.It 的每个 RecyclerView 应该一个接一个地滚动。
如果底部的 recyclerview 不随主要内容滚动,请将 LinearLayout(参见 alan_derua 的回答)更改为 ConstraintLayout 并将两个 RecyclerView 包裹在里面。请参阅下面的代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:id="@+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/first_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:gravity="left"
android:paddingTop="0dp"
android:text="@string/my_tasks"
app:layout_constraintBottom_toTopOf="@+id/second_list_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/first_list_view" />
<android.support.v7.widget.RecyclerView
android:id="@+id/second_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
这对我有用!
您应该像这样创建一个 XML 布局文件
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/ingredients_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/steps_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
并且在代码中,您应该调用 setNestedScrollingEnabled(false)
RecyclerView ingredientsList = findViewById(R.id.ingredients_list);
RecyclerView stepsList = findViewById(R.id.steps_list);
ingredientsList.setNestedScrollingEnabled(false);
stepsList.setNestedScrollingEnabled(false);
如何在一个布局中让两个 RecyclerView 相互重叠?我不希望所有项目都只有一个 RecyclerView。 我的代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/main__item_background"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:text="@string/find_friends__already_playing"
android:background="@color/header"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:visibility="visible"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/in_app_friends"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:text="@string/find_friends__invite_friends"
android:background="@color/find_friends__header"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="@dimen/list_header" />
<android.support.v7.widget.RecyclerView
android:id="@+id/friends_to_invite"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
您可以给每个 RecycleView
高度等于 0dp 和重量等于 1:
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
我自己找到了答案。
您需要将 LinearLayout 放入 ScrollView 并使用 wrap_content
作为 RecyclerView 的 layout_height
.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:background="@color/header"
android:gravity="center"
android:text="@string/find_friends__already_playing"
android:visibility="visible" />
<android.support.v7.widget.RecyclerView
android:id="@+id/in_app_friends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="@color/white"/>
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/list_header"
android:background="@color/find_friends__header"
android:gravity="center"
android:text="@string/find_friends__invite_friends" />
<android.support.v7.widget.RecyclerView
android:id="@+id/friends_to_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
</LinearLayout>
</ScrollView>
RecyclerView 和 wrap_content
也存在错误,因此您必须使用 自定义布局管理器 。看看这个 post:How do I make WRAP_CONTENT work on a RecyclerView
我也遇到了同样的问题并编写了一个库,它通过连接适配器和布局来帮助实现这一点。
Gradle 尝试它的依赖项(需要 jcenter repo):
compile 'su.j2e:rv-joiner:1.0.3'//latest version by now
Thea 更改 xml 以使用与父级匹配的单个 RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/joined_friends_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:background="@color/white"/>
然后在代码中初始化 RecyclerView:
//init your RecyclerView as usual
RecyclerView rv = (RecyclerView) findViewById(R.id.joined_friends_rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//construct a joiner
RvJoiner rvJoiner = new RvJoiner();
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_in_app));
rvJoiner.add(new JoinableAdapter(new YourInAppRvAdapter()));
rvJoiner.add(new JoinableLayout(R.layout.your_title_for_invite));
rvJoiner.add(new JoinableAdapter(new YourInviteRvAdapter()));
//set join adapter to your RecyclerView
rv.setAdapter(rvJoiner.getAdapter());
您可以查看 this link 了解更多库的详细信息和说明。希望对你有帮助。
只需使用:
<android.support.v7.widget.RecyclerView
android:id="@+id/card_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:scrollbars="vertical"
android:layout_below="@+id/your_first_recycler"/>
最后一行是给你的problem.use吧。
使用NestedScrollView
作为父布局,它应该有
android:weightSum="2"
并给予
android:layout_weight="1"
yours.It 的每个 RecyclerView 应该一个接一个地滚动。
如果底部的 recyclerview 不随主要内容滚动,请将 LinearLayout(参见 alan_derua 的回答)更改为 ConstraintLayout 并将两个 RecyclerView 包裹在里面。请参阅下面的代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:id="@+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/first_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:gravity="left"
android:paddingTop="0dp"
android:text="@string/my_tasks"
app:layout_constraintBottom_toTopOf="@+id/second_list_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/first_list_view" />
<android.support.v7.widget.RecyclerView
android:id="@+id/second_list_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
这对我有用!
您应该像这样创建一个 XML 布局文件
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/ingredients_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/steps_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
并且在代码中,您应该调用 setNestedScrollingEnabled(false)
RecyclerView ingredientsList = findViewById(R.id.ingredients_list);
RecyclerView stepsList = findViewById(R.id.steps_list);
ingredientsList.setNestedScrollingEnabled(false);
stepsList.setNestedScrollingEnabled(false);