滚动视图中的 Recyclerview - 如何滚动整个内容?
Recyclerview inside scrollview- How to scroll whole content?
我在 Scrollview 中有 Recyclerview
<Scrollview
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/layoutStaticContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
现在,在滚动时,layoutStaticContent 固定在顶部,recyclerview 内容在底部独立滚动。
如何滚动整个内容,即(layoutStaticContent + recyclerview 内容)使得只有 1 个滚动视图?
我也尝试用 Nestedscrollview 替换 scrollview 但没有成功。
如果你想滚动所有数据,我认为你必须使用 CoordinatorLayout。在 CoordinatorLayout 中,您使用 appbar 布局和 CollapsingToolbarLayout,您可以在其中放置静态内容。因为在 android 中将一个可滚动容器用于另一个可滚动容器是错误的方法。你可以像这样使用坐标布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
解决此问题的一种可能方法是仅使用 RecyclerView 将静态内容作为 header 到您的 Recyclerview。
那么布局就是:
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
您的静态内容将有一个 list_item_header.xml 布局:
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
并且您必须更改您的 recyclerview 适配器以包含:
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
@Override
public int getItemCount()
{
int itemCount = super.getItemCount();
if (mIsHeaderPresent)
{
itemCount += 1;
}
return itemCount;
}
@Override
public int getItemViewType(int position)
{
if (mIsHeaderPresent && position == 0)
{
return TYPE_HEADER;
}
return TYPE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (viewType == TYPE_HEADER)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_header, parent, false);
ViewHolderHeader viewHolder = new ViewHolderHeader(itemView);
return viewHolder;
} else if (viewType == TYPE_ITEM)
{
return getItemViewHolder(parent);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder passedViewHolder)
{
if (mIsHeaderPresent && passedViewHolder instanceof ViewHolderHeader)
{
onBindHeaderViewHolder((ViewHolderHeader) passedViewHolder);
} else
{
onBindItemViewHolder(passedViewHolder);
}
}
使用android.support.v4.widget.NestedScrollView
then inside both layout
NestedScrollView
经过大量的搜索和尝试,我找到了解决方案:
1。在 ScrollView 中设置 RecyclerView 的高度:
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<android.support.v7.widget.RecyclerView
android:id="@+id/myPostsRecyclerView"
android:layout_width="match_parent"
**android:layout_height="550dp"**
android:layout_below="@+id/textView7"
android:background="@drawable/background"
android:padding="5dp"
**android:visibility="gone"**
tools:listitem="@layout/item_send" />
</RelativeLayout>
</ScrollView>
2。在适配器中,您设置数据列表的位置:
public void setData(List<Post> posts) {
this.posts.clear();
this.posts.addAll(posts);
notifyDataSetChanged();
if (posts.size() < 1)
activity.recyclerView.setVisibility(View.GONE);
else {
activity.recyclerView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams params=activity.recyclerView.getLayoutParams();
params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
activity.recyclerView.setLayoutParams(params);
}
}
将同时包含静态和动态数据的 LinearLayout 放入 NestedScrollView 中,它会非常有效。
这是您需要的代码:
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutStaticContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
希望对您有所帮助!
我在 Scrollview 中有 Recyclerview
<Scrollview
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/layoutStaticContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
现在,在滚动时,layoutStaticContent 固定在顶部,recyclerview 内容在底部独立滚动。 如何滚动整个内容,即(layoutStaticContent + recyclerview 内容)使得只有 1 个滚动视图? 我也尝试用 Nestedscrollview 替换 scrollview 但没有成功。
如果你想滚动所有数据,我认为你必须使用 CoordinatorLayout。在 CoordinatorLayout 中,您使用 appbar 布局和 CollapsingToolbarLayout,您可以在其中放置静态内容。因为在 android 中将一个可滚动容器用于另一个可滚动容器是错误的方法。你可以像这样使用坐标布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
解决此问题的一种可能方法是仅使用 RecyclerView 将静态内容作为 header 到您的 Recyclerview。
那么布局就是:
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
您的静态内容将有一个 list_item_header.xml 布局:
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
并且您必须更改您的 recyclerview 适配器以包含:
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
@Override
public int getItemCount()
{
int itemCount = super.getItemCount();
if (mIsHeaderPresent)
{
itemCount += 1;
}
return itemCount;
}
@Override
public int getItemViewType(int position)
{
if (mIsHeaderPresent && position == 0)
{
return TYPE_HEADER;
}
return TYPE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (viewType == TYPE_HEADER)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_header, parent, false);
ViewHolderHeader viewHolder = new ViewHolderHeader(itemView);
return viewHolder;
} else if (viewType == TYPE_ITEM)
{
return getItemViewHolder(parent);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder passedViewHolder)
{
if (mIsHeaderPresent && passedViewHolder instanceof ViewHolderHeader)
{
onBindHeaderViewHolder((ViewHolderHeader) passedViewHolder);
} else
{
onBindItemViewHolder(passedViewHolder);
}
}
使用android.support.v4.widget.NestedScrollView
then inside both layout
NestedScrollView
经过大量的搜索和尝试,我找到了解决方案:
1。在 ScrollView 中设置 RecyclerView 的高度:
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
<android.support.v7.widget.RecyclerView
android:id="@+id/myPostsRecyclerView"
android:layout_width="match_parent"
**android:layout_height="550dp"**
android:layout_below="@+id/textView7"
android:background="@drawable/background"
android:padding="5dp"
**android:visibility="gone"**
tools:listitem="@layout/item_send" />
</RelativeLayout>
</ScrollView>
2。在适配器中,您设置数据列表的位置:
public void setData(List<Post> posts) {
this.posts.clear();
this.posts.addAll(posts);
notifyDataSetChanged();
if (posts.size() < 1)
activity.recyclerView.setVisibility(View.GONE);
else {
activity.recyclerView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams params=activity.recyclerView.getLayoutParams();
params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
activity.recyclerView.setLayoutParams(params);
}
}
将同时包含静态和动态数据的 LinearLayout 放入 NestedScrollView 中,它会非常有效。
这是您需要的代码:
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutStaticContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//Static content.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
.
.
<LinearLayout>
//Dynamic content(newsfeed)
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
希望对您有所帮助!