简体   繁体   中英

ListView setOnScrollListener keep returning to top of View

I've ListView with setOnScrollListener to load more data from JSON, i using Asynctask to get JSON. and the JSON is paginated 10 per page. The problem is when scrolling reached the bottom, the scroll its always return to the top after more data displayed (loaded), like the Activity is restarted.

So how to make the scroll stay in position and not return to the top?

Code

    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);    
        }
    }

The error is caused by you re-initializing the Adapter and resetting it for the ListView. You need to initialize the Adapter only once and each time you add data, you need to add it to the ListData in the adapter. Then call notifyDataSetChanged()

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

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