我有一个包含2个片段的android应用,第一个片段包含一个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导航组件 在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中输入enter过渡,但它不起作用:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
}
请帮忙,我上网冲浪寻求解决方案,但是所有示例都在旧的导航系统上运气不佳,而新示例则是如何从一个片段到另一个片段,而不是从recyclerview中使用SharedElements,并且android网站上的文档又少又少,并且没有涵盖使用新的导航组件实现此过渡的所有步骤
答案 0 :(得分:0)
确保布局之间具有匹配的transitionName(s)
。
android:transitionName="YOUR_TRANSITION_NAME"
然后确保像这样传递共享视图实例(您也可以使用safeArgs):
some_view.setOnClickListener{
val extras = FragmentNavigatorExtras(
yourSharedViewInstance to "YOUR_TRANSITION_NAME"
)
findNavController().navigate(R.id.your_destination_action, null, null, extras)
}
然后在您的目的地
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move /* or any other transition you want to use */)
}
那应该是:)
答案 1 :(得分:0)
我已经解决了这个问题,而不是将过渡名称放在xml元素中,而是将其放在ViewHolder(或Adapter)中,并为回收视图中的每个元素赋予唯一的过渡名称
示例:
位置0->过渡名称=“ image_view_transition_0”的元素
位置1->过渡名称=“ image_view_transition_1”
因此android知道要设置动画的元素,如果所有元素都具有相同的过渡名称,它将对要设置动画的元素感到困惑