gpt4 book ai didi

具有回收 View 和详细信息 View 的 fragment 之间的 Android 共享元素转换

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:30 24 4
gpt4 key购买 nike

我有 2 个 fragment : fragment 1 包含 recyclerview, fragment 2 包含所选项目的详细 View 。这两个 fragment 之间有一个共享元素 imageview。从 fragment 1 过渡到 fragment 2 时,我想看到标准的共享元素过渡效果,但它似乎不适用于 recyclerview。因为如果我在第一个 fragment 中使用另一个单独的 View 而不是 recyclerview,它会显示正确的过渡。请注意,我已将基于位置的唯一转换名称分配给 recyclerview 项目图像。

代码如下:我的 Activity .java

public class MyActivity extends Activity {

private static final String TAG = MyActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.main_container, new PlaceholderFragment())
.commit();
}
}

public static class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(View view, int position);
}

private OnItemClickListener mItemClickListener;

List<Integer> items = new ArrayList<Integer>();
public void setOnItemClickListener(OnItemClickListener listener) {
this.mItemClickListener = listener;
}


public ImageAdapter() {
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
items.add(1);
}

@Override
public ImageAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
viewHolder.container.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(view, i);
}
}
});
viewHolder.imgDummy.setTransitionName("test" + i);
}

@Override
public int getItemCount() {
return items.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public View container;
public View imgDummy;

public ViewHolder(View itemView) {
super(itemView);
container = itemView.findViewById(R.id.container);
imgDummy = itemView.findViewById(R.id.imgDummy);
}
}
}
/**
* A placeholder fragment containing a simple view.
*/
@SuppressLint("ValidFragment")
public class PlaceholderFragment extends Fragment {

private RelativeLayout.LayoutParams lp;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
final View imgDummy2 = rootView.findViewById(R.id.imgDummy2);
imgDummy2.setTransitionName("test");

RecyclerView list = (RecyclerView) rootView.findViewById(R.id.streams_list);
list.setLayoutManager(new LinearLayoutManager(getActivity()));
ImageAdapter adapter = new ImageAdapter();
adapter.setOnItemClickListener(new ImageAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
View imgDummy = view.findViewById(R.id.imgDummy);

SecondFragment secondFragment = new SecondFragment();
secondFragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.trans_move));

secondFragment.setId(imgDummy.getTransitionName());
android.app.FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.main_container, secondFragment);
trans.addToBackStack(null);
trans.addSharedElement(imgDummy, imgDummy.getTransitionName());
trans.commit();
}
});
list.setAdapter(adapter);

return rootView;
}
}

public static class SecondFragment extends Fragment {
private String mId;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.second_fragment, container, false);
rootView.findViewById(R.id.imgDummy).setTransitionName(mId);
return rootView;
}

public void setId(String id) {
mId = id;
}
}
}

如果我更改以下两行:

secondFragment.setId(imgDummy.getTransitionName());
trans.addSharedElement(imgDummy, imgDummy.getTransitionName());

通过

secondFragment.setId(imgDummy2.getTransitionName());
trans.addSharedElement(imgDummy2, imgDummy2.getTransitionName());

然后我可以看到 imgDummy2 图像正确转换,可能是因为它在 recyclerview 之外。

知道如何在 recyclerview 中应用相同的内容吗?

最佳答案

我知道来晚了。但是一直在努力解决这个问题,最终得到了从 fragment A 中的回收者 View 到 fragment B 工作的过渡动画。

尝试在下面的行中更改 imgDummy.getTransitionName())

    trans.addSharedElement(imgDummy, imgDummy.getTransitionName());

到fragmentB中ImageView的transition名称

关于具有回收 View 和详细信息 View 的 fragment 之间的 Android 共享元素转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27999847/

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