Pertanyaan smoothScrollToPosition tidak berfungsi?


Pengujian dengan Moto G di 5.1, yang smoothScrollToPosition tidak bekerja:

@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);
}

Saya telah mencoba melakukan list.post tetapi itu juga tidak berhasil:

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

Bahkan dengan tocListAdapter.notifyDataSetChanged(); berkomentar itu tidak bekerja.

Larutan:

_lstTOC.smoothScrollToPositionFromTop(position, 0); bekerja dengan aneh ...


5
2018-04-03 14:18


asal


Jawaban: