android - onScroll 事件未针对 ListView 触发

标签 android

我正在开发一个应用程序,我需要在其中显示一些 ListView 控件。我只是在我的 Activity 中实现 OnScrollListener 以监视我的 ListView 内容,但是当我为我的 Activity 设置布局时它没有调用 onScroll 事件。如果我评论那个 block 然后它调用它就好了。我该如何解决这个问题?

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) 是将监听器绑定(bind)到 ListView 所必需的。 ListActivity 类不提供绑定(bind)大多数监听器的“自动”方式,它仅提供绑定(bind) OnItemClickListener 的自动方式。查看ListActivity code .

关于android - onScroll 事件未针对 ListView 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6357624/

相关文章:

android - 更改图像的高度和宽度 - android

android - 适用于 android 和 ios 的水平数字选择器

java - 使用 Controller/MVC 初始化 RecyclerView

android - 查询方法参数应该是可以转换为数据库列的类型或包含此类类型的列表/数组

android - 有没有办法检测应用程序是否通过后退按钮关闭?

java - 实现 Clarifai API

android - 滚动时ListView "coming back"

android - 如何使用 Kotlin 在 ListAdapter 中使用 Filterable?

android - 错误 :tensorflow:Couldn't understand architecture name ''

android - 单元测试 Android 应用程序,特别是与数据库相关的