Android smoothScrollToPosition不起作用?
在5.1上使用Moto G进行测试时,Android smoothScrollToPosition不起作用?,android,listview,scroll,position,smooth-scrolling,Android,Listview,Scroll,Position,Smooth Scrolling,在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_fragme
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);
}
getListView().post(new Runnable() {
@Override
public void run() {
getListView().smoothScrollToPosition(pos);
}
})
即使使用tocListAdapter.notifyDataSetChanged()代码>注释掉它不工作
解决方案:
\u lstoc.smoothScrollToPositionFromTop(位置0)
工作起来很奇怪…?@Selvin这是另一个问题,我在官方5.1上,无论我指定什么位置,我的都不会滚动。今天我遇到了一个类似的问题,smoothScrollToPosition不工作(也尝试了Post),但smoothScrollToPositionFromTop工作。。。我很想得到一个解释。