【问题标题】:Transition animation starting position wrong place过渡动画起始位置错误的地方
【发布时间】:2017-01-12 00:26:57
【问题描述】:

我在使用 android 转换时遇到问题。我有一个包含多个元素的 recyclerview 列表。点击时动画应该从任意行的图像开始,但不是,它从行的中间开始。

我有一个带有 RecyclerView 的片段,这里是过渡开始的地方

fragment_states:

<FrameLayout 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"
    tools:context="namespace.fragments.StatesFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewStates"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    android:layout_marginBottom="?attr/actionBarSize"
    />
</FrameLayout>

之前的 recyclerView 有一个列表适配器,其中一行定义如下所示。这里我定义了 android:transitionName="stateImage",是过渡应该从哪里开始的图像。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/imgState"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:transitionName="stateImage"
        android:padding="6dp" />
<TextView
    android:padding="10dp"
    android:layout_toRightOf="@id/imgState"
    android:id="@+id/txtNombreEstado"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
    />
</RelativeLayout>

我是这样称呼过渡的:

StatesFragment.java

public class StatesFragment extends Fragment {
...
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

 recyclerView.addOnItemTouchListener(new Helper.RecyclerTouchListener(getActivity(), recyclerView, new Helper.ClickListener() {
            @Override
            public void onClick(View view, int position) {

                try {    
                    Intent intent = new Intent(getActivity(), CitiesActivity.class);

                    //Check if we're running on Android 5.0 or higher
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        // Call some material design APIs here
                        ActivityOptionsCompat options = ActivityOptionsCompat.
                                makeSceneTransitionAnimation(getActivity(), view, "stateImage");
                        startActivity(intent, options.toBundle());
                    } else {
                        // Implement this feature without material design
                        startActivity(intent);
                    }


                }
                catch (Exception ex)
                {

                }
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));
}

知道我做错了什么吗?

【问题讨论】:

    标签: android android-fragments android-recyclerview android-transitions


    【解决方案1】:

    我的错误在这一行:

    ActivityOptionsCompat options = ActivityOptionsCompat.
                                    makeSceneTransitionAnimation(getActivity(), view, "stateImage");
    

    我正在传递视图,在这种情况下是行。我就是这样解决的

     final ImageView image = (ImageView)
                                view.findViewById(R.id.stateImage);
    
    ActivityOptionsCompat options = ActivityOptionsCompat.
                                    makeSceneTransitionAnimation(getActivity(), image, "stateImage");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2018-02-28
      相关资源
      最近更新 更多