当我向下滚动时,滚动以在recyclerview中添加项目总是自动显示在顶部 [英] on scrolled to add items in recyclerview always comes to top automatically when i scroll down

查看:343
本文介绍了当我向下滚动时,滚动以在recyclerview中添加项目总是自动显示在顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现无休止的回收站视图.从服务器aslo向下滚动并显示它可以得到很好的数据.但是在滚动时,它会自动一次又一次地到达顶部.我实施了许多解决方案,但没有用.这是一些代码.

I am implementing endless recycler view . Data is getting fine from server aslo scroll down getting down and show it. But on scroll it automatically comes to top again and again. I implemented number of solutions but doesn't work. here is bit of code.

    private int postNo = 15;

public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    if (!lastTime) {
        if (dy > 0) { // check for scroll down
            visibleItemCount = layoutManager.getChildCount();
            totalItemCount = layoutManager.getItemCount();
            pastVisibleItems = ((GridLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
            if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                postNo = postNo + 15;
                getDataFromUrl(postNo);
            }
        }
    }
} 

我想要的是当我向下滚动应用程序时增加项目数量,并且应该可以正常滚动.

What I want is when I scroll down app increase num of items and it should scroll fine.

         public void getDataFromUrl(int postNoLocal) {
    final int postLocal = postNoLocal;
    call = apiInterface.getGames("androidapp/get_apps?per_page="+postLocal+"&type=12");
    call.enqueue(new Callback<List<Example>>() {
        @Override
        public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
            if (response.isSuccessful()) {
                retrylayout.setVisibility(View.GONE);
                    jsonData = response.body();
                adapter = new RecyclerAdapter_apps(jsonData, getContext());
                    recyclerView.setAdapter(adapter);
                    refreshLayout.setRefreshing(false);
                    spinner.setVisibility(myFragmentView.INVISIBLE);
            }
        }

        @Override
        public void onFailure(Call<List<Example>> call, Throwable t) {
            runFun();
            retrylayout.setVisibility(View.VISIBLE);
            retry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    spinner.setVisibility(View.VISIBLE);
                    retrylayout.setVisibility(View.GONE);
                    getDataFromUrl(postNo);
                }
            });
            if (jsonData!=null){
                jsonData.clear();
            }
            refreshLayout.setRefreshing(false);
            recyclerView.removeAllViewsInLayout();
            spinner.setVisibility(myFragmentView.INVISIBLE);
        }
    });
}

推荐答案

尝试这种方式.希望它会起作用.

Try this way.Hope it will work.

 if(adapter ==null){
    adapter = new RecyclerAdapter_apps(jsonData, getContext());
    recyclerView.setAdapter(adapter);
 }else{
    homeAdapter.notifyDataSetChanged();
 }

这篇关于当我向下滚动时,滚动以在recyclerview中添加项目总是自动显示在顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆