当前位置: 首页 > 知识库问答 >
问题:

ViewHolder中嵌套的RecycerView中断折叠工具栏布局

姜鸿
2023-03-14

再次强调,在绑定的ViewHolder中,如果我开始在嵌套的RecycerView之外的任何其他项上滚动,滚动工作很好。但是,如果我在嵌套的RecycerView上开始滚动,那么它就会中断。

是否可以防止内部RecycerView上的滚动拦截,只让父级滚动?但是,我希望内部的RecycerView仍然可以处理点击。

以下是关于这种现象的YouTube链接:https://youtu.be/fzj7hmqskzu

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

}

共有1个答案

秦俊友
2023-03-14

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

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

mRecyclerView.setNestedScrollingEnabled(false);

注意:我试着在XML中的RecycerView上设置这个,但它不起作用?

android:nestedScrollingEnabled="false"
android:minHeight="1000dp"
<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>
 类似资料:
  • 我正在使用以及和设置为固定,我想知道当折叠时,是否有方法更改工具栏的标题文本。 总结一下,我想要滚动和展开时的两个不同的标题。 提前谢谢大家

  • 我正在创建一个视图及其相应的应用程序。我的包含一些项目,如果单击这些项目,则会将用户带到 。 在< code>DetailViewActivity上,我实现了一个可折叠的工具栏。现在,每次打开< code>DetailViewActivity时,都会在可折叠工具栏内的< code>ImageView上设置不同的图像(具有不同的尺寸)。 我希望默认打开到一定高度(例如 256dp),但如果图像高度大

  • 这个链接!告诉我如何禁用折叠工具栏布局。我想要的行为是折叠并禁用折叠工具栏,并在没有internet连接时显示错误视图。那么我如何才能做到这一点,折叠和禁用折叠工具栏布局?

  • 我最近遇到了协调器布局的问题。当我尝试创建简单的折叠工具栏布局(如本例所示)时,工具栏似乎位于状态栏下,如下图所示(在preLolipop设备上,一切都正常,因为应用程序不会在状态栏下绘制)。 我的活动布局的代码片段: My Styles(仅限21版),其中BaseAppTheme父级为Theme.AppCompat.Light.NoActionBar:

  • 我知道有成千上万个这样的问题,但是我尝试了所有的方法,但是我不能想出一个解决方案。基本上,我使用的是NoActionBar风格的活动。 风格。xml: v21/风格。xml: 家庭活动正常,抽屉将在透明状态栏下正常打开: 问题是使用以下布局的另一个活动: 基本上,状态栏是透明的,所以我看不到它,因为我的colorPrimary是白色的。奇怪的是,如果我折叠工具栏(因此只有选项卡可见),状态栏将正确

  • 我正在尝试在我的android应用程序中实现折叠工具栏。我可以按我希望的方式显示工具栏,但滚动时它不会塌陷。 我正在使用以下代码 activity.xml main_toolbar.xml 下面是屏幕的外观