1

预期的输出是数据应该加载一次,然后为每个后续滚动加载。但相反,它一次加载列表视图中的所有数据。

public class ViewPage extends ListActivity{

ListView lv;
ListAdapter ld;
int size=3;

ArrayList<String> filename = new ArrayList<String>();
Cursor contactcursor;
ArrayAdapter<String> arr_ad;
int contact_column_index;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mylist);




    String[] proj = { ContactsContract.Contacts.DISPLAY_NAME };
    contactcursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, proj, null, null, null);
    contact_column_index = contactcursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
    lv = (ListView)findViewById(android.R.id.list);
    contactcursor.moveToFirst();
        for(int i=0;i<size;i++)
        {

            filename.add(contactcursor.getString(contact_column_index));
            if(!contactcursor.moveToNext())
                break;
        }

        arr_ad = new ArrayAdapter<String>(ViewPage.this, android.R.layout.simple_list_item_1, filename);
        setListAdapter(arr_ad);


        lv.setOnScrollListener(new EndlessScrollListener());
}

public class EndlessScrollListener implements OnScrollListener {

private int visibleThreshold = size;
private int currentPage = 0;
private int previousTotal = 0;
private boolean loading = true;

public EndlessScrollListener() 
{

}

public EndlessScrollListener(int visibleThreshold) 
{
    this.visibleThreshold = visibleThreshold;
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // TODO Auto-generated method stub



    /*Runnable action = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }
    };
    runOnUiThread(action);*/

    Toast.makeText(ViewPage.this, "In" , 1).show();
     if (loading) 
     {
         if (totalItemCount > previousTotal) 
         {
            loading = false;
            previousTotal = totalItemCount;
            currentPage++;
         }
     }
     if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) 
     { 


            for(int i=0;i<size;i++)
            {

                if(!contactcursor.moveToNext())
                    break;


                filename.add(contactcursor.getString(contact_column_index));

            }

            arr_ad.notifyDataSetChanged();

            loading = true;
            Toast.makeText(ViewPage.this,totalItemCount+" "+visibleItemCount+" "+firstVisibleItem+" "+visibleThreshold+" ", 1).show();
     }
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // TODO Auto-generated method stub

}

}
}

neverscrolllistener 类中的 onscroll 方法运行并加载完整的数据,甚至没有完成滚动。提前谢谢。

4

1 回答 1

0

onScroll 被调用了很多,在我看来,在滚动时执行该逻辑非常昂贵。

我实现了为我的适配器添加一个简单逻辑的目标,请查看我刚才给出的这个答案。

于 2012-04-26T06:19:12.563 回答