从概览按钮调用时,Fragment的onResume()不会更改视图的可见性

时间:2018-07-20 15:47:10

标签: android android-fragments

我有一个应用程序,该应用程序在调用BottomNavigationView时应该是可见的ConstraintLayout和不可见的onResume()Here's what it should look like

在运行时,用户可以单击RecyclerView中的项目,该项目将具有蓝色背景,并且将显示ConstraintLayout,而隐藏BottomNavigationView。如果用户要执行其他操作,我希望此ConstraintLayout隐藏,而BottomNavigationView可见,因此我更改了onResume()中两个视图的可见性。如果用户转到应用程序中的另一个片段或活动,然后又回来或该片段被销毁,则效果很好。但是,如果用户单击主页按钮并使用电话的概览按钮返回到片段,则视图不会更改。 This就是它的样子。请注意,所选项目的背景会变回白色。这很奇怪,因为根据日志会调用onResume()。另外,我在更改可见性后打电话给getVisibility,并说BottomNavigationView是可见的,而ConstraintLayout不是。这是动作的gif

现在,这是我的代码。请不要介意不良做法,我已经调试了几天了。

public class CurrentWorkoutFragment extends Fragment implements EditSetFragment.OnSetModifiedListener {
private String TAG = "CurrentWorkoutFragment";
// View
private FloatingActionMenu menuCreate;
private FloatingActionButton fabAddRoutine;
private FloatingActionButton fabAddExercise;
private FloatingActionButton fab3;
private RecyclerView mPerformedExercisesRecyclerView;
private Workout mWorkout;
private ConstraintLayout mEditSetContainer;

private TextView mWarningTextView;


private BottomNavigationView mBottomNavigationView;
private MenuItem mPrevMenuItem;
// Controller
private Callbacks mCallbacks;
private PerformedExerciseAdapter mAdapter;

public interface Callbacks {
    public void onPerformedExerciseDeleted(PerformedExercise performedExercise);
}

// Model
private User mUser;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle onSavedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_current_workout, container, false);

    mUser = UserLab.get(getContext()).getUser();

    mWarningTextView = v.findViewById(R.id.fragment_current_workout_warning_text_view);
    enableNoAcriveWorkoutWarning(true);


    menuCreate = (FloatingActionMenu) v.findViewById(R.id.action_menu_current_workout_add);

    fabAddRoutine = (FloatingActionButton) v.findViewById(R.id.fab_add_routine);

    fabAddExercise = (FloatingActionButton) v.findViewById(R.id.fab_add_exercise);
    fabAddExercise.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivityForResult(ExerciseListActivity.newIntent(getContext(), true), RequestCodes.REQUEST_CODE_ADD_EXERCISE);
        }
    });
    fab3 = (FloatingActionButton) v.findViewById(R.id.fab12);

    mBottomNavigationView = (BottomNavigationView) v.findViewById(R.id.bottom_navigation);
    mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.bottom_navigation_current_workout:
                    break;
                case R.id.bottom_navigation_workout_log:
                    break;
                case R.id.bottom_navigation_routines:
                    break;
                case R.id.bottom_navigation_progress:
                    break;
            }
            return true;
        }
    });

    mPerformedExercisesRecyclerView = (RecyclerView) v.findViewById(R.id.fragment_current_workout_recycler_view);
    mPerformedExercisesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mEditSetContainer = (ConstraintLayout) v.findViewById(R.id.fragment_current_workout_edit_set_container);
    getChildFragmentManager().beginTransaction().replace(R.id.fragment_current_workout_edit_set_container, new EditSetFragment(), "EditSetFragment").commit();
    mEditSetContainer.setVisibility(View.INVISIBLE);
    return v;
}

@Override
public void onResume() {
    //mBottomNavigationView.setVisibility(View.VISIBLE);
    super.onResume();
    fixVisibility();
    getActivity().setTitle("Current Workout");
    updateUI();
    Log.i(TAG, "onResume");
    menuCreate.close(false);
    Log.i(TAG, "EditSetContainer's value: " + String.valueOf(mEditSetContainer.getVisibility()) + ", BottomNavigationView's visibility: " + String.valueOf(mBottomNavigationView.getVisibility()));
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_current_workout_fragment, menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    switch (item.getItemId()) {
        case R.id.menu_current_workout_fragment_finish_workout:
            finishWorkout();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onPause() {
    super.onPause();
    saveUI();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }

    if (requestCode == RequestCodes.REQUEST_CODE_ADD_EXERCISE) {
        if (data == null) {
            return;
        }
        if (mWorkout == null) {
            mWorkout = new Workout();
        }
            WorkoutLab.get(getContext()).addWorkout(mWorkout);
            mUser.setActiveWorkout(mWorkout.getId());
            UserLab.get(getContext()).updateUser(mUser);
            List<PerformedExercise> newPerformedExercises = PerformedExercise.createFromExercises(SelectableExerciseListFragment.getSelectedExercises(data), mWorkout.getId(), new Date());
            PerformedExerciseLab.get(getContext()).addPerformedExercises(newPerformedExercises);
            mWorkout.addPerformedExercises(newPerformedExercises);
            WorkoutLab.get(getContext()).updateWorkout(mWorkout);


        updateUI();
    }
    else if (requestCode == RequestCodes.REQUEST_CODE_EDIT_PERFORMED_EXERCISE) {
        if (data == null) {
            return;
        }
    }

}

@Override
public void onSetChanged(Set set) {

}

private void enableNoAcriveWorkoutWarning(boolean needWarning) {
    if (!needWarning) {
        mWarningTextView.setVisibility(View.GONE);
    }
    else {
        mWarningTextView.setVisibility(View.VISIBLE);
    }
}

// Update all variables that hold model data that can be changed in other fragments/activities. Will be called at onResume()
public void updateUI() {
    mUser = UserLab.get(getContext()).getUser();
    List<PerformedExercise> performedExercises = new ArrayList<>();

    if (mUser.getActiveWorkout() == null) {
        mWorkout = null;
    }
    else {
        mWorkout = WorkoutLab.get(getContext()).getWorkout(mUser.getActiveWorkout());
        performedExercises = PerformedExerciseLab.get(getContext()).getPerformedExercises(mWorkout.getPerformedExercises());

    }
    if (mAdapter == null) {
        mAdapter = new PerformedExerciseAdapter(performedExercises, getContext());
        mPerformedExercisesRecyclerView.setAdapter(mAdapter);
        mAdapter.setPerformedExerciseListEventListener(new OnPerformedExerciseListEventListener() {
            @Override
            public void onExerciseClicked(PerformedExercise performedExercise) {
                startActivityForResult(PerformedExerciseEditActivity.newIntent(getContext(), performedExercise.getmId()), RequestCodes.REQUEST_CODE_EDIT_PERFORMED_EXERCISE);
            }

            @Override
            public void onSetClicked(Set set, Boolean sameSet) {
                if (sameSet) {
                    hideEditSetFragment();
                }
                else {
                    showEditSetFragment();
                }
            }
        });
        SwipeAndDragHelper swipeAndDragHelper = new SwipeAndDragHelper(mAdapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(swipeAndDragHelper);
        touchHelper.attachToRecyclerView(mPerformedExercisesRecyclerView);
    }
    else {
        mAdapter.setPerformedExercises(performedExercises);
        mAdapter.notifyDataSetChanged();
    }
    enableNoAcriveWorkoutWarning(mWorkout == null);

}

// Updates the database to match any changed data.
private void saveUI() {
    UserLab.get(getContext()).updateUser(mUser);
    if (mWorkout != null) {
        mWorkout.setPerformedExercises(PerformedExercise.toUUIDs(((PerformedExerciseAdapter) mPerformedExercisesRecyclerView.getAdapter()).getPerformedExercises()));

        WorkoutLab.get(getContext()).updateWorkout(mWorkout);
    }
}

private void finishWorkout() {
    Log.i(TAG, "finishWorkout");
    mWorkout.setPerformedExercises(PerformedExercise.toUUIDs(((PerformedExerciseAdapter) mPerformedExercisesRecyclerView.getAdapter()).getPerformedExercises()));
    WorkoutLab.get(getContext()).updateWorkout(mWorkout);
    mWorkout = null;
    mUser.setActiveWorkout(null);
    UserLab.get(getContext()).updateUser(mUser);
    updateUI();
}

public class SwipeAndDragHelper extends ItemTouchHelper.Callback {

    private ActionCompletionContract contract;

    public SwipeAndDragHelper(ActionCompletionContract contract) {
        this.contract = contract;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        contract.onViewMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public void onChildDraw(Canvas c,
                            RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder,
                            float dX,
                            float dY,
                            int actionState,
                            boolean isCurrentlyActive) {
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            float alpha = 1 - (Math.abs(dX) / recyclerView.getWidth());
            viewHolder.itemView.setAlpha(alpha);
        }
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        recyclerView.getAdapter().notifyDataSetChanged();
    }
}

public interface ActionCompletionContract {
    void onViewMoved(int oldPosition, int newPosition);
    }

public interface OnPerformedExerciseListEventListener {
    void onExerciseClicked(PerformedExercise performedExercise);
    void onSetClicked(Set set, Boolean sameSet);
}


private void hideEditSetFragment() {
   // mBottomNavigationView.setVisibility(View.VISIBLE);
    //mEditSetContainer.setVisibility(View.VISIBLE);
    TranslateAnimation animate = new TranslateAnimation(
            0,                 // fromXDelta
            0,                 // toXDelta
            0,  // fromYDelta
            mEditSetContainer.getHeight());                // toYDelta
    animate.setDuration(500);
    animate.setFillAfter(true);

    TranslateAnimation animate1 = new TranslateAnimation(0,0,mBottomNavigationView.getHeight(),0);
    animate1.setDuration(500);
    animate1.setFillAfter(true);
    mBottomNavigationView.startAnimation(animate1);
    mEditSetContainer.startAnimation(animate);
    mBottomNavigationView.setVisibility(View.VISIBLE);
    mEditSetContainer.setVisibility(View.INVISIBLE);
}

private void showEditSetFragment() {
    mEditSetContainer.setVisibility(View.VISIBLE);
    TranslateAnimation animate = new TranslateAnimation(
            0,                 // fromXDelta
            0,                 // toXDelta
            mEditSetContainer.getHeight(),  // fromYDelta
            0);                // toYDelta
    animate.setDuration(500);
    animate.setFillAfter(true);

    TranslateAnimation animate1 = new TranslateAnimation(0,0,0,mBottomNavigationView.getHeight());
    animate1.setDuration(500);
    animate1.setFillAfter(true);
    mBottomNavigationView.startAnimation(animate1);
    mEditSetContainer.startAnimation(animate);
    mBottomNavigationView.setVisibility(View.INVISIBLE);
}

private void fixVisibility() {
    mBottomNavigationView.setVisibility(View.VISIBLE);
    mEditSetContainer.setVisibility(View.INVISIBLE);
}

}

0 个答案:

没有答案