0

I have a single-activity application that implements (so far) three different fragments:

MainActivity - takes care of switching between fragments
HomeFragment - home screen for application
TasksFragment - includes RecyclerView with CardView and autohiding of Toolbar on vertical scrolling AssesmentFragment -

The (main) issue:
In TasksFragment, when Toolbar is hidden after scrolling to bottom of RecyclerView, Button on CardView is pressed and AssessmentFragment is created and slides in.

  • toolbar is now hidden in new AssessmentFragment (because of auto-hidden toolbar in RecyclerView)
  • after quickfix (see below) backbutton is unpressable!

My terrible quickfix:

public class TasksAdapter extends
    RecyclerView.Adapter<TasksAdapter.ViewHolder> {

    (... details omitted ...)

    viewHolder.messageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (position) {
                case 0:
                    //http://stackandroid.com/tutorial/custom-dialog-with-user-input/
                    Log.i("Press", "" + position);
                    break;
                case 1:
                    Log.i("Press", "" + position);
                    break;
                case 2:
                    Log.i("Press", "" + position);

                    MainActivity mainActivity = (MainActivity) mContext;
                    mainActivity.moveToolbarUp();
                    mainActivity.taskToAssessment();
                break;
            }
    });
}

MainActivity

public void moveToolbarUp() {
    TranslateAnimation anim = new TranslateAnimation(0, 0, -50, 122);
    anim.setDuration(400);
    toolbar.startAnimation(anim);
    anim.setFillAfter(true);
    //toolbar.setTranslationY(0);
}

public void taskToAssessment() {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.anim.left_in, R.anim.left_out);
    AssessmentFragment af = new AssessmentFragment();
    ft.replace(R.id.mainFragment, af);
    ft.addToBackStack(null);
    ft.commit();
}

Which looks like this:

That looks okay at first, but as you can see the backbutton is not pressable in the new Fragment, and also when going back to previous Fragment the toolbar is now in all wrong position.

Does anyone have ideas on how to improve on this? I would like to: have the toolbar accessible at all times, and reappear when its hidden. There has got to be a better way to do this. I would like to see some alternative way to deal with toolbar hidden on scrolling from recyclerview, when changing to new fragment. Surely someone will have had similar issue!

I will of course provide more code snippets if needed! Thanks.

user1736049
  • 366
  • 4
  • 18

1 Answers1

0

Fixed by adding

toolbar.animate().translationY(toolbar.getTop()).setInterpolator(new AccelerateInterpolator()).start();

at MainActivity's taskToAssessment():

public void taskToAssessment() {
    toolbar.animate().translationY(toolbar.getTop()).setInterpolator(new AccelerateInterpolator()).start();
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.anim.left_in, R.anim.left_out);
    AssessmentFragment af = new AssessmentFragment();
    ft.replace(R.id.mainFragment, af);
    ft.addToBackStack(null);
    ft.commit();
}
user1736049
  • 366
  • 4
  • 18