如何自动更改 ListView 中的选定项目?

How can I change automatically the selected item in a ListView?

我有一个 ListView,其中包含用户可以 select 的一些项目。

我希望第一个元素出现 selected,1 或 2 秒后,selected 项目将自动成为第二个。

我该怎么做?

当项目被 selected 时,它可以有一个粗体文本。

我有一个用于 ListView 的自定义适配器。


更新:

listview.setSelection(1);
System.out.println(listview.getSelectedItemPosition());

我用这个 println 测试了代码,但它 returns“-1”。没有 select 行。

如果您要求遍历列表并更改它的选择器,您可以使用这样的处理程序:

private Handler handler = new Handler();

private Runnable runnable = new Runnable() {
    @Override public void run() {
        int currentSelectedIndex = mListView.getSelectedItemPosition();
        if ((currentSelectedIndex + 1) < listCount) {
            mListView.setSelection(currentSelectedIndex + 1);
        } else {
            mListView.setSelection(0);
        }
        handler.postDelayed(runnable, 1000);
    }
};

然后在您的代码中,例如在 onCreate() 中调用:

handler.postDelayed(runnable, 1000);

这将根据需要每秒更改一次列表选择。

如果您想停止自动列表选择器,请调用:

handler.removeCallbacks(runnable);

在没有用户直接输入的情况下自动更改 UI 被认为是糟糕的用户体验,但如果您想以编程方式 select ListView 中的项目,您可以调用

ListView.setSelection(int);

Sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. If the specified selection position is less than 0, then the item at position 0 will be selected.

对于延迟,您需要将它放在处理程序中。

public class ListLooper {

    private LoopHandler mHandler;
    private ListView mListView;

    public ListLooper(Activity activity) {
        mListView = new ListView(activity);
        mHandler = new LoopHandler(mListView);
    }

    private void start() {
        mHandler.sendEmptyMessageDelayed(LoopHandler.MSG_LOOP, LoopHandler.DELAY);
    }

    private void stop() {
        mHandler.removeMessages(LoopHandler.MSG_LOOP);
    }

    private static class LoopHandler extends Handler {

        private static final int MSG_LOOP = 1;
        private static final int DELAY = 2000;

        /**
         * Use a WeakReference so we don't keep an implicit reference to the Activity
         */
        private WeakReference<ListView> mListRef;
        private int mPosition;

        private LoopHandler(ListView list) {
            mListRef = new WeakReference<>(list);
        }

        @Override public void handleMessage(Message msg) {
            super.handleMessage(msg);

            // Check if we still have a reference to the ListView, and the Activity/Fragment hasn't been destroyed
            if (mListRef.get() == null) {
                return;
            }

            // If we're looping, run this code
            if (msg.what == MSG_LOOP) {
                int count = mListRef.get().getAdapter().getCount();

                mListRef.get().setSelection(mPosition);

                // If the position is less than the count, increment it, otherwise set it to 0
                if (mPosition < count - 1) {
                    mPosition++;
                } else {
                    mPosition = 0;
                }

                // Send the same message again, so we repeat this process
                sendEmptyMessageDelayed(MSG_LOOP, DELAY);
            }
    }
}

有关 ListView 的精彩教程,请参阅本教程:http://www.vogella.com/tutorials/AndroidListView/article.html

回到原来的问题 select 一项使用以下内容:

 ListView.setSelection(int); 

调用只会更改 UI,无需用户直接输入。

对于延迟,您可以使用以下代码段:

final Handler handler = new Handler(Looper.getMainLooper());

handler.postDelayed(new Runnable() {
   @Override
   public void run() {
        ListView.setSelection(int); 
   }
}, 100);

有关如何更改 selected 项目的 Android ListView 背景颜色的信息,请参阅此 post

非常感谢!

这条指令:

listview.setSelection(int)

在我的代码中不起作用。

当我 select 某些项目时,背景颜色变为蓝色。这是正确的。 但是当 activity 刚加载时,没有项目 select 自动编辑。

我使用 performItemClick 并且项目被选中,突出显示并且 getSelectedItem 方法将 return 正确的值:

ListView1.performItemClick(ListView1.getChildAt(1),1,ListView1.getItemIdAtPosition(1));

其中 1 = 在列表中的位置