Я хочу вызвать Detailfragment с помощью кнопки 1st MainFrargment, которую я пробовал в MainActivity, создав метод goToDetail (), но появился пустой вывод и ... во вкладке запуска я увидел это сообщение -> 'I / Choreographer: Пропущено 42 кадра! Приложение может выполнять слишком много работы над своим основным потоком. '
Вот мой код
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/...
viewPager = findViewById(R.id.fragment_container);
ArrayList<MovieData> movieData = new ArrayList<>();
movieData.add(new MovieData(1, " title1", "61.6%", "15"));
movieData.add(new MovieData(2, " title2", "50.6%", "12"));
/...
adapter = new MoviePagerAdapter(getSupportFragmentManager(), movieData);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(adapter.getCount());
}
/...
public void goToDetail(){
DetailFragment detailFragment = new DetailFragment();
FragmentManager detail_fgm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = detail_fgm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, detailFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
MainFragment
public class MainFragment extends androidx.fragment.app.Fragment {
public static MainFragment newInstance(MovieData movieData) {
Bundle bundle = new Bundle();
bundle.putParcelable("data", movieData);
MainFragment mainFragment = new MainFragment();
mainFragment.setArguments(bundle);
return mainFragment;
}
public MainFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
this.movieData = args.getParcelable("data");
}
if (movieData == null) {
throw new IllegalArgumentException("movieData would be null");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_frargment, container,
false);
image = view.findViewById(R.id.image);
setImage(movieData.getMovie_number());
Button detail_btn = view.findViewById(R.id.detail_btn);
detail_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DetailFragment detailFragment = new DetailFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
((MainActivity) Objects.requireNonNull(getActivity())).goToDetail();
}
}
});
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TextView) view.findViewById(R.id.title)).setText(movieData.movie_number + ". " + movieData.title);
((TextView) view.findViewById(R.id.reservation_rate)).setText(movieData.reservation_rate);
((TextView) view.findViewById(R.id.grade)).setText(" | " + movieData.grade + " | D-1");
}
/ ...}
DetailFragment (это всего лишь простой фрагмент с 3 кнопками. Я сделал это для теста)
public class DetailFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test, container, false);
return view;
}
}