如何从请求到API动态地向回收站添加新项目

时间:2017-03-27 22:51:45

标签: android android-recyclerview infinite-scroll

在打开我的应用程序时,它会发送一个请求,它会返回10个项目,我希望一旦我在底部它再发出一个请求并将另外10个项目添加到最初的10个项目,我该怎么做?

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {       
        new RequestBase(getActivity())
        {
            @Override
            public JsonObject onHttpOk(JsonObject response) throws JSONException
            {
                JsonObject objeto, pagination_details = null;
                final JsonArray data;

                if (getActivity() == null)
                    return response;

                if (response.get("pagination") == null)
                {
                    objeto = response;
                } else
                    {
                    objeto = response;
                    pagination_details = response.get("pagination").getAsJsonObject();
                    pagination = pagination_details.get("limit").getAsInt();
                    data = objeto.get("data").getAsJsonArray();
                    gson = new Gson();
                    arrayBusiness = new ArrayList<Business>();
                    arrayBusiness = gson.fromJson(data, BUSINESS_TYPE);

                        FeedsFragment.this.getActivity().runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run() {
                            recycler = (RecyclerView) FeedsFragment.this.getActivity().findViewById(R.id.recycler_main);
                            adapter = new MainAdapter(getActivity(), arrayBusiness, R.layout.main_row);

                            recycler.setNestedScrollingEnabled(false);
                            mLayoutManager = new GridLayoutManager(FeedsFragment.this.getActivity(), 2);
                            recycler.setLayoutManager(mLayoutManager);
                            recycler.setAdapter(adapter);
                            adapter.notifyItemInserted(arrayBusiness.size() - 1);

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

                                    visibleItemCount = recycler.getChildCount();
                                    totalItemCount = mLayoutManager.getItemCount();
                                    firstVisiblePosition = mLayoutManager.findFirstVisibleItemPosition();

                                    if (loading)
                                    {
                                        if (totalItemCount > previousTotal)
                                        {
                                            loading = false;
                                            previousTotal = totalItemCount;
                                        }
                                    }
                                    if (!loading && (totalItemCount - visibleItemCount) <= (firstVisiblePosition + visibleThreshold))
                                    {
                                        Log.d("FINAL","LLEGASTE A FINAL DEL SCROLL");
                                        loadMore();                             
                                        loading = true;
                                    }

                                }
                            });

                            GifTextView loading = (GifTextView) FeedsFragment.this.getActivity().findViewById(R.id.loading);
                            TextView loadingText = (TextView) FeedsFragment.this.getActivity().findViewById(R.id.loadingText);
                            loading.setVisibility(View.GONE);
                            loadingText.setVisibility(View.GONE);
                        }
                    });
                }
                if (pagination_details.isJsonNull())
                {
                    Log.d("Paginacion", pagination_details.toString());
                }
                return objeto;
            }


            @Override
            public void onHttpCreate(JsonObject response) throws JSONException
            {

            }

            @Override
            public void onHttpUnprocessableEntity(JsonObject response) throws JSONException
            {
                this.cancel(true);
                final String error = response.get("errors").toString();
                getActivity().runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Toast.makeText(getActivity().getApplicationContext(), error, Toast.LENGTH_LONG).show();
                    }
                });
            }
        }.execute("businesses/premiums", "GET");
        return android;
    }

我的方法是 loadMore 它的作用是将请求发送给API,但是第2页的结果是我试图追加到初始版本10的结果,但它的作用是替换它们而不是附加

  public void loadMore()
    {
        new Thread(new Runnable()
        {
            public void run()
            {

                new RequestBase(getActivity()) {
                    @Override
                    public JsonObject onHttpOk(JsonObject response) throws JSONException {
                        JsonObject objeto, pagination_details = null;
                        final JsonArray data;

                        if (getActivity() == null)
                            return response;

                        if (response.get("pagination") == null) {
                            objeto = response;
                        } else {


                            objeto = response;
                            pagination_details = response.get("pagination").getAsJsonObject();
                            pagination = pagination_details.get("limit").getAsInt();
                            data = objeto.get("data").getAsJsonArray();
                            gson = new Gson();
                            arrayBusiness = new ArrayList<Business>();
                            arrayBusiness = gson.fromJson(data, BUSINESS_TYPE);

                            Log.d("size", String.valueOf(arrayBusiness.size()));

                            FeedsFragment.this.getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    recycler = (RecyclerView) FeedsFragment.this.getActivity().findViewById(R.id.recycler_main);
                                    adapter = new MainAdapter(getActivity(), arrayBusiness, R.layout.main_row);

                                    recycler.setNestedScrollingEnabled(false);
                                    mLayoutManager = new GridLayoutManager(FeedsFragment.this.getActivity(), 2);
                                    recycler.setLayoutManager(mLayoutManager);
                                    recycler.setAdapter(adapter);                   
                                }
                            });
                        }
                        if (pagination_details.isJsonNull())
                        {
                            Log.d("Paginacion", pagination_details.toString());
                        }
                        return objeto;
                    }
                }.execute("businesses/premiums?offset=2", "GET");               
            }
        }).start();
    }

感谢您的帮助

0 个答案:

没有答案