列表视图的Onscroll监听器不起作用

时间:2016-01-02 12:23:42

标签: android listview onscrolllistener

我有一个listview,它应该在到达20个列表项时加载更多列表项。我添加了onscroll监听器来执行此操作 但它没有像我预期的那样工作。

要完全理解,请观看此video

我的代码:

public class InterActivity extends Activity implements OnItemClickListener, OnItemLongClickListener
     {


    ListView listview;
    List<ParseObject> ob;
    ProgressDialog mProgressDialog;
    FinalAdapter adapter;
     List<CodeList> codelist = null;
     SharedPreference shrdPreference;
     boolean loadingMore = false;
     private int limit = 20;



    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.inter_layout);
        shrdPreference = new SharedPreference();
        //Execute RemoteDataTask AsyncTask
        new RemoteDataTask().execute();
    }

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(InterActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Loading");
            // Set progressdialog message
            mProgressDialog.setMessage("Please wait loading ...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setCancelable(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            //my own doinbackground ()
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.inter_layoutListView);
            // Pass the results into ListViewAdapter.java
            adapter = new FinalAdapter(InterActivity.this,
                                          codelist);

            AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(adapter);
            animationAdapter.setAbsListView(listview);

            View loadMoreView = ((LayoutInflater)InterActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.loadmore, null, false);

            listview.addFooterView(loadMoreView);

            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);

            listview.setOnItemClickListener(InterActivity.this);

            listview.setOnItemLongClickListener(InterActivity.this); 
            // Close the progressdialog
            mProgressDialog.dismiss();

            listview.setOnScrollListener(new 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((lastInScreen == totalItemCount) && !(loadingMore)){     

        new LoadMoreDataTask().execute();
       }
      }
      });

        }
    }
    private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();



            }

        @Override
        protected Void doInBackground(Void... params) {
            // my own doinbackground ()
        }   

        @Override
        protected void onPostExecute(Void result) {
            // Locate listview last item
            int position = listview.getLastVisiblePosition();
            // Pass the results into ListViewAdapter.java
            adapter = new FinalAdapter(InterActivity.this, codelist);
            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);
            // Show the latest retrived results on the top
            listview.setSelectionFromTop(position, 0);


        }

    }

1 个答案:

答案 0 :(得分:0)

onScroll()更改为

if (!loadingMore && lastInScreen >= totalItemCount && totalItemCount > 0)
    new LoadMoreDataTask().execute();