SmoothScrollToPosition not working?

Testing with Moto G on 5.1, 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 tried to do list.post , but this also does not work:

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

Even if there is no comment tocListAdapter.notifyDataSetChanged(); he does not work.

Decision:

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

+5
source share

All Articles