gpt4 book ai didi

Android 共享元素 fragment 转换 : return transition not working

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:46 26 4
gpt4 key购买 nike

在我的项目中,我使用 RecyclerView 来显示带有缩略图的项目列表。当用户点击一个项目时,一个 DetailView 被打开,图像显示在顶部。我正在尝试在这些 fragment 之间共享一些元素,但过渡仅在进入细节 fragment 时起作用,而不是在从细节 fragment 返回时起作用。

在我的 ListAdapter 中,我设置了 TransitionName 并在用户点击某个项目时通知 ListFragment:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
viewHolder.image.setTag(Constants.SHARED_NAME_IMAGE + item.itemId);
}

点击事件:

@OnClick(R.id.root)
public void onClickedItem() {
List<Pair<View, String>> sharedElements = new ArrayList<>();
sharedElements.add(new Pair<View, String>(image, (String) image.getTag()));
interactionListener.onItemSelected(data.get(getAdapterPosition() - headerItemCount).type, data.get(getAdapterPosition() - headerItemCount).itemId, sharedElements);
}

然后在 ListFragment 中启动 DetailFragment:

public void navigateToFragment(Fragment curFragment, Fragment nextFragment,
boolean addToBackStack, List<Pair<View, String>> sharedElements) {
if (nextFragment == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && sharedElements != null && curFragment != null) {
// Setup transition on first fragment
curFragment.setSharedElementEnterTransition(TransitionUtil.getReturnTransition(this));
curFragment.setSharedElementReturnTransition(TransitionUtil.getEnterTransition(this));
curFragment.setEnterTransition(null);
curFragment.setExitTransition(null);

// Setup transition on second fragment
nextFragment.setSharedElementEnterTransition(TransitionUtil.getEnterTransition(this));
nextFragment.setSharedElementReturnTransition(TransitionUtil.getReturnTransition(this));
nextFragment.setEnterTransition(null);
nextFragment.setExitTransition(null);

// Add second fragment by replacing first
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, nextFragment);
if (addToBackStack) {
ft.addToBackStack("fragment");
}

for (Pair<View, String> sharedElement : sharedElements) {
ViewCompat.setTransitionName(sharedElement.first, sharedElement.second);
ft.addSharedElement(sharedElement.first, sharedElement.second);
}

// Apply the transaction
ft.commit();
} else {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, nextFragment);
if (addToBackStack) {
ft.addToBackStack("fragment");
}
ft.commit();
}
}

最后,我在 DetailFragment 的 onCreateView() 中设置当前转换名称(因为它取决于 itemId):

ViewCompat.setTransitionName(header, Constants.SHARED_NAME_IMAGE + itemId);

最佳答案

我刚刚解决了这个问题:有两个问题:

  1. 我已经覆盖了我的列表适配器(因此转换名称已被删除)
  2. 我还没有在 onBindViewHolder 中设置转换名称

为了解决 #1,我像这里更改了 setUpList(){...}(第一个 if 很重要!):

 private void setupList() {
if (adapter != null) {
list.setAdapter(adapter);
linearLayoutManager = new LinearLayoutManager(getActivity());
list.setLayoutManager(linearLayoutManager);
setScrollListener();
return;
}

refreshLayout.setColorSchemeColors(ResourcesCompat.getColor(getResources(), R.color.main_color_2, getActivity().getTheme()));
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
loadList(true);
}
});

adapter = new ListAdapter((int) (DisplayUtils.getWidth(getActivity()) * 0.6), this);
list.setAdapter(adapter);

linearLayoutManager = new LinearLayoutManager(getActivity());
list.setLayoutManager(linearLayoutManager);

refreshLayout.post(new Runnable() {
@Override
public void run() {
refreshLayout.setRefreshing(true);
loadFilterItems();

为了解决#2,我只是将 setTag(..) 替换为 setTransitionName(..)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
vh.image.setTransitionName(Constants.SHARED_NAME_IMAGE + item.itemId);
}

在我的 onClick 方法中:

 List<Pair<View, String>> sharedElements = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
sharedElements.add(new Pair<View, String>(image, image.getTransitionName()));
}
interactionListener.onItemSelected(data.get(getAdapterPosition() - headerItemCount).type, data.get(getAdapterPosition() - headerItemCount).itemId, sharedElements);

关于Android 共享元素 fragment 转换 : return transition not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43181747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com