Is there any way to load 5 - 10 items in recyclerview at a time instead of 50k?

懵懂的女人 提交于 2020-07-24 05:24:49

问题


I have to show a list of airports of the world, and user has to choose one of them. So I have made a recyclerview:

    RecyclerView recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerview);
    SearchView searchView = (SearchView)rootView.findViewById(R.id.searchview);

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    final AirportAdapter adapter = new  AirportAdapter(airports,getActivity());
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

where airports is the List<Airport>. The problem is that it loads 20 seconds for time to charge the complete list in recyclerview and when I try to search any item it is so slow. So I want to load 20 airports for time and then, load the others when the users scroll the recyclerview. Is there a way to do it? I also want to do it because the search with the searchview it's also a bit slow, and I want to make it fast. I thought that notifyDataSetChanged() preload it but it not works...

EDIT: this is how to retrieve my list from Realm:

 final RealmResults<AirportR> airps = realm.where(AirportR.class).findAll();
 airports = realm.copyFromRealm(airps, 0);

Thank you for answers...


回答1:


You can use pagination in recycle view for resolve your issue.

Please refer below links.

how to implement pagination in RecyclerView on scroll

https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b#.gxn5ft3n1




回答2:


You can use pagination concept.

Pagination:It is a process of dividing a document into multiple pages.

I used pagination as below:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            LinearLayoutManager linearLayoutManager= (LinearLayoutManager) recyclerView.getLayoutManager();


    /*countToShowLoadButton here is total number of items available to 
    *user after no. of page requests. So when total visible item would 
    *reach to the end (i.e. countToShowLoadButton-3) then a "load more"
    *button would appear and it will call a new request loadMoreData
    */

            if(linearLayoutManager.findLastVisibleItemPosition() > countToShowLoadButton-3 && currentPageNumber < totalPageNumber){
                loadMoreButton.setVisibility(View.VISIBLE);
                loadMoreButton.setEnabled(true);
            }
            super.onScrolled(recyclerView, dx, dy);
        }
    });

loadMoreData:

public void loadMoreData(View view){
    getAllData(currentPageNumber+1);
}

getAllData:

public void getAllData(int pageNo){

    progressBar.setVisibility(View.VISIBLE);

    final String key = sharedPreferences.getString("Auth_key",null);

    String pageNumber = String.valueOf(pageNo);

    checkInternet = new InternetConnectivityChecker(getApplicationContext());

    if(checkInternet.isOnline()) {

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URL_string.api_url +"?"+pageNumber,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("Result", "Success \n" + response);

                        try {
                            JSONObject responseJSON = new JSONObject(response);

                            JSONArray dataPart = responseJSON.getJSONArray("data");
                            JSONObject metaPart = responseJSON.getJSONObject("meta").getJSONObject("pagination");

//Here it updates all the variable so that when user clicks on "load more" 
// button again then it loads the data from new page

                            currentPageNumber = metaPart.getInt("current_page");
                            totalPageNumber = metaPart.getInt("total_pages");
                            totalNumberOfData = metaPart.getInt("total");
                            perPageItems = metaPart.getInt("per_page");
                            dataAtShownPage = metaPart.getInt("count");
                            countToShowLoadButton = perPageItems * currentPageNumber;


//Here it adds new data to the shown page of the app
                            prepareValueWithServerData(dataPart,dataAtShownPage);


                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(),noNetConnMessage,Toast.LENGTH_LONG).show();
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),noNetConnMessage,Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> header = new HashMap<>();
                header.put("Authorization", "Bearer " + key);
                return header;
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(stringRequest);

        if(stringRequest.getTimeoutMs() > DefaultRetryPolicy.DEFAULT_TIMEOUT_MS*2){
            requestQueue.stop();
            Toast.makeText(getApplicationContext(),noNetConnMessage,Toast.LENGTH_LONG).show();
            progressBar.setVisibility(View.INVISIBLE);
        }

    } else {
        Toast.makeText(getApplicationContext(),"Please Check Internet Connection",Toast.LENGTH_LONG).show();
    }

}

PrepareValueWithServerData:

public void prepareValueWithServerData(JSONArray data, int count){

    progressBar.setVisibility(View.INVISIBLE);

    for(int i=0; i<count; i++){
        try {
            JSONObject singleItem = data.getJSONObject(i);
            item_details a = new item_details(details_1,details_2,..);
            list.add(a);


 //notifying adapter that data has been changed in the list
            adapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Note: Server would have to return data according to pages counts i.e. pagination concept will be used in server-side programming so that it doesn't return 50K data at once, else it should return just 10-20 or so data according to the requested page. It would make your app work faster.



来源:https://stackoverflow.com/questions/40605487/is-there-any-way-to-load-5-10-items-in-recyclerview-at-a-time-instead-of-50k

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!