简体   繁体   中英

Android automatically load more items in listview when at the bottom

I'm trying to implement this feature into my app and I know this question has been asked before but I don't get it. At this moment I retrieve all items by JSON request like so.

myTicketsFragment.java

private class PrefetchData extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Downloading..");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpReader httpReader = new HttpReader();
        httpReader
                .setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
                    @Override
                    public void resultReady(String result) {
                        JsonHelper jsonHelper = new JsonHelper();
                        tickets = jsonHelper.getTickets(result);

                        TicketAdapter ticketAdapter = new TicketAdapter(
                                getActivity(), tickets);
                        final ListView listViewTickets = (ListView) rootView
                                .findViewById(R.id.listViewTickets);
                        listViewTickets.setAdapter(ticketAdapter);

                        listViewTickets
                                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(
                                            AdapterView<?> parentView,
                                            View childView, int position,
                                            long id) {

                                        if (isNetworkAvailable()) {
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("ticketId",
                                                    tickets.get(position)
                                                            .getId());
                                            Intent ticketDetail = new Intent(
                                                    getActivity(),
                                                    TicketActivity.class);
                                            ticketDetail.putExtras(bundle);
                                            startActivity(ticketDetail);
                                        } else {
                                            showNoInternetDialog();
                                        }
                                    }
                                });
                        listViewTickets.setEmptyView(rootView
                                .findViewById(R.id.empty));
                        dialog.dismiss();
                    }
                });
        httpReader
                .execute("http://aa.aaaa.aa:8882/desk/API/v1/json.php?auth=platformt&a=getTickets&uauth="
                        + authCode + "&uid=" + uId + "&mode=mytickets");
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
    }
}

I guess I need to know which last item is shown and retrieve new ones.. I just don't really get the technique and I can't find a good example.

If anyone could lead me into the right direction, would be great!

I suggest you to move ListView out of AsyncTask . If you'll do it, you can implement lazy loading in such a way (here is a basic idea):

public class MyActivity extends Activity {
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listView = findViewById(R.id.listView);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                boolean loadMore = 
                        firstVisibleItem + visibleItemCount >= totalItemCount;
                if (loadMore) {
                    AsyncTask<Integer, Void, JSONObject> asyncTask = new AsyncTask<Integer, Void, JSONObject>() {
                        @Override
                        protected void onPreExecute() {
                            //do stuff, show loading image
                        }

                        @Override
                        protected void onPostExecute(JSONObject jsonObject) {
                            myAdapter.data = jsonObject;
                            myAdapter.notifyDataSetChanged();
                        }

                        @Override
                        protected JSONObject doInBackground(Integer... params) {
                            int startValue = params[0];
                            //make a request with pointing offset = startValue
                            return new JSONObject();
                        }
                    };
                    int startValue = totalItemCount;
                    asyncTask.execute(startValue);
                }
            }
        });
    }
}

So when you see the last item of ListView , then just download additional data and push it into adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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