从inputsream到列表视图将10个项目下载到下一个10个项目中

时间:2018-06-25 21:56:11

标签: java android listview

在我的android应用程序中,我正在通过输入流下载内容。目前,从我的代码开始,所有内容都通过asynctask从服务器和解析器一次加载到列表视图中。如何通过以下片段中的十个输入,从服务器中下载项目

private String downloadData()
    {
        HttpURLConnection con=Connector.connect(urlAddress);
        if(con==null)
        {
            return null;
        }

        try
        {
            InputStream is=new BufferedInputStream(con.getInputStream());
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            String line;
            StringBuffer jsonData=new StringBuffer();


            return jsonData.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

请问我如何使用创建的dowloaddata()从服务器中批量提取10个项目,然后在滚动时相应地移至列表视图

1 个答案:

答案 0 :(得分:0)

您需要为列表视图设置滚动侦听器:

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) {

                int lastInScreen = firstVisibleItem + visibleItemCount;
                if(!loadingMore && (lastInScreen == totalItemCount)) {
                    new Thread(() -> {
                        loadingMore = true; // loadingMore is local boolean to check if you already loaded
                        runOnUiThread(() -> {
                            //Here You Can Execute your AsyncTask and update your List of Items
                            adapter.notifyDataSetChanged(); // notify the data in list of items has changed
                            loadingMore = false;
                        });

                    }).start();
                }

            }
        });

您可以阅读本教程以创建“ ENDLESS LIST VIEW” https://www.mindstick.com/Articles/1570/endless-list-view-adapter-in-android