ViewHolder中的嵌套RecyclerView会破坏折叠工具栏布局

时间:2015-07-25 23:46:25

标签: android-recyclerview androiddesignsupport android-collapsingtoolbarlayout android-appbarlayout

我在CoordinatorLayout中托管了一个垂直的RecyclerView,其中包含一个折叠工具栏布局。此RecyclerView的ViewHolder包含另一个带有GridLayoutManager的RecyclerView。

这种嵌套的原因是内部RecyclerView显示的图片集合可以包含1-20张图片。我不知道会有多少张图片,但网格的跨度必须始终为3.每次绑定ViewHolder时,我都会调整RecyclerView的布局参数,以便只容纳内容。

如果没有CollapsingToolBarLayout,这很有用。但是,如果有一个折叠的工具栏,用我的手指在内部RecyclerView上滚动不会滚动“折叠”工具栏,而是滚动另一个项目。但

要重新强调,在绑定的ViewHolder中,如果我开始滚动除嵌套的RecyclerView之外的任何其他项目,滚动工作正常。但是,如果我在嵌套的RecyclerView上开始滚动,那么它会中断。

是否可以阻止内部RecyclerView上的滚动拦截并且只允许父滚动?我希望内部的RecyclerView仍能处理点击次数。

以下是该现象的YouTube链接:https://youtu.be/fzj7HmqskzU

以下是父级RecyclerView的XML:

<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"
tools:context=".MainActivityFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        app:contentScrim="?attr/colorPrimary"
        app:layout_collapseParallaxMultiplier="0.7"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <View
            android:id="@+id/header_view"
            android:layout_width="match_parent"
            android:layout_height="192dp"
            android:animateLayoutChanges="true"
            android:background="@color/primary" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/action_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/abc_action_bar_default_height_material"
            android:elevation="4dp"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/Theme.AppCompat.NoActionBar"
            app:theme="@style/Theme.AppCompat.NoActionBar" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

这是托管嵌套RecyclerView的主要ViewHolder:

public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.ViewHolder> {

private static final int MODEL_OBJECT = 1;

private List<ModelObject> modelObjects;

private Context context;

private int imageSize;

public SimpleAdapter(List<ModelObject> modelObjects) {

    this.modelObjects = modelObjects;
    setHasStableIds(true);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    context = viewGroup.getContext();

    int screenWidth= context.getResources().getDisplayMetrics().widthPixels;

    imageSize = screenWidth / 3;

    final LayoutInflater inflater = LayoutInflater.from(context);

    View itemView;

    itemView = inflater.inflate(R.layout.fragment_main_row, viewGroup, false);

    return new ViewHolder(itemView, viewType);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {

    final ModelObject modelObject = modelObjects.get(position);

    // set text
    modelObject.setPosition(position + 1);
    viewHolder.titleTextView.setText(modelObject.getTitle());
    viewHolder.positionTextView.setText("" + (position + 1));


    adjustsizes(viewHolder.recyclerView, modelObject, imageSize);

    final NestedAdapter nestedAdapter = new NestedAdapter(modelObject.getPhotos());

    // Create and set GridLayoutManager for RecyclerView
    final GridLayoutManager recylerViewGridLayoutManager = new GridLayoutManager(context, 3);
    recylerViewGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return 1;
        }
    });

    viewHolder.recyclerView.setAdapter(nestedAdapter);
    viewHolder.recyclerView.setLayoutManager(recylerViewGridLayoutManager);
}

@Override
public int getItemViewType(int position) {
    return MODEL_OBJECT;
}

@Override
public int getItemCount() {
    return modelObjects.size();
}

@Override
public long getItemId(int position) {
    return modelObjects.get(position).hashCode();
}

private void adjustsizes(RecyclerView recyclerView, ModelObject modelObject, int imageSize) {
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) recyclerView.getLayoutParams();

    if (modelObject.getPhotos() != null) {

        Double numberOfRowsDouble = modelObject.getPhotos().size() / 3.0;

        int numberOfRowsInt = (numberOfRowsDouble.intValue() < numberOfRowsDouble)
                ? numberOfRowsDouble.intValue() + 1
                : numberOfRowsDouble.intValue();

        params.height = imageSize * numberOfRowsInt;
    }
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public int viewType;

    public ViewGroup mContainer;
    public View mDragHandle;
    public TextView titleTextView;
    public TextView positionTextView;
    public RecyclerView recyclerView;

    public ViewHolder(View itemView, int viewType) {
        super(itemView);
        this.viewType = viewType;
        mContainer = (ViewGroup) itemView.findViewById(R.id.container);
        mDragHandle = itemView.findViewById(R.id.drag_handle);
        titleTextView = (TextView) itemView.findViewById(R.id.title);
        positionTextView = (TextView) itemView.findViewById(R.id.position);
        recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view);
    }
}

}

最后,这是嵌套子项的适配器,带有图片:

public class NestedAdapter extends RecyclerView.Adapter<NestedAdapter.PhotoViewHolder> {

private static final int PHOTO = 1;

private Context context;

ArrayList<String> photos;

/**
 * Default constructor, takes no data as nothing has been recieved from the API
 */

public NestedAdapter(ArrayList<String> photos) {
    this.photos = photos;
}

@Override
public PhotoViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    this.context = viewGroup.getContext();
    View itemView;

    switch (viewType) {
        default: // Load default Message layout as bucketLists all have the same layout
            itemView = LayoutInflater.from(context).inflate(R.layout.fragment_photo_row, viewGroup, false);
            break;
    }

    return new PhotoViewHolder(itemView, viewType);
}

@Override
public void onBindViewHolder(PhotoViewHolder viewHolder, final int position) {

    // Switch depending on the kind of View
    switch (getItemViewType(position)) {
        case PHOTO: // Subtract 1, padding has taken up a space
            final String photo = photos.get(position);

            Picasso.with(context)
                    .load(photo)
                    .placeholder(R.drawable.rocks) // Must use place holder or fit won't work!
                    .fit()
                    .centerCrop()
                    .into(viewHolder.photo);
            break;
    }
}

@Override
public int getItemViewType(int position) {
    return PHOTO;
}

@Override
public int getItemCount() {
    return photos.size();

}

// ViewHolder for actual content
public final static class PhotoViewHolder extends RecyclerView.ViewHolder {

    public int viewType;        // Used to specify the view type.

    public ImageView photo;

    public PhotoViewHolder(View itemView, int ViewType) {
        super(itemView);

        switch (ViewType) {
            case PHOTO:
                viewType = PHOTO;
                photo = (ImageView) itemView.findViewById(R.id.photo);
                break;
        }
    }
}

}

3 个答案:

答案 0 :(得分:4)

我在RecyclerView和CollapsingToolbarLayout上遇到了同样的问题,其中RecyclerView位于NestedScrollView子布局中。

  

对我来说,解决方案是以编程方式设置:

mRecyclerView.setNestedScrollingEnabled(false);
     

注意:我尝试在XML中的RecyclerView上设置它,但它不起作用?

android:nestedScrollingEnabled="false"
     

使CollapsingToolbarLayout滚动的另一个技巧是将1000dp最小高度值设置为该子项的   NestedScrollView

android:minHeight="1000dp"
     

为什么1000dp?这里的SupportDesignDemos示例:   https://github.com/android/platform_development/blob/master/samples/SupportDesignDemos/res/layout/include_appbar_scrollview.xml

布局:

<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
        <android.support.design.widget.CollapsingToolbarLayout >

            <ImageView/>
            <android.support.v7.widget.Toolbar/>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.NestedScrollView
         app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout android:minHeight="1000dp">

            <android.support.v7.widget.RecyclerView 
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
             />

        </LinearLayout>

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

答案 1 :(得分:1)

mRecyclerView.setNestedScrollingEnabled(false);

答案 2 :(得分:0)

所以虽然上述情况仍然存在,但我找到了一种解决方法。

解决方法涉及将ViewHolder RecyclerView包装在RelativeLayout中,并在其上覆盖透明的可点击视图。在我的例子中,我使用了TextView。

现在在修复滚动时,仍然存在将点击传递到ViewHolderRecyclerView中右侧ViewHolder的问题。为了解决第二次挫折,我扩展了GestureDetector.SimpleOnGestureListener类以包含recyclerView。然后,我在wrapperTextView上设置一个OnTouchListener,然后将MotionEvent转发给GestureDetector.SimpleOnGestureListener子类。

子类最终解释运动事件并将其传递给内部的RecyclerView,然后再从点击的坐标开始,找出点击的子视图并在子项上调用performClick()。