5

Testing with a Moto G on 5.1, the smoothScrollToPosition does not work:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    View fragView = inflater.inflate(R.layout.toc_fragment, parent, false);
    initialiseFragment(fragView);
    return fragView;
}

private void initialiseFragment(View fragmentView)
{
    _parentActivity = (MainActivity) getActivity();
    long bookId = getArguments().getLong("bookId");
    _tocEntries = Repository.getInstance().getTableOfContents(bookId);
    initList(fragmentView);
}

private void initList(View fragmentView)
{
    _lstTOC = (ListView) fragmentView.findViewById(R.id.lstTOC);
    final TOCListAdapter tocListAdapter = new TOCListAdapter(_parentActivity, _tocEntries);
    _lstTOC.setAdapter(tocListAdapter);
    _lstTOC.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            TOCEntry selectedTOCEntry = _tocEntries.get(position);
            if(selectedTOCEntry.isContainer())
            {
                if(selectedTOCEntry.getState() == TOCEntryState.Collapsed)
                {
                    expand(position, selectedTOCEntry);
                }
                else
                {
                    collapse(selectedTOCEntry);
                }

                tocListAdapter.notifyDataSetChanged();
                _lstTOC.smoothScrollToPosition(position); // does nothing...
                //_lstTOC.setSelection(position); // works
            }
        }
    });
}

private void expand(int entryToExpandPos, TOCEntry entryToExpand)
{
    List<TOCEntry> children = Repository.getInstance().getTableOfContentsByParentId(entryToExpand.getId());
    for(TOCEntry childTOCEntry : children)
    {
        _tocEntries.add(entryToExpandPos + 1, childTOCEntry);
    }
    entryToExpand.setState(TOCEntryState.Expanded);
}

I have tried doing a list.post but that also does not work:

 getListView().post(new Runnable() {
        @Override
        public void run() {
            getListView().smoothScrollToPosition(pos);
        }
    })

Even with the tocListAdapter.notifyDataSetChanged(); commented out it does not work.

Solution:

_lstTOC.smoothScrollToPositionFromTop(position, 0); works strangely...

Community
  • 1
  • 1
sprocket12
  • 5,368
  • 18
  • 64
  • 133
  • https://code.google.com/p/android/issues/detail?id=36062 ? – Selvin Apr 03 '15 at 14:25
  • @Selvin That's a different issue, I am on official 5.1, and mine does not scroll no matter what position I specify. – sprocket12 Apr 03 '15 at 14:28
  • I had a similar issue today in that smoothScrollToPosition does not work (also tried with Post), yet smoothScrollToPositionFromTop works... I would be interested in an explanation. – ldirer Jan 25 '16 at 14:14

0 Answers0