gpt4 book ai didi

android - sharedElment 过渡使用不过渡的 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:38 27 4
gpt4 key购买 nike

AndroidStudio 2.3 Beta 1

我正在尝试让过渡与 fragment 一起工作,但我使用的图像如何不过渡。它只是正常弹出。

我已经创建了一个简单的应用程序来尝试让它工作。我有 2 个 fragment ListMovieFragmentDetailMovieFragment。和 1 个 MainActivity

用户将单击 ListMovieFragment 中的图像以转换到 DetailMovieFragment

这是我的转换 xml change_image_transform:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeImageTransform/>
</transitionSet>

列表电影 fragment :

public class ListMovieFragment extends Fragment {
public interface MovieSelectedListener {
void onMovieSelected(int movieId);
}
private MovieSelectedListener mMovieSelectedListener;

public ListMovieFragment() {
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
mMovieSelectedListener = (MovieSelectedListener)context;
}

@Override
public void onDetach() {
super.onDetach();
mMovieSelectedListener = null;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_list, container, false);
final ImageView ivMoviePoster = (ImageView)view.findViewById(R.id.ivMoviePoster);

ivMoviePoster.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mMovieSelectedListener != null) {
mMovieSelectedListener.onMovieSelected(12345);
}
}
});

Glide.with(getActivity())
.load("https://image.tmdb.org/t/p/w185/qjiskwlV1qQzRCjpV0cL9pEMF9a.jpg")
.placeholder(R.drawable.placeholder_poster)
.centerCrop()
.crossFade()
.into(ivMoviePoster);

return view;
}
}

详细电影 fragment

public class DetailMovieFragment extends Fragment {
public DetailMovieFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_detail, container, false);

final ImageView ivMovieDetailPoster = (ImageView)view.findViewById(R.id.ivMovieDetailPoster);

Glide.with(getActivity())
.load("https://image.tmdb.org/t/p/w185/qjiskwlV1qQzRCjpV0cL9pEMF9a.jpg")
.placeholder(R.drawable.placeholder_poster)
.centerCrop()
.crossFade()
.into(ivMovieDetailPoster);

return view;
}
}

主要 Activity

public class MainActivity extends AppCompatActivity implements ListMovieFragment.MovieSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if(savedInstanceState == null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.activity_main, new ListMovieFragment(), "listmoviefragment");
fragmentTransaction.commit();
}
}

@Override
public void onMovieSelected(int movieId) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
/* Get the fragments that will be using the transition */
ListMovieFragment listMovieFragment = new ListMovieFragment();
DetailMovieFragment detailMovieFragment = new DetailMovieFragment();

/* Inflate the transition */
Transition changeTransition = TransitionInflater
.from(MainActivity.this)
.inflateTransition(R.transition.change_image_transform);

Transition explodeTransition = TransitionInflater
.from(MainActivity.this)
.inflateTransition(android.R.transition.explode);

/* Set the exit and return on the source fragment (ListMovieFragment) */
listMovieFragment.setSharedElementReturnTransition(changeTransition);
listMovieFragment.setExitTransition(explodeTransition);

/* Set the enter on the destination fragment (MovieDetailFragment) */
detailMovieFragment.setSharedElementEnterTransition(changeTransition);
detailMovieFragment.setEnterTransition(explodeTransition);

/* Get the shared imageview from the source fragment (MovieListFragment) */
final ImageView ivSharedImage = (ImageView)findViewById(R.id.ivMoviePoster);

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_main, new DetailMovieFragment(), "detailmoviefragment");
fragmentTransaction.addToBackStack("detailmoviefragment");
fragmentTransaction.addSharedElement(ivSharedImage, getResources().getString(R.string.transition_poster_image));
fragmentTransaction.commit();
}
else {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_main, new DetailMovieFragment(), "detailmoviefragment");
fragmentTransaction.addToBackStack("detailmoviefragment");
fragmentTransaction.commit();
}
}

@Override
public void onBackPressed() {
if(getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStackImmediate();
}
else {
super.onBackPressed();
}
}
}

ListMovieFragment 的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="6dp">

<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ivMoviePoster"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/placeholder_poster"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:transitionName="@string/transition_poster_image">
</ImageView>
</LinearLayout>

DetailMovieFragment 的布局:

<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="me.androidbox.fragmenttransitions.detail.DetailMovieFragment">
<ImageView
android:id="@+id/ivMovieDetailPoster"
android:layout_width="140dp"
android:layout_height="160dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="112dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_gravity="end"
android:transitionName="@string/transition_poster_image"/>
</FrameLayout>

转换名称的字符串名称:

<string name="transition_poster_image">imagePoster</string>

实现看起来很简单,所以我认为我忽略了一些错误。

非常感谢您的任何建议,

最佳答案

您的代码中有两个问题,在您的 onMovieSelected 方法中:

  1. 您正在创建 ListMovieFragment新实例,然后对其应用转换逻辑。但是您忘记了您已经拥有该 fragment 的实例(您是在 onCreate 方法中创建的)。因此,您需要从 FragmentManager 中检索现有的 ListMovieFragment 对象,并将您的过渡应用到它。
  2. 您正在将转换逻辑应用于 DetailMovieFragment 的一个实例,但随后突然将 ListMoveFragment 替换为新实例。

所以你固定的 onMovieSelected 方法将是:

@Override
public void onMovieSelected(int movieId) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
/* Get the fragments that will be using the transition */
ListMovieFragment listMovieFragment = (ListMovieFragment) getSupportFragmentManager().findFragmentByTag("listmoviefragment");
DetailMovieFragment detailMovieFragment = new DetailMovieFragment();

/* Inflate the transition */
Transition changeTransition = TransitionInflater
.from(MainActivity.this)
.inflateTransition(R.transition.change_image_transform);

Transition explodeTransition = TransitionInflater
.from(MainActivity.this)
.inflateTransition(android.R.transition.explode);

/* Set the exit and return on the source fragment (ListMovieFragment) */
listMovieFragment.setSharedElementReturnTransition(changeTransition);
listMovieFragment.setExitTransition(explodeTransition);

/* Set the enter on the destination fragment (MovieDetailFragment) */
detailMovieFragment.setSharedElementEnterTransition(changeTransition);
detailMovieFragment.setEnterTransition(explodeTransition);

/* Get the shared imageview from the source fragment (MovieListFragment) */
final ImageView ivSharedImage = (ImageView)findViewById(R.id.ivMoviePoster);

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_main, detailMovieFragment, "detailmoviefragment");
fragmentTransaction.addToBackStack("detailmoviefragment");
fragmentTransaction.addSharedElement(ivSharedImage, getResources().getString(R.string.transition_poster_image));
fragmentTransaction.commit();
} else {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_main, new DetailMovieFragment(), "detailmoviefragment");
fragmentTransaction.addToBackStack("detailmoviefragment");
fragmentTransaction.commit();
}
}

现在应该可以了。

P.S. 我已经使用并运行了您的代码,发现您有一些奇怪的转换,所以如果您遇到一些问题,请查看此 great article , 如何使用 fragment 进行平滑且用户友好的转换:)

关于android - sharedElment 过渡使用不过渡的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41289234/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com