简体   繁体   中英

OnClick in Fragment - Only Firing Once

My app has a tabbed screen with three fragments, two of which are the same but with different data.

Initial screen

With comments

Clicking the comment down the bottom brings up the full list of comments and clicking the [x] closes the comments. This is done through animations and.setVisibility().

The problem is I can only do this once as clicking the comment after closing it does nothing.

Bringing up the comments is shown here - from debugging, the onClick fires if I don't mess with the visibilities so they must be causing a problem somewhere

comment_overview.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
            handleCommentOverviewClick(v);
       }
});
...

private void handleCommentOverviewClick(View v) {
        Log.d("OPEN", "open");
        scrollView.fullScroll(ScrollView.FOCUS_UP);
        Animation bottomUp = AnimationUtils.loadAnimation(getContext(),
                R.anim.slide_in_bottom);
        bottomUp.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                bottom_info_section.setVisibility(View.GONE);
                // animation.cancel();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        comment_section.startAnimation(bottomUp);
        comment_section.setVisibility(View.VISIBLE);
}

Closing the comments is similar and shown below

private void handleCommentsClose(View v) {
        Log.d("CLOSE", "cls");
        Animation topDown = AnimationUtils.loadAnimation(getContext(),
                R.anim.slide_out_bottom);
        topDown.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                comment_section.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        comment_section.startAnimation(topDown);
        bottom_info_section.setVisibility(View.VISIBLE);
    }

I've tried following advice of making the fragment implement OnClickListener and handling each clickable in a switch but I got the same behaviour - it would work once but not again unless I navigated away and came back.

Would love any suggestions as I'm kinda stuck

Maybe local variable bottomUp, topDown will be deleted when handleComment ends. Then onAnimationEnd won't called back and the view isn't gone. Atempt to declare it outside.

Animation topDown;

topDown = AnimationUtils.loadAnimation(getContext(),
                R.anim.slide_out_bottom);

Pretty sure the way I was animating it was messing it up. Switching to the Transition animations used here helped.

private void handleCommentOverviewClick(ViewGroup v) {
        Log.d("OPEN", "open");
        scrollView.fullScroll(ScrollView.FOCUS_UP);

        Transition transition = new Slide(Gravity.BOTTOM);
        transition.setDuration(200);
        transition.addTarget(comment_section);

        TransitionManager.beginDelayedTransition(v, transition);
        comment_section.setVisibility(View.VISIBLE);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM