LoginSignup
6
7

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-07-06

概要

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とは何か?

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7