0

我正在使用 ListView,其中数据是通过使用适配器从资产文件呈现的。当列表自动滚动时,它不会显示 ListView 的底部子项。但是当它触摸屏幕时,看不见的孩子就会变得可见。我想用自动滚动模式显示它。这是代码

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.list);
    loadUrduFileInList();
    adapter = new LessonListAdapter(this, R.layout.lesson_list_item, urduDataList);
    list.setAdapter(adapter);
    list.setOnScrollListener(this);
    verticalScrollMax = getListViewHeight(list);
    verticalScrollMax = verticalScrollMax - 921;
    ViewTreeObserver vto = list.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            list.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            verticalScrollMax = getListViewHeight(list);
            verticalScrollMax = verticalScrollMax - 921;
            startAutoScrolling();
        }
    });
}

public void startAutoScrolling()
{
    if (scrollTimer == null)
    {
        scrollTimer = new Timer();
        final Runnable Timer_Tick = new Runnable()
        {
            public void run()
            {
                moveScrollView();
            }
        };

        if (scrollerSchedule != null)
        {
            scrollerSchedule.cancel();
            scrollerSchedule = null;
        }
        scrollerSchedule = new TimerTask()
        {
            @Override
            public void run()
            {
                runOnUiThread(Timer_Tick);
            }
        };

        scrollTimer.schedule(scrollerSchedule, 20, 20);
    }
}
public void moveScrollView()
{
    scrollPos = (int) (list.getScrollY() + 1.0);
    if (scrollPos >= verticalScrollMax)
    {
        scrollPos = 0;
    }
    list.scrollTo(0, scrollPos);
}
4

1 回答 1

0

我建议,您的适配器以有效的方式实施。所以这段代码只是滚动列表视图

您需要尝试其他变量值

final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()

final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother

final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling

listView.post(new Runnable() {
                        @Override
                        public void run() {
                                new CountDownTimer(totalScrollTime, scrollPeriod ) {
                                    public void onTick(long millisUntilFinished) {
                                        listView.scrollBy(0, heightToScroll);
                                    }

                                public void onFinish() {
                                    //you can add code for restarting timer here
                                }
                            }.start();
                        }
                    });
于 2013-08-24T09:49:24.817 回答