Android共享元素返回转换从11 recyclerview元素及以上开始工作

b91juud3  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(148)

我想从回收视图的两个文本视图有一个正确的过渡时,返回。现在它的工作非常奇怪,我的意思是返回过渡的两个文本视图开始从11元素,然后它是我想要的,但在此之前,只有一个文本视图(标题)有一个返回过渡。为什么然后返回过渡开始从11项目,以及如何有这个过渡当然从第一个元素回收视图列表?
这是我的代码和解释。
我有一个recyclerview,它可以动态地为两个文本视图设置transitionname:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Note noteItem = noteItemList.get(position);

    holder.noteTitleText.setText(noteItem.getNoteTitle());
    ViewCompat.setTransitionName(holder.noteTitleText, "title" + position);
    holder.noteDetailsText.setText(noteItem.getNoteDetails());
    ViewCompat.setTransitionName(holder.noteDetailsText, "details" + position);
   (...)
}

在这个适配器中,我有ViewHolder,在其中的父视图上,我像这样设置onclick:

rlNumberToGradient.setOnClickListener(view -> noteTitleDetailsOnClickAction());

其中

private void noteTitleDetailsOnClickAction() {
        boolean edit = true;
        if (fragment instanceof FragmentMain) {
            list = db.getAllNotes();
            isOnMove = ((FragmentMain) fragment).isOnMove;
        } else if (fragment instanceof FragmentFav) {
            list = db.getFavNotes();
        } else if (fragment instanceof FragmentSearch) {
            list = db.getSearchNotes(((FragmentSearch) fragment).onChange());
        }

        if (!isOnMove) {
            Bundle bundle = new Bundle();
            bundle.putBoolean("edit", edit);
            bundle.putInt("position", list.get(getAdapterPosition()).getId());
            ((ActivityMain) fragment.requireActivity()).onPassValuesToFragmentShowNote(bundle, noteTitleText, noteDetailsText);
        }
        isOnMove = false;
    }

我不关心isOnMove变量,为此,我们假设它始终为真。
一旦我点击了这个rlNumberToGradient视图,就会调用main activity中的onPassValuesToFragmentShowNote方法,如下所示:

@Override
public void onPassValuesToFragmentShowNote(Bundle bundle, TextView tvTitle, TextView tvDetails) {
    bundle.putString("transition_name_title", ViewCompat.getTransitionName(tvTitle));
    bundle.putString("title", tvTitle.getText().toString());
    bundle.putString("transition_name_details", ViewCompat.getTransitionName(tvDetails));
    bundle.putString("details", tvDetails.getText().toString());
    fragmentShowNote.setArguments(bundle);

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setReorderingAllowed(true);
    ft.addSharedElement(tvTitle, ViewCompat.getTransitionName(tvTitle));
    ft.addSharedElement(tvDetails, ViewCompat.getTransitionName(tvDetails));
    ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
    ft.addToBackStack(fragmentShowNote.getClass().getSimpleName());
    ft.replace(R.id.content_frame, fragmentShowNote);
    ft.commit();
}

正如我们所看到的片段显示出来了。这里是关键代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    sp = PreferenceManager.getDefaultSharedPreferences(requireContext());
    setSharedElementEnterTransition(TransitionInflater.from(requireContext()).inflateTransition(R.transition.change_image_transform));
    setSharedElementReturnTransition(TransitionInflater.from(requireContext()).inflateTransition(R.transition.change_image_transform));
    setExitTransition(TransitionInflater.from(requireContext()).inflateTransition(R.transition.change_image_transform));
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_show_note, container, false);

    if (getArguments() != null) {
        Bundle b = getArguments();
        position = b.getInt("position");
        transitionNameTitle = b.getString("transition_name_title");
        title = b.getString("title");
        transitionNameDetails = b.getString("transition_name_details");
        details = b.getString("details");
    }
    
    return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (((ActivityMain) requireActivity()).getSupportActionBar() != null) {
        ((ActivityMain) requireActivity()).getSupportActionBar().hide();
    }
    Toolbar toolbar = view.findViewById(R.id.toolbar);
    ViewCompat.setTransitionName(toolbar, transitionNameTitle);
    ((ActivityMain) requireActivity()).setSupportActionBar(toolbar);
    ((AppBarLayout) view.findViewById(R.id.app_bar_layout)).setExpanded(true);
    toolbar.setTitle(title);
    TextView tvNoteDescValue = view.findViewById(R.id.note_desc_value);
    ViewCompat.setTransitionName(tvNoteDescValue, transitionNameDetails);
    tvNoteDescValue.setText(details);
}
x8diyxa7

x8diyxa71#

解决了。原来是不兼容。我试着在工具栏和文本视图上设置转换。一旦我用这个方法从工具栏获得文本视图,一切都很好:

public static TextView getTextViewTitle(Toolbar toolbar){
    TextView textViewTitle = null;
    for(int i = 0; i<toolbar.getChildCount(); i++) {
        View view = toolbar.getChildAt(i);
        if(view instanceof TextView) {
            textViewTitle = (TextView) view;
            break;
        }
    }
    return textViewTitle;
}

相关问题