gpt4 book ai didi

java - RecyclerView getChildCount() 和 getItemCount() 返回相同的值

转载 作者:太空狗 更新时间:2023-10-29 22:58:41 25 4
gpt4 key购买 nike

RecyclerView 有一个 InfiniteScrollListener 并且在该监听器内部,在 OnScrolled() 方法中我计算可见项和总项数以检查是否应加载新项。当totalItemCount 和visibleItemCount 相同时,会导致无限循环加载。监听器与我的其他 RecyclerView 完美配合,这些 RecyclerView 没有 CoordinatorLayoutNestedScrollView 作为父级。我想保留此结构,因为客户不会接受更改。

我在一个具有这样布局的 Activity 中有一个 fragment

CoordinatorLayout{
NestedScrollView{
RecyclerView{
}
}
}

SDK 版本

compileSdkVersion 24
buildToolsVersion "23.0.3"
...
minSdkVersion 21
targetSdkVersion 24

fragment_profile.xml 作为父布局

<?xml version="1.0" encoding="utf-8"?>
<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">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_behavior="com.paxotic.paxira.util.behavior.AppBarFlingBehavior">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height_small"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
app:layout_collapseMode="parallax">

<ImageView
android:id="@+id/img_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:scaleType="centerCrop"
tools:src="@drawable/bg_greeting_activity" />

</RelativeLayout>

</android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<include
layout="@layout/activity_profile_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
... />

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

fragment_profile.xml 作为子布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

...

<android.support.v7.widget.RecyclerView
android:id="@+id/feedRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="@dimen/spacing_normal"
tools:listitem="@layout/list_item_activity" />

...

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

ProfileFragment.java: 相关函数

private void init() {

feedAdapter = new FeedAdapter(host);
feedAdapter.setHasStableIds(true);

LinearLayoutManager layoutManager = new LinearLayoutManager(host);
feedRecycler.setLayoutManager(layoutManager);
feedRecycler.setNestedScrollingEnabled(false);
feedRecycler.setHasFixedSize(true);
feedRecycler.setAdapter(feedAdapter);

infiniteScrollListener = new InfiniteScrollListener(host, layoutManager) {
@Override
public void onLoadMore(int pageIndex) {
if (!feedAdapter.isLoading) {
loadFeed(pageIndex);
}
}
};
feedRecycler.addOnScrollListener(infiniteScrollListener);
}

InfiniteScrollListener.java

public abstract class InfiniteScrollListener extends RecyclerView.OnScrollListener {
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = false; // True if we are still waiting for the last set of data to load.
private static final int VISIBLE_THRESHOLD = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 0;
private int loadingItemCount;

private LinearLayoutManager mLinearLayoutManager;

public InfiniteScrollListener(Context context, LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
loadingItemCount = context.getResources().getInteger(R.integer.feed_pull_item_count);
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if (dy < 0) return; // check for scroll down

visibleItemCount = mLinearLayoutManager.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

synchronized (this) {

if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + VISIBLE_THRESHOLD)) {
// End has been reached, Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
}

public abstract void onLoadMore(int current_page);

public void setLoading(boolean loading) {
this.loading = loading;
}

@Override
public void onScrollStateChanged(RecyclerView view, int scrollState) {
// Don't take any action on changed
}
}

最佳答案

您从 getChildCount() getItemCount() 获得相同的值,因为 recyclerview 将返回项目/子项的总数。

您必须根据您的用法覆盖其中一种方法,以获得您想要的值。在您的自定义 recyclerview 适配器中执行此操作。

为此,将其添加到您的 recyclerview 适配器中:

@Overide
public int getChildCount() {
//return your desired value here after obtaining it
}

关于java - RecyclerView getChildCount() 和 getItemCount() 返回相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38362058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com