嗨,我创建了一个包含复杂项目的recyclerview(带有嵌套RecyclerView的CardView和带有网格布局管理器的CardView)。我在滚动时出现滞后问题。为了解决这个问题,我尝试将主recyclerView放在NestedScrollView中,并添加滚动侦听器(当NestedScrollView滚动到内容结尾时)以进行分页。当我滚动第一页时,它可以完美工作,但是,当加载新页面时,NestedScrollView冻结会更改自己的高度。它的外观像是一场大灾难。也许您遇到了一些问题?

我的回收者查看项目:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/news_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
android:layout_margin="5dp"
app:cardElevation="3dp"
app:cardCornerRadius="3dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/news_item_avatar_img"
            android:layout_margin="15dp"
            android:layout_width="40dp"
            android:layout_height="40dp"/>

        <TextView
            android:layout_marginTop="20dp"
            android:layout_marginRight="10dp"
            android:layout_toRightOf="@+id/news_item_avatar_img"
            android:layout_toLeftOf="@+id/news_item_more_img"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/news_item_full_name"
            android:textStyle="bold"
            android:textSize="12sp"
            android:text="Кот черный"
            android:textColor="@color/colorAccent"/>

        <TextView
            android:layout_marginRight="10dp"
            android:layout_below="@+id/news_item_full_name"
            android:layout_toRightOf="@+id/news_item_avatar_img"
            android:layout_toLeftOf="@+id/news_item_more_img"
            android:id="@+id/news_item_date"
            android:layout_marginTop="5dp"
            android:textSize="12sp"
            android:textColor="@color/gray_middle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:visibility="gone"
            android:id="@+id/news_item_more_img"
            android:layout_alignParentRight="true"
            android:layout_marginTop="15dp"
            android:layout_marginRight="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_more_vert_gray_18dp"/>

    </RelativeLayout>

    <TextView
        android:maxLines="6"
        android:id="@+id/news_item_content_txt"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/light_black"/>

    <android.support.v7.widget.RecyclerView
        android:visibility="visible"
        android:layout_marginBottom="5dp"
        android:id="@+id/news_item_image_container"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <RelativeLayout
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_centerVertical="true"
            android:id="@+id/news_item_like_count_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_favorite_black_14dp"/>

        <TextView
            android:layout_toRightOf="@+id/news_item_like_count_img"
            android:padding="5dp"
            android:layout_centerVertical="true"
            android:text="0"
            android:textSize="14sp"
            android:textColor="@color/gray_middle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/news_item_like_count_txt"/>

        <ImageView
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/news_item_like_count_txt"
            android:layout_centerVertical="true"
            android:id="@+id/news_item_comment_count_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_chat_bubble_black_14dp"/>

        <TextView
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:layout_toRightOf="@+id/news_item_comment_count_img"
            android:text="0"
            android:drawablePadding="5dp"
            android:textSize="14sp"
            android:textColor="@color/gray_middle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/news_item_comment_count_txt"/>

        <ImageView
            android:visibility="gone"
            android:layout_toLeftOf="@+id/news_item_label_txt"
            android:layout_centerVertical="true"
            android:id="@+id/news_item_label_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_label_black_14dp"/>

        <TextView
            android:visibility="gone"
            android:gravity="right"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:id="@+id/news_item_label_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="@color/gray_middle"
            android:text="@string/group_str"/>
    </RelativeLayout>
</LinearLayout>




@Override
public void onBindViewHolder(NewsItemHolder holder, final int position) {
    final PostEntity postEntity = newsList.get(position);

    holder.fullName.setText(userFullName);
    holder.date.setText(postEntity.getCreateAt());

    holder.content.setText(text);

    holder.likeCount.setText(String.valueOf(postEntity.getLikesList().size()));
    holder.commentsCount.setText(String.valueOf(postEntity.getCommentsList().size()));

    if(postEntity.getUserAvatar() != null) {
        Picasso.with(context)
                .load(postEntity.getUserAvatar().getThumb())
                .fit()
                .centerCrop()
                .into(holder.avatar);
    }

    if(postEntity.getPhotosList() != null && postEntity.getPhotosList().size() > 0) {
        holder.imageContainer.setVisibility(View.VISIBLE);
        int cardWidth = width - dpToPx(16);

        ArrayList<PhotoEntity> shortListOfPhoto = new ArrayList<>();
        if(postEntity.getPhotosList().size() > 5){
            shortListOfPhoto.addAll(postEntity.getPhotosList().subList(0, 5));
        } else {
            shortListOfPhoto.addAll(postEntity.getPhotosList());
        }

        final GalleryAdapter galleryAdapter = new GalleryAdapter(context,shortListOfPhoto, postEntity.getPhotosList(), cardWidth);

        GridLayoutManager mLayoutManager = new GridLayoutManager(context, 4);
        holder.imageContainer.setLayoutManager(mLayoutManager);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return ClubUtils.getSpanLookup(galleryAdapter.getItemCount(), position);
            }
        });

        galleryAdapter.setHasStableIds(true);

        holder.imageContainer.setNestedScrollingEnabled(false);
        holder.imageContainer.setAdapter(galleryAdapter);
        holder.imageContainer.setHasFixedSize(true);


    } else {
        holder.imageContainer.setVisibility(View.GONE);
    }
}

最佳答案

这里的滚动是NestedScrollView

      scroll.getViewTreeObserver().addOnScrollChangedListener(() -> {
                View view = (View) scroll.getChildAt(scroll.getChildCount() - 1);

                Log.d("CategoryNeswList", scroll.getChildCount() + " child");
                int diff = (view.getBottom() - (scroll.getHeight() + scroll
                        .getScrollY()));

                if (diff == 0) {
//                        getPlaylistFromServer("more");
                    Toast.makeText(mContext, "Load More", Toast.LENGTH_SHORT).show();
                }
            });

关于android - 在NestedScrollView中具有分页的RecyclerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39700386/

10-13 03:06