我开发一个应用程序,我需要表现出一定的列表视图控件。我只是执行 OnScrollListener
在我的活动,以监测我的列表视图的内容,但是当我设置为我的活动布局它没有调用onScroll事件。如果我评论该块然后调用它就好了。我该如何解决这个问题?
包com.Threadtest;进口com.Threadtest.main.myRunnable;进口android.app.Activity;进口android.app.ListActivity;进口android.os.Bundle;进口android.util.Log;进口android.view.View;进口android.view.ViewGroup;进口android.widget.AbsListView;进口android.widget.BaseAdapter;进口android.widget.ListView;进口android.widget.TextView;进口android.widget.AbsListView.OnScrollListener;公共类TestList扩展ListActivity实现OnScrollListener { Aleph0适配器=新Aleph0(); @覆盖 保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.test); ListView控件列表视图=(ListView控件)findViewById(android.R.id.list); listview.setAdapter(适配器); / * setListAdapter(适配器); 。getListView()setOnScrollListener(本); * / } 公共无效onScroll(AbsListView来看, INT firstVisible,最终诠释visibleCount,诠释TOTALCOUNT){ Log.i(TestList,firstVisible =+ firstVisible +visibleCount =+ visibleCount +TOTALCOUNT =+ TOTALCOUNT); 布尔loadMore = / *也许添加填充* / firstVisible + visibleCount> = TOTALCOUNT; 如果(loadMore){ Log.i(TestList,在通知); 线程线程=新主题() { @覆盖 公共无效的run(){ 尝试{ this.sleep(10000); runOnUiThread(新myRunnable(适配器,visibleCount)); }赶上(InterruptedException的E){ // TODO自动生成catch块 e.printStackTrace(); } } }; thread.start(); } } 公共无效onScrollStateChanged(AbsListView V,int类型){} 类myRunnable实现Runnable { Aleph0适配器; 公共myRunnable(Aleph0适配器,诠释visibleCount) { this.adapter =适配器; this.adapter.count + = visibleCount; } 公共无效的run(){ // TODO自动生成方法存根 Log.i(TestList,列表计数=+ adapter.count); adapter.notifyDataSetChanged(); } } 类Aleph0扩展了BaseAdapter { 诠释计数= 40; / *起始量* / 公众诠释的getCount(){返回计数; } 公共对象的getItem(INT POS){返回POS; } 众长getItemId(INT POS){返回POS; } 公共查看getView(INT POS,视图V的ViewGroup以及P){ 如果(POS!=计数-1) { TextView的视图=新的TextView(TestList.this); view.setText(进入+ POS) 返回视图。 } TextView的视图=新的TextView(TestList.this); view.setText(中..+ POS) 返回视图。 } }}
解决方案
getListView()。setOnScrollListener(本)
是必要的监听器绑定到的ListView
。在 ListActivity
类不提供绑定的大部分侦听器的自动的方式,它只是提供了 OnItemClickListener 。退房的ListActivity code 。
I am developing an app where I need to show some list view control. I am just implementing the OnScrollListener
in my activity in order to monitor my listview content but it's not invoking the onScroll event when I set the layout for my activity. If i comment that block then it invokes it just fine. How do I solve this issue?
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)
is necessary to bind the listener to the ListView
. The ListActivity
class does not provide an "automatic" way of binding most of the listeners, It only provides an automatic way of binding the OnItemClickListener
. Check out the ListActivity code.