我最近想出了如何实例化ListView
,并使用onAnimationEnd
和performItemClick
,我能够以编程方式点击ListView
中的第一项。
此解决方案非常有效,但我的问题是,如果ListView
包含太多项目以适应屏幕,则会选择ListView
中的其他项目(甚至不只是一个)。有谁知道为什么会发生这种情况以及如何解决它?
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (savedInstanceState == null) {
Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
mListView.performItemClick(mListView, 0, mListView.getItemIdAtPosition(0));
}
});
return anim;
} else {
return super.onCreateAnimation(transit, enter, nextAnim);
}
}
答案 0 :(得分:4)
尝试
mListView.performItemClick(mListView.getAdapter().getView(your_click_position, null, null), your_click_position, mListView.getItemIdAtPosition(your_click_position));
或者
mListView.performItemClick(mListView.getChildAt(your_click_position), your_click_position, mListView.getItemIdAtPosition(your_click_position));
而不是
mListView.performItemClick(mListView, 0, mListView.getItemIdAtPosition(0));