简体   繁体   English

当我向下滚动时,滚动以在recyclerview中添加项目总是自动显示在顶部

[英]on scrolled to add items in recyclerview always comes to top automatically when i scroll down

I am implementing endless recycler view . 我正在实施无休止的回收站视图。 Data is getting fine from server aslo scroll down getting down and show it. 从服务器aslo向下滚动并显示数据可以得到很好的数据。 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();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM