1

Helo,我创建了一个带有复杂项目的 recyclerview(它的 CardView 带有嵌套的 RecyclerView 和网格布局管理器)。而且我在滚动时遇到滞后问题。为了解决这个问题,我尝试将 main 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);
    }
}
4

2 回答 2

2

这里滚动是 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();
                }
            });
于 2017-05-26T07:00:01.157 回答
0

尝试像这样进行分页。

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView,
                                               int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            if (dy > 0) {
                                visibleItemCount = mLayoutManager.getChildCount();
                                totalItemCount = mLayoutManager.getItemCount();
                                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                                if (loading) {
                                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                                        if (totalItemCount != TOTAL_JOBS) {
                                            loading = false;
                                            Log.e("total", "ite" + totalItemCount + " count" + TOTAL_JOBS);
                                            PAGE_COUNT = PAGE_COUNT + 10;
                                            serviceCall();
                                        } else {
                                            loading = false;
                                        }
                                    }
                                }
                            }
                        }
                    });

在此代码中 TOTAL_JOBS :来自服务器的作业总数, totalItemCount :是 recyclerView 中的项目总数,如果仍然有 recycleView 响应,则每次设置 loading = true ;

于 2016-09-26T10:37:31.280 回答