1

我最近想出了如何实例化 a ListView,并使用onAnimationEndand 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);
    }
}
4

1 回答 1

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));
于 2013-08-29T13:58:50.707 回答