user1549672
user1549672

Reputation: 486

Performing performItemClick

I recently figured out how to instantiate a ListView, and using onAnimationEnd and performItemClick, I was able to click the first item in the ListView programmatically.

This solution works wonderfully, but my problem is that if the ListView contains too many items to fit on the screen, other items in the ListView are selected instead (not even just one). Does anyone know why this happens and how to fix it?

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

Upvotes: 1

Views: 1569

Answers (1)

Ritesh Gune
Ritesh Gune

Reputation: 16739

Try

mListView.performItemClick(mListView.getAdapter().getView(your_click_position, null, null), your_click_position, mListView.getItemIdAtPosition(your_click_position));

Or

mListView.performItemClick(mListView.getChildAt(your_click_position), your_click_position, mListView.getItemIdAtPosition(your_click_position));

instead of

mListView.performItemClick(mListView, 0, mListView.getItemIdAtPosition(0));

Upvotes: 4

Related Questions