我正在使用导航组件在两个片段之间导航,一个片段带有一个RecyclerView,该片段显示其余项目,另一个片段应显示其余细节。
我想为标题图像和其余名称实现共享的转换行为。我已经设法实现了(列表->详细信息)过渡,但是我在没有帮助的情况下尝试了很多用于返回过渡(详细信息->列表)的解决方案。
RestsListFragment的代码
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
postponeEnterTransition();
if (getActivity() != null) {
mViewModel = ViewModelProviders.of(getActivity()).get(SearchViewModel.class);
setupObservers();
initializeRecycler();
initializeSwipeLayout();
if(allRestsList == null)
mViewModel.getAllRests();
}
}
这是我初始化回收站的方式
public void initializeRecycler() {
mAllRestsAdapter = new AllRestsAdapter(getContext());
mAllRestsAdapter.setSearchOperation(this);
allRestsRecyclerView.setAdapter(mAllRestsAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
allRestsRecyclerView.setLayoutManager(layoutManager);
allRestsRecyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
startPostponedEnterTransition();
return true;
}
});
}
这是回收站项目点击的回调
@Override
public void goToDetail(TextView textView, ImageView imageView, int id, int pos, AllRestsModel.Data data) {
position_detail = pos;
Bundle bundle = new Bundle();
bundle.putInt("position", pos);
bundle.putInt("id", id);
bundle.putString("cover", data.getCover());
bundle.putString("name", data.getName());
FragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder()
.addSharedElement(imageView, "image" + id)
.addSharedElement(textView, "name" + id)
.build();
NavHostFragment.findNavController(this).navigate(R.id.action_master_istrahti_list_to_istrahti_details, bundle, null, extras);
}
RestDetailsFragment
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
postponeEnterTransition();
mViewModel = ViewModelProviders.of(getActivity()).get(SearchViewModel.class);
setupObservers();
getRestDetails();
}
最后,这是我如何获取其余详细信息
private void getRestDetails() {
Bundle bundle = getArguments();
if (bundle != null) {
int position = getArguments().getInt("position");
int id = getArguments().getInt("id");
mCoverUri = getArguments().getString("cover");
String name = getArguments().getString("name");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
restDetailsIv.setTransitionName("image"+id);
restDetailsNameTv.setTransitionName("name"+ id);
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
Transition transition = TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move);
setSharedElementEnterTransition(transition);
setSharedElementReturnTransition(transition);
}
restDetailsNameTv.setText(name);
Glide.with(getContext())
.load(Constants.STORAGE_API + mCoverUri)
.placeholder(R.drawable.splash)
.centerCrop()
.addListener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
startPostponedEnterTransition();
return false;
}
})
.into(restDetailsIv);
mViewModel.getRest(id + "");
}
}