RecyclerView的末尾未显示ProgressBar

时间:2019-08-11 06:19:31

标签: android android-recyclerview android-progressbar

我在显示进度栏上遇到问题,无法为用户提供有关应用程序正在加载要显示的新数据的信息。我已经看了看很多有关它的解决方案。我在加载和显示新数据方面相当成功。但是显示进度条并不是真的成功。寻找为什么我的代码错误或我的错误在哪里真令人沮丧。所以在这里,我需要这个领域的一些专家的帮助。

我已经使用了在互联网上找到的所有内容。当它到达列表的底部时,我正在输入文字。但是,当我调试程序时,键入返回数字以显示进度条未调用。

这是我的适配器的重要部分:

public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == 1) {
            header = false;
            return new NewsViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_news, viewGroup, false));
        } else if (i == 2) {
            Log.d("END", "Bagian terakhir");
            return new LoadingViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_progress, viewGroup, false));
        } else {
            header = true;
            return new NewsGroupViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_group, viewGroup, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position > 0 && articleList.get(position).getPublishedAt().equals(articleList.get(abs(position - 1)).getPublishedAt())) {
            return 1;
        }
        else if(articleList.get(position) == null){
            return 2;
        }
        else {
            return 0;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof NewsViewHolder) {
            NewsViewHolder newsViewHolder = (NewsViewHolder) viewHolder;
            newsViewHolder.bind(articleList.get(i));
        } else if (viewHolder instanceof LoadingViewHolder) {
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) viewHolder;
            loadingViewHolder.progressBar.setIndeterminate(true);
        } else if (viewHolder instanceof NewsGroupViewHolder) {
            NewsGroupViewHolder newsGroupViewHolder = (NewsGroupViewHolder) viewHolder;
            newsGroupViewHolder.bind(listDate.get(i), articleList.get(i));
        }
    }

以下是我在MainActivity中加载数据的方式:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if(newsPresenter.isListItemDisplaying(recyclerView) && !newsAdapter.isSearching()){
//                    if(loadMore!=null){
//                        loadMore.onLoadMore();
//                    }
                    allArticle.add(null);
                    newsAdapter.notifyItemInserted(allArticle.size()-1);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            allArticle.remove(allArticle.size()-1);
                            newsAdapter.notifyItemRemoved(allArticle.size());
                            Log.d("LAST_ITEM","loadmore "+totalPages);
                            totalPages++;
                            List<Article> newArticle = new ArrayList<>();
                            newArticle.addAll(newsPresenter.loadEverything("android&page="+totalPages));
                            isLoading = true;
                        }
                    },PERIOD_MS);
                }
            }
        });

还有一些代码来确定我是否到达RecyclerView的结尾:

public boolean isListItemDisplaying(RecyclerView recyclerView){
        if(recyclerView.getAdapter().getItemCount()!=0){
            int lastVisibleItemPosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();

            if(lastVisibleItemPosition!=RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount()-1){
                return true;
            }
        }
        return false;
    }

下面是ProgressBar的XML文件:

    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foregroundGravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这是我用于MainActivity的XML文件的一部分:

    <RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/relative_slider"
        android:layout_height="150dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/headline_vp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <me.relex.circleindicator.CircleIndicator
            android:layout_width="match_parent"
            android:id="@+id/indicator"
            android:layout_height="48dp"
            android:layout_alignBottom="@+id/headline_vp"
            android:layout_alignParentStart="true"/>

    </RelativeLayout>

    <com.tokopedia.testproject.problems.news.view.VerticalRecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:clipToPadding="false"
        app:layout_constrainedHeight="true"
        android:padding="16dp"
        android:visibility="gone"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/relative_slider"
        app:layout_constraintVertical_bias="0.0" />


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh_news"
        android:layout_width="390dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

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

我希望进度条在每次到达recyclerview底部时显示。感谢您的时间和帮助。问我是否需要一些解释。

0 个答案:

没有答案