Android scrollview内部的Recyclerview-如何滚动整个内容?

Android scrollview内部的Recyclerview-如何滚动整个内容?,android,android-recyclerview,nestedscrollview,Android,Android Recyclerview,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"> <

我正在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内容),以便只有一个滚动视图?
我还尝试用Nestedscrollview替换scrollview,但没有成功。

如果您想滚动所有数据,我认为您必须使用CoordinatorLayout。在CoordinatorLayout中,您可以使用appbar布局和CollazingToolbarLayout来放置静态内容。因为在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标题的RecyclerView

然后,布局将简单为:

//Dynamic content(newsfeed)
     <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

使用
android.support.v4.widget.NestedScrollView
然后在两个布局中使用

经过大量的搜索和尝试,我找到了解决方案:

1.设置ScrollView中RecyclerView的高度:

...
2.在适配器中设置数据列表的位置:
public void setData(列出帖子){
this.posts.clear();
this.posts.addAll(posts);
notifyDataSetChanged();
如果(posts.size()<1)
activity.recyclerView.setVisibility(View.GONE);
否则{
activity.recyclerView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams params=activity.recyclerView.getLayoutParams();
params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
activity.recyclerView.setLayoutParams(参数);
}
}

将包含静态和动态数据的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>

//静态内容。
.
.
//动态内容(新闻源)

我希望有帮助

谢谢你的回答。但我不希望这里有可折叠的解决方案。此解决方案的问题是,向下滚动时,静态内容将立即开始显示&而不是整个内容。因此,您希望静态内容看起来也像是回收视图的一部分。当它到达第一个位置时,静态内容才可见。我认为@Aniket的第二个答案会对你有所帮助。Ankit静态内容中可能有很多项目&这些项目在不同的屏幕上可能不同。而动态内容布局是固定的。所以我不想修改适配器,因为我在很多地方都在使用这个适配器。我一直在寻找这个解决方案。如果有人使用AndroidX库而不是支持库,AndroidX的等价物是:
AndroidX.core.widget.NestedScrollView
。你能改写这个吗?这是否意味着将ScrollView替换为NestedScrollView?
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);
    }
}
    <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>
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);
    }

 }
<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>