Help us understand the problem. What is going on with this article?

ExpandableListViewで確実にスクロール位置を設定する方法

More than 5 years have passed since last update.

概要

ExpandableListViewでGroupを選択時にsetSelectionFromTopでスクロール位置を設定する。
この際、Galaxy S2(Android4.0.4)では設定した位置にスクロールされたがGalaxy J(Android4.3)ではY位置が0の位置に来てしまう

解決法

View.post(Runnable action)でスクロール位置を設定する

final ExpandableListView expandableListView = (ExpandableListView)findViewById(id);
expandableListView.post(new Runnable()
{
    @Override
    public void run()
    {
        expandableListView.setSelectionFromTop(position, y);
    }
}).start;

バージョン毎によるsetSelectionFromTopの設定タイミングは分からないが、post()でメッセージキューを設定することにより、ExpandableListViewの描画処理が終わった後にsetSelectionFromTopをするとこにより確実にスクロール位置を設定することが出来る。

参考

AndroidのHandlerとは何か?

http://www.adamrocker.com/blog/261/what-is-the-handler-in-android.html

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away