Helo,我创建了一个包含复杂项目的Recyclerview(其CardView带有嵌套的RecyclerView和网格布局管理器)。我有滚动滞后的问题。为了解决这个问题,我尝试将主要的recyclelerView放在NestedScrollView中,并添加滚动侦听器(当NestedScrollView滚动到内容结尾时)以进行分页。当我滚动第一页时,它工作得很完美,但是,当加载新页面时,NestedScrollView会在冻结时更改自己的高度。它看起来像一些灾难。也许你遇到了一些问题?
我的recyclerView项目:
<?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);
}
}
答案 0 :(得分: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();
}
});
答案 1 :(得分: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;