问题描述
|
我正在开发一个需要显示列表视图控件的应用程序。我只是在活动中实现“ 0”以监视我的Listview内容,但在为活动设置布局时并没有调用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)
是将侦听器绑定到ListView
所必需的。 ListActivity
类没有提供绑定大多数侦听器的“自动”方式,它仅提供了OnItemClickListener
的自动绑定方式。签出ListActivity代码。
, 如果将onScroll签名更改为public void onScroll(AbsListView view,int firstVisible,int visibleCount,int totalCount)
,它将被调用吗?通过添加“ 7”限定符,您已经更改了方法签名,这将导致看不到您覆盖的方法。将visibleCount int复制到新的(最终)int,以便您可以在线程中访问它。
, import android.widget.AbsListView.OnScrollListener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ListView listview=(ListView)findViewById(android.R.id.list);
listview.setAdapter(adapter);
listview.setOnScrollListener(onLstScroll);
}
OnScrollListener onLstScroll= new OnScrollListener()
{
@Override
public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount)
{
//Do something
}
@Override
public void onScrollStateChanged(AbsListView view,int scrollState)
{
//Do something
}
};
, 您在onScroll()
和onScrollStateChanged()
方法上缺少@Override
表示法。