Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RecyclerView getChildCount()和getItemCount()返回相同的值_Java_Android_Infinite Scroll - Fatal编程技术网

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

Java RecyclerView getChildCount()和getItemCount()返回相同的值,java,android,infinite-scroll,Java,Android,Infinite Scroll,RecyclerView有一个InfiniteSrollListener,在该listener中,在OnScrolled()方法中,我计算可见项和总项计数以检查是否应加载新项。当totalItemCount和VisibleTItemCount相同时,会导致无限循环的加载。监听器与我的另一个RecyclerViews完美配合,后者没有CoordinatorLayout或NestedScrollView作为父视图。 我想保留这种结构,因为客户不会接受更改 我在一个活动中有一个片段,它的布局如下 Co

RecyclerView
有一个InfiniteSrollListener,在该listener中,在
OnScrolled()
方法中,我计算可见项和总项计数以检查是否应加载新项。当totalItemCount和VisibleTItemCount相同时,会导致无限循环的加载。监听器与我的另一个
RecyclerView
s完美配合,后者没有
CoordinatorLayout
NestedScrollView
作为父视图。 我想保留这种结构,因为客户不会接受更改

我在一个活动中有一个片段,它的布局如下

CoordinatorLayout{
 NestedScrollView{
    RecyclerView{
    }
 }
}
sdk版本 fragment_profile.xml作为父布局 InfiniteSrollListener.java
公共抽象类InfiniteSrollListener扩展了RecyclerView.OnScrollListener{
private int previousTotal=0;//上次加载后数据集中的项目总数
private boolean load=false;//如果仍在等待加载最后一组数据,则为True。
private static final int VISIBLE_THRESHOLD=5;//在加载更多内容之前,在当前滚动位置下方的最小项目数。
int firstVisibleItem、visibleItemCount、totalItemCount;
私有int当前页面=0;
私有int加载项计数;
私人线路布局经理;
公共无限滚动侦听器(上下文上下文,LinearLayoutManager LinearLayoutManager){
this.mLinearLayoutManager=linearLayoutManager;
loadingItemCount=context.getResources().getInteger(R.integer.feed\u pull\u item\u count);
}
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
如果(dy<0)返回;//检查是否向下滚动
visibleItemCount=mLinearLayoutManager.getChildCount();
totalItemCount=mLinearLayoutManager.getItemCount();
firstVisibleItem=mLinearLayoutManager.findFirstVisibleItemPosition();
已同步(此){

如果(!loading&&(totalItemCount-VisibleTItemCount)您不应该在
nestedScrollview
中使用
recyclerView
垂直视图。 查找要在RecyclerView上显示的项目并开始获取新项目 您可以使用适配器。在
RecyclerView.adapter
中,您可以覆盖
onViewAttachedToWindow()


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

必须根据使用情况重写其中一个方法,才能获得所需的值。 在自定义recyclerview适配器中执行此操作

要执行此操作,请将此添加到您的recyclerview适配器:

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

您将获得视图的实际计数。如果适配器有15个项目和((LinearLayoutManager)myRecyclerView.getLayoutManager())。getChildCount()返回7,则在更新新项目后,由于“最大高度”或“静态高度”值,视图中有两个项目可见,直到其高度保持不变

模型(适配器)中的项目数和视图数不是一回事。

RecyclerView不会保存内存中所有15个项目的视图,在您的案例中只有7个。当您滚动时,它会重用这7个视图来显示不同的项目。这样做的目的是提高渲染性能。


如果希望得到15,那么应该使用mAdapter.getItemCount()相反。

嘿,你找到解决方案了吗?我也有同样的问题。.我找到了核心问题,不幸的是@DarkLeonhart没有找到解决方案。问题是NestedScrollView的行为与它在屏幕上可见的行为相同,因此它会绘制所有子项,而RecyclerView会返回所有可见的子项,这些子项都是它的适配器所具有的,因此,会给我们与getChildCount()和getItemCount()的值相同。我现在已经更改了我的UI。我用RecyclerView只加载了10个项目,并且有一个按钮可以导致另一个具有无限滚动的活动。@DarkLeonhart您找到解决方案了吗?正如@dgngulcan所说,问题是当RecyclerReview位于NestedScrollView中时,所以RecyclerView的滚动侦听器似乎不会影响任何内容。相反,我使用了NestedScrollView的基于
<?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>
<?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>
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);
}
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
    }
}
@Override
public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
    super.onViewAttachedToWindow(holder);
    if ((getItemCount() - 1) == holder.getAdapterPosition()){
        Log.d(TAG,"list done");
        onLoadMore(current_page);
    } else {
        Log.d(TAG,"remain item : " + (getItemCount() - holder.getAdapterPosition() - 1));
    }
}
@Overide
public int getChildCount() {
//return your desired value here after obtaining it
}