我的问题是,我已经为从回收者视图到详细信息屏幕的图像进行了共享元素转换。从循环播放器视图到详细屏幕,它工作正常,但是在反向动画结束时错误的地方,图像消失在它应该的位置上方,如果根本没有动作条,它应该是它的位置。这是我的代码:
public class ListViewAdapter extends FirebaseRecyclerAdapter<ListItem, ListViewAdapter.ViewHolder> {
public static final String KEY_TITLE = "title";
public static final String KEY_IMAGE = "image";
private AppCompatActivity mContext;
ListViewAdapter(AppCompatActivity context, FirebaseRecyclerOptions<ListItem> options) {
super(options);
mContext = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item_grid_view, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position, @NonNull ListItem item) {
Glide.with(mContext).
load(item.getImage()).
into(holder.mImage);
ViewCompat.setTransitionName(holder.mImage, item.getImage());
holder.mTitle.setText(item.getTitle());
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
@BindView(R.id.image)
ImageView mImage;
@BindView(R.id.title)
TextView mTitle;
@BindView(R.id.scrim)
View mScrim;
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
mImage.setOnClickListener(this);
mTitle.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Context context = view.getContext();
Intent detailActivityIntent = new Intent(context, DetailActivity.class);
ListItem item = getItem(getAdapterPosition());
Bundle bundle = new Bundle();
bundle.putString(KEY_TITLE, item.getTitle());
bundle.putString(KEY_IMAGE, item.getImage());
detailActivityIntent.putExtras(bundle);
Pair<View, String> imagePair = new Pair<>((View)mImage, mImage.getTransitionName());
Pair<View, String> scrimPair = new Pair<>(mScrim, mScrim.getTransitionName());
// Pair<View, String> titlePair = new Pair<>((View)mTitle, mTitle.getTransitionName());
Bundle animation = ActivityOptions.makeSceneTransitionAnimation((Activity)context,
imagePair, scrimPair).toBundle();
context.startActivity(detailActivityIntent, animation);
}
}
}
public class DetailActivity extends AppCompatActivity {
@BindView(R.id.image)
ImageView mImageView;
@BindView(R.id.collapsing_toolbar_layout)
CollapsingToolbarLayout mCollapsingToolbarLayout;
@BindView(R.id.fab)
FloatingActionButton mFab;
@BindView(R.id.detail_scroll_view)
ScrollView mScrollView;
@BindView(R.id.layout_ingredients)
View mLayoutIngredients;
@BindView(R.id.app_bar)
Toolbar mToolbar;
private String mTitle;
private String mImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
ButterKnife.bind(this);
supportPostponeEnterTransition();
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
mTitle = bundle.getString(KEY_TITLE);
mImage = bundle.getString(KEY_IMAGE);
loadImage();
}
prepareToolbar();
prepareFab();
mScrollView.smoothScrollTo(0, 0);
}
private void loadImage() {
mImageView.setTransitionName(mImage);
Glide.with(this).
setDefaultRequestOptions(RequestOptions.centerCropTransform()).
load(mImage).
listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model,
Target<Drawable> target, boolean isFirstResource) {
supportStartPostponedEnterTransition();
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
target, DataSource dataSource, boolean isFirstResource) {
supportStartPostponedEnterTransition();
return false;
}
}).
into(mImageView);
}
private void prepareToolbar() {
mCollapsingToolbarLayout.setTitle(mTitle);
setSupportActionBar(mToolbar);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ripple_back);
}
}
private void prepareFab() {
mScrollView.getViewTreeObserver().addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mScrollView.getScrollY() < mLayoutIngredients.getHeight()){
mFab.show();
} else {
mFab.hide();
}
}
}
);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//to provide proper custom animation on return direct call to finishAfterTransition()
// is needed
if(item.getItemId() == android.R.id.home) {
finishAfterTransition();
return true;
}
return super.onOptionsItemSelected(item);
}
}