我有一个安卓应用程序,包含2个片段。第一个片段包含一个RecyclerView,用于显示联系人列表。以下是第一个片段的代码:
public class NotesFragment extends Fragment implements ContactsAdapter.OnContactClicked {
public NotesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_notes, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView rvContacts = view.findViewById(R.id.rvContacts);
rvContacts.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
List<Contact> contacts = new ArrayList<Contact>(){{
add(new Contact(1,"Antoine El Murr"));
add(new Contact(2,"Joelle Abou Chakra"));
add(new Contact(3,"Michel Jebran"));
add(new Contact(4,"Charlie Abou Moussa"));
add(new Contact(5,"Edward Salameh"));
add(new Contact(6,"Makram Merdass"));
add(new Contact(7,"Sami Abou Jaoudeh"));
add(new Contact(8,"Elie Nassif"));
add(new Contact(9,"Samer Abou Samra"));
add(new Contact(10,"Fady Ammoury"));
add(new Contact(11,"Dany Fahed"));
add(new Contact(12,"Mariana Haddad"));
add(new Contact(13,"Mohamad Restom"));
}};
ContactsAdapter adapter = new ContactsAdapter(contacts);
adapter.setOnContactClicked(this);
rvContacts.setAdapter(adapter);
}
@Override
public void onContactClicked(int position, Contact contact, View holderView) {
NavController navController = Navigation.findNavController(holderView);
Bundle arguments = new Bundle();
arguments.putParcelable("contact", contact);
FragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder()
.addSharedElement(holderView.findViewById(R.id.tvIcon), ViewCompat.getTransitionName(holderView.findViewById(R.id.tvIcon)))
.addSharedElement(holderView.findViewById(R.id.tvName), ViewCompat.getTransitionName(holderView.findViewById(R.id.tvName)))
.build();
navController.navigate(R.id.ContactToDetails, arguments, null, extras);
}
}
我正在使用Android Jetpack Navigation组件。在RecyclerView的元素布局文件中,我正在设置每个元素的过渡名称,如下所示:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/circular_shape"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:transitionName="icon"
tools:text="A" />
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="8dp"
android:gravity="start"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/tvIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tvIcon"
app:layout_constraintTop_toTopOf="@+id/tvIcon"
android:transitionName="name"
tools:text="Antoine El Murr" />
</androidx.constraintlayout.widget.ConstraintLayout>
我在适配器中创建了一个接口,当联系人被点击时触发该函数。
在我的第二个片段中,我已经在XML布局中设置了过渡名称,如下所示:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ContactDetailsFragment">
<TextView
android:id="@+id/tvIcon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/circular_shape"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="A"
android:transitionName="icon"/>
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="8dp"
android:gravity="start"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/tvIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tvIcon"
app:layout_constraintTop_toTopOf="@+id/tvIcon"
tools:text="Antoine El Murr"
android:transitionName="name"/>
</androidx.constraintlayout.widget.ConstraintLayout>
在onCreate方法中设置进入动画过渡效果,但它没有起作用:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
请帮忙,我在互联网上搜索了解决方法,但是没有成功,所有示例都是关于旧导航系统的。新示例是如何从一个片段到另一个片段使用SharedElements,但不是从RecyclerView,而且安卓官网上的文档很少,也不涵盖使用新导航组件实现此过渡效果的所有步骤。