在5.1上使用Moto G进行测试时,smoothScrollToPosition
不起作用:
@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);
}
我尝试过list.post
,但这也行不通:
getListView().post(new Runnable() {
@Override
public void run() {
getListView().smoothScrollToPosition(pos);
}
})
即使tocListAdapter.notifyDataSetChanged();
已注释掉它也不起作用。
_lstTOC.smoothScrollToPositionFromTop(position, 0);
奇怪地工作......