共享元素在片段之间转换而不是动画

时间:2017-07-30 18:07:47

标签: android android-fragments element transition shared

所需的共享元素没有动画,它们只是显示/淡入而不是从一个视图移动到另一个视图。什么可能是错的?我已经阅读了多个教程,但没有一个有效。这肯定是一件小事...... 我正在测试AVD 6.0。

感谢您的帮助!

应用程序通过 MainActivity 加载起始片段

开始
onMessageReceived()

它带您进入起始片段 select dancer

启动fragment.java

public void openFindDancerFragment() {
    FindDancerFragment findDancerFragment = new FindDancerFragment();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.main_fragment_container, findDancerFragment)
            .addToBackStack(null)
            .commit();
}

启动片段XML

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_find_dancer, container, false);
    DancerItemAdapter adapter = new DancerItemAdapter(getContext(), dancers);
    ListView dancersView = (ListView)v.findViewById(R.id.dancersListView);

    dancersView.setAdapter(adapter);
    dancersView.setOnItemClickListener(this);
    return v;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    ImageView imageView = (ImageView) view.findViewById(R.id.dancerIcon);
    TextView textView = (TextView) view.findViewById(R.id.dancerName);
    String imageTransitionName = getString(R.string.dancer_icon_transition);
    String textTransitionName = getString(R.string.dancer_name_transition);
    DancerDetailsFragment detailsFragment = new DancerDetailsFragment();

    Bundle bundle = new Bundle();
    bundle.putString("NAME_TEXT", textView.getText().toString());
    bundle.putParcelable("IMAGE", ((BitmapDrawable) imageView.getDrawable()).getBitmap());
    detailsFragment.setArguments(bundle);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setSharedElementReturnTransition(TransitionInflater.from(
                getActivity()).inflateTransition(android.R.transition.fade));
        setExitTransition(TransitionInflater.from(
                getActivity()).inflateTransition(android.R.transition.fade));

        detailsFragment.setSharedElementEnterTransition(TransitionInflater.from(
                getActivity()).inflateTransition(R.transition.dancer_item_transition));
        detailsFragment.setEnterTransition(TransitionInflater.from(
                getActivity()).inflateTransition(R.transition.dancer_item_transition));
    }

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .addToBackStack(null)
            .addSharedElement(imageView, imageTransitionName)
            .addSharedElement(textView, textTransitionName)
            .replace(R.id.find_dancer_fragment, detailsFragment)
            .commit();
}

点击列表项会带您进入结束片段end fragment

结束片段

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/find_dancer_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
tools:context="com.example.ginold.dance.Fragments.FindDancerFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">
    <ListView
        android:id="@+id/dancersListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

结束片段XML

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(R.transition.dancer_item_transition));
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    Bitmap imageBitmap = null;
    String dancerName = "";
    View view = inflater.inflate(R.layout.fragment_dancer_details, container, false);

    if (bundle != null) {
        imageBitmap = bundle.getParcelable("IMAGE");
        dancerName = bundle.getString("NAME_TEXT");
    }

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.findViewById(R.id.dancerImageDetails).setTransitionName(getString(R.string.dancer_icon_transition));
        view.findViewById(R.id.dancerNameDetails).setTransitionName(getString(R.string.dancer_name_transition));
    }

    ((ImageView) view.findViewById(R.id.dancerImageDetails)).setImageBitmap(imageBitmap);
    ((TextView) view.findViewById(R.id.dancerNameDetails)).setText(dancerName);

    return view;
}

舞者项目转换XML

<RelativeLayout 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"
    android:background="@color/colorPrimaryDark"
    tools:context="com.example.ginold.dance.Fragments.DancerDetailsFragment">

<TextView
    android:id="@+id/dancerNameDetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_marginStart="113dp"
    android:text="chuhj"
    android:textColor="@color/colorAccent"
    android:textSize="50dp"
    android:transitionName="@string/dancer_name_transition" />

<ImageView
    android:id="@+id/dancerImageDetails"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_marginStart="12dp"
    android:layout_marginTop="103dp"
    android:transitionName="@string/dancer_icon_transition" />

1 个答案:

答案 0 :(得分:0)

您已将transitionName添加到End Fragment xml中,因此无需再次设置即可删除它将起作用的代码。它应该是这样的:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
 Bundle savedInstanceState) {
          Bundle bundle = getArguments();
          Bitmap imageBitmap = null;
          String dancerName = "";
          View view = inflater.inflate(R.layout.fragment_dancer_details, container, false);

          if (bundle != null) {
              imageBitmap = bundle.getParcelable("IMAGE");
              dancerName = bundle.getString("NAME_TEXT");
          }

          ((ImageView) view.findViewById(R.id.dancerImageDetails)).setImageBitmap(imageBitmap);
          ((TextView) view.findViewById(R.id.dancerNameDetails)).setText(dancerName);

          return view;
      }