我正在开发一个应用程序,我需要显示一些列表视图控件。我只是在我的活动中实现了OnScrollListener
,以便监视我的listview内容,但是当我为我的活动设置布局时,它不会调用onScroll事件。如果我评论该块,那么它调用它就好了。我该如何解决这个问题?onScroll事件不会触发列表视图
package com.Threadtest;
import com.Threadtest.main.myRunnable;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
public class TestList extends ListActivity implements OnScrollListener {
Aleph0 adapter = new Aleph0();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ListView listview=(ListView)findViewById(android.R.id.list);
listview.setAdapter(adapter);
/*setListAdapter(adapter);
getListView().setOnScrollListener(this);*/
}
public void onScroll(AbsListView view,
int firstVisible, final int visibleCount, int totalCount) {
Log.i("TestList", "firstVisible="+firstVisible+" visibleCount="+visibleCount+" totalCount="+totalCount);
boolean loadMore = /* maybe add a padding */
firstVisible + visibleCount >= totalCount;
if(loadMore) {
Log.i("TestList", "In Notify");
Thread thread = new Thread()
{
@Override
public void run() {
try {
this.sleep(10000);
runOnUiThread(new myRunnable(adapter,visibleCount));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread.start();
}
}
public void onScrollStateChanged(AbsListView v, int s) { }
class myRunnable implements Runnable
{
Aleph0 adapter;
public myRunnable(Aleph0 adapter,int visibleCount)
{
this.adapter=adapter;
this.adapter.count += visibleCount;
}
public void run() {
// TODO Auto-generated method stub
Log.i("TestList", "List Count="+adapter.count);
adapter.notifyDataSetChanged();
}
}
class Aleph0 extends BaseAdapter {
int count = 40; /* starting amount */
public int getCount() { return count; }
public Object getItem(int pos) { return pos; }
public long getItemId(int pos) { return pos; }
public View getView(int pos, View v, ViewGroup p) {
if(pos!=count-1)
{
TextView view = new TextView(TestList.this);
view.setText("entry " + pos);
return view;
}
TextView view = new TextView(TestList.this);
view.setText("Loading.. " + pos);
return view;
}
}
}
我很困惑。你的意思是说,如果你省略getListView()。setOnScrollListener(this)',它不起作用? – dmon 2011-06-15 12:36:00
是的。如果一个Ommit的getListView()。setOnScrollListener(this)它不会被解雇。 – ram 2011-06-15 12:45:37
对,如果你省略了监听器,它不会触发。什么是真正的问题,**你想在没有设置监听器的情况下触发?** – 2011-06-15 12:56:27