【问题标题】:Onscroll listener for a listview isn't working列表视图的滚动监听器不起作用
【发布时间】:2016-04-06 13:43:07
【问题描述】:

我有一个列表视图,当它达到 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);


        }

    }

【问题讨论】:

标签: android listview onscrolllistener


【解决方案1】:

onScroll() 更改为

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多