android – 处理RecyclerView,NestedScrollView和CardView

前端之家收集整理的这篇文章主要介绍了android – 处理RecyclerView,NestedScrollView和CardView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我将在我的应用程序中实现此UI:

好吧,我之前尝试过的一些方法

1.使用CollapsingToolbarLayout
我把我的CardView insid放在了CollapsingToolbarLayout并将它们全部放在AppBarLAyout中.

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" >

            <CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
                  <!-- My Views Goes there -->
            </CardView

            <android.support.v7.widget.Toolbar
                android:id="@+id/flexible.example.toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@null"
                app:layout_collapseMode="pin"
                style="@style/ToolBarWithNavigationBack"
                />
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <RecyclerView
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
    ></RecyclerView>

</android.support.design.widget.CoordinatorLayout>

P.S:我删除了不相关的代码,不提我

这种方式正常但是!!!当CardView高度高于屏幕高度时,其内容由AppBarLayout igigred并且不向用户显示

2.使用NestedScrollView
我把CardView和RecyclerView放在NestedScrollView中.但问题是当用户到达时回收到RecyclerView,然后滚动回到顶部,fling会变得迟钝和错误并停止一些用户必须滚动越来越多的地方!

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scrollbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:scrollbars="none" >
        <LinearLayout
            android:id="@+id/nested_scrollbar_linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

                <android.support.v7.widget.CardView
                    android:id="@+id/flexible.example.cardview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:cardBackgroundColor="@color/post_card_backgroind"
                    app:cardCornerRadius="0dp"
                    app:cardElevation="0dp">

                </android.support.v7.widget.CardView>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/four"
                    android:layout_marginEnd="@dimen/four"
                    android:layout_marginLeft="@dimen/four"
                    android:layout_marginRight="@dimen/four"
                    android:layout_marginStart="@dimen/four"
                    android:layout_marginTop="@dimen/four"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

如何解决这个问题?!
我不想使用为recyclelerview制作标题的适配器,我从其中一些获得了与性能相关的问题.

回答

将RecyclerView放在NestedScrollView中,如2所示,并应用.setNestedScrollingEnabled并将其设置为false

解决方法

您应该使用 getItemViewType.它很容易,不会产生任何性能开销.更改适配器中的代码,如下所示:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class CardViewHolder extends RecyclerView.ViewHolder {
        ...
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ...
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0; // Card Type
        } else {
            return 1; // Item Type
        };
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
         switch (viewType) {
             case 0: 
                 // Card Type
                 return new CardViewHolder(...);
             case 1: 
                 // Item Type
                 return new ItemViewHolder(...);
         }
    }

    // Optional
    // If your data list does not contain CardView data
    // You may need to add extra count in adapter

    @Override
    public final int getItemCount() {
        // Add one count for CardView data
        return list.size() + 1;
    }

    @Override
    public T getItem(int position) {
        // As the position is change because of CardView at position 0
        // So you may have to decrement the corresponding index 
        return list.get(position - 1);
    }
}

更新1

如果您不想更新适配器,则可以使用

<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">
        <CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
              <!-- Views Goes there -->
        </CardView>
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            // This is the key
            android:nestedScrollingEnabled="false"/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

android:nestedScrollingEnabled在API Level 21到xml中可用.
对于较低的API,使用java方法,即recyclerView.setNestedScrollingEnabled(false);或ViewCompat.setNestedScrollingEnabled(recyclerView,false);

注意(12-12-2016)

When using nested scroll. Be aware of the known issue of RecyclerView not recycling views as asked 07001 and 07002 (apparent reason answered by 07003 and 07004 respectively). So I’ll suggest to go with the first solution i.e. using getItemViewType. For more details,see complete and better answers like 07005 or 07006.

更新2

您可以使用setFullSpan.如果StaggeredGridLayoutManager的方向是垂直的,您可以使用以下代码将其宽度跨越到全屏:

public void onBindViewHolder(RecyclerView.ViewHolder viewHolder,int position) {
    if (position == 0) {
        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
        layoutParams.setFullSpan(true);
    }
}
原文链接:https://www.f2er.com/android/308809.html

猜你在找的Android相关文章