根据这些指南,我想在活动开始和结束时显示/隐藏我的FAB按钮
https://material.io/guidelines/motion/material-motion.html#material-motion-what-makes-a-good-transition
我正在使用共享元素活动转换来显示我的详细活动,如下所示:
https://github.com/codepath/android_guides/wiki/Shared-Element-Activity-Transition
CardAdapter.java
@Override
public void onBindViewHolder(CardViewHolder holder, int position) {
[...]
holder.getBinding().setVariable(BR.handler, mHandler);
holder.getBinding().executePendingBindings();
}
CardHandler.java
public class CardHandler {
private Activity mActivity;
CardHandler(Activity activity) {
mActivity = activity;
}
public void onCardClick(View view, Module module) {
final Context context = view.getContext();
final Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra(DetailActivity.MODULE, module);
View image = view.findViewById(R.id.card_image);
View description = view.findViewById(R.id.card_description);
Pair<View, String> p1 = Pair.create(image, "image");
Pair<View, String> p2 = Pair.create(description, "description");
ActivityOptionsCompat options = ActivityOptionsCompat
.makeSceneTransitionAnimation(mActivity, p1, p2);
context.startActivity(intent, options.toBundle());
}
}
DetailActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewDataBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_detail);
// Enable back button
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
if (null != getSupportActionBar()) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
// Retrieve module from extra
final Module module = getIntent().getParcelableExtra(MODULE);
// Bind module data
binding.setVariable(BR.module, module);
mFabAction = (FloatingActionButton) findViewById(R.id.fab_action);
addTransitionEnterListener();
}
private boolean addTransitionEnterListener() {
final Transition transition = getWindow().getSharedElementEnterTransition();
if (transition == null) return false;
transition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionEnd(Transition transition) {
mFabAction.show();
transition.removeListener(this);
addTransitionReturnListener();
}
});
return true;
}
private boolean addTransitionReturnListener() {
final Transition transition = getWindow().getSharedElementReturnTransition();
if (transition == null) return false;
transition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionStart(Transition transition) {
mFabAction.hide();
transition.removeListener(this);
}
});
return true;
}
隐藏方法无法正常工作,我的fab按钮在转换过程中会被我的布局重叠。