简体   繁体   English

当位于底部时,Android自动在列表视图中加载更多项目

[英]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. 现在,我像这样通过JSON请求检索所有项目。

myTicketsFragment.java 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 . 我建议您将ListView移出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. 因此,当您看到ListView的最后一项时,只需下载其他数据并将其推入适配器即可。

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

相关问题 Android同时使用顶部的Pull to Refresh ListView并在滚动到底部时自动加载更多数据 - Android simultaneously use Pull to Refresh ListView at the top and automatically Load more data when scroll to the Bottom 如何到达listview的底部,加载更多项目 - How to reach bottom of listview, load more items 自动在ListView中加载更多项目(XML解析) - Automatically load more items in ListView (XML parsing) 在android中向下滚动时,加载更多项目并在列表底部添加底部 - Load more items and add bottom to the list when scroll down in android Android ListView 滚动到顶部时自动加载更多数据 - Android ListView automatically Load more data when scroll to the top Android:在“水平”列表视图中添加“加载更多项目” - Android : Add Load more items in Horizontal listview android从listview问题中加载更多项目 - android load more items from listview issue Android:保存ListView项目并加载/添加更多内容 - Android: Saving ListView items and load/add more 加载比android listview中可见的项目更多的项目 - load more items than visible in android listview 从上到下滚动时如何自动停止对项目的列表视图取消选择? - how to stop listview deselection on items automatically when scrolling top to bottom?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM