ListView setOnScrollListener 不断返回View顶部

ListView setOnScrollListener keep returning to top of View

我使用带有 setOnScrollListener 的 ListView 从 JSON 加载更多数据,我使用 Asynctask 获取 JSON。 JSON 每页分页 10。问题是当滚动到达底部时,在显示(加载)更多数据后,滚动总是 return 到顶部,就像 Activity 重新启动一样。

那么如何让滚动条停留在原位而不是 return 到顶部?

代码

    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) {
                    if (firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0){
                        nextpage = nextpage + 1;
                        new getJSON().execute();
                    }
                }
            });

private class getJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            footer.setVisibility(View.VISIBLE);
            pDialog = new ProgressDialog(PoActivity.this);
            pDialog.setMessage("Please Wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            Handler handler = new Handler();

            String po = handler.makeServiceCall(url + "?page=" + nextpage);

            if (po != null){
                try {
                    JSONObject obj = new JSONObject(po);
                    JSONArray objArray = obj.getJSONArray("data");
                    for (int i = 0; i < objArray.length(); i++){
                        JSONObject data = objArray.getJSONObject(i);
                        String employee = data.getString("name");
                        String status = data.getString("status");


                        HashMap<String, String> datarow = new HashMap<>();

                        datarow.put("emp", employee);
                        datarow.put("status", status);

                        polist.add(datarow);

                    }
                }catch (final JSONException e){
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }else {
                Log.e(TAG,"Couldn't get json from server");
                endpage = 0;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Couldn't get json from server. check logcat", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            pDialog.dismiss();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            pDialog.dismiss();
            footer.setVisibility(View.GONE);

            listAdapter = new SimpleAdapter(PoActivity.this, polist,
                    R.layout.po_row, new String[]{"emp", "status"},
                    new int[]{R.id.name, R.id.status});

            listView.setAdapter(listAdapter);    
        }
    }

该错误是因为您重新初始化了Adapter,并为ListView重新设置了Adapter。 Adapter只需要初始化一次,每次添加数据时,都需要添加到Adapter中的ListData中。然后调用 notifyDataSetChanged()

@Override
protected void onPostExecute(Void aVoid) {
   //should be
   listAdapter.add(dataNew);
   listAdapter.notifyDataSetChanged()
}