我遇到了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) {
}
}));
}
关于我做错了什么的想法?
答案 0 :(得分:1)
我的错误在于这一行:
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(getActivity(), view, "stateImage");
我正在传递视图,在这种情况下是行。这是我修复它的方式
final ImageView image = (ImageView)
view.findViewById(R.id.stateImage);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(getActivity(), image, "stateImage");