如何将信息传递到 ViewPager 选项卡内的片段?
How Can I Pass Information to my Fragments Inside ViewPager Tabs?
我有一个主主页 activity,它由屏幕底部的 3 个选项卡组成。
在这个activity
里面我有一个fragment
.
此片段包含 view pager
。此视图寻呼机用于在 2 个活动之间导航:
1.Exercise 表格 = 这将显示练习视频。
2.Exercise历史=这显示了所有用户记录的练习数据。
要获得练习形式的视频以及练习历史,我需要能够从 ProgressFagment2(屏幕顶部的文本)访问 ExerciseName 并将其传递给 2 个子片段(ExerciseHomeVideoFragment 和练习历史片段)
如何访问子片段中的 exerciseName
?
ProgressFragment2(片段内部 Activity)
public class Progress_Fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.exercises_history_demo, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
childExerciseName = getArguments().getString("ExerciseName");
TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
title_tv.setText(childExerciseName);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
ViewPager mViewPager = (ViewPager) getView().findViewById(R.id.view_pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) getView().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.top_navigation_two, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment();
break;
case 1:
fragment = new ExerciseHistoryFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Exercise Form";
case 1:
return "Exercise History";
}
return null;
}
}
}
ExerciseHomeVideoFragment(viewPager 中的 2 个片段之一)|仅相关代码
public class ExerciseHomeVideoFragment extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onViewCreated(View view, Bundle savedInstanceState) {
Progress_Fragment2 parentFrag = ((Progress_Fragment2)ExerciseHomeVideoFragment.this.getParentFragment());
youTubeBtn = getView().findViewById(R.id.youTubeSearchButton);
parent_tv = getView().findViewById(R.id.textView_exerciseName8);
childExerciseViewModel = ViewModelProviders.of(this).get(ChildExerciseViewModel.class);
//
// HOW CAN I ACCESS THE CHILDEXERCISE NAME???
//
// childExerciseName = getArguments().getString("ExerciseName"); // THIS DOESNT WORK
// exerciseNameTV.setText(childExerciseName + " Demo");
}
}
您可以使用接口、设置参数或 newInstance 发送数据
在您的选项卡片段中
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseName);
break;
case 1:
fragment = new ExerciseHomeVideoFragment();
break;
}
ExerciseHomeVideoFragment
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ExerciseHomeVideoFragment extends Fragment {
public static ExerciseHomeVideoFragment newInstance(String ExerciseName) {
Bundle args = new Bundle();
args.putString("ExerciseName", ExerciseName);
ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onViewCreated(View view, Bundle savedInstanceState) {
TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
//
Bundle bundle = getArguments();
if(bundle!=null)
{
title_tv.setText(bundle.getString("ExerciseName"));
}
}
}
你也可以发送模型,但是模型应该使用Serializer实现
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseViewModel);
break;
case 1:
fragment = new ExerciseHomeVideoFragment();
break;
}
片段
public static ExerciseHomeVideoFragment newInstance(String ExerciseName,ChildExerciseViewModel childExerciseViewModel) {
Bundle args = new Bundle();
args.putString("ExerciseName", ExerciseName);
args.putSerializable("model", childExerciseViewModel);
ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
fragment.setArguments(args);
return fragment;
}
Bundle bundle = getArguments();
if(bundle!=null)
{
title_tv.setText(bundle.getString("ExerciseName"));
ChildExerciseViewModel model = (ChildExerciseViewModel) bundle.getSerializable("model");
title_tv.setText(model.gettourExerciseText());
}
我认为使用更好的架构是个好主意。例如,如果你使用单例 class 来存储你的数据,你将能够在不传递给片段的情况下使用它们。搜索单例。
Android 有一项新功能已在 3 个月后发布。你可以使用 setFragmentResultListener。
这是文档:
https://developer.android.com/reference/androidx/fragment/app/FragmentManager#setfragmentresultlistener
我有一个主主页 activity,它由屏幕底部的 3 个选项卡组成。
在这个activity
里面我有一个fragment
.
此片段包含 view pager
。此视图寻呼机用于在 2 个活动之间导航:
1.Exercise 表格 = 这将显示练习视频。
2.Exercise历史=这显示了所有用户记录的练习数据。
要获得练习形式的视频以及练习历史,我需要能够从 ProgressFagment2(屏幕顶部的文本)访问 ExerciseName 并将其传递给 2 个子片段(ExerciseHomeVideoFragment 和练习历史片段)
如何访问子片段中的 exerciseName
?
ProgressFragment2(片段内部 Activity)
public class Progress_Fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.exercises_history_demo, container, false);
}
public void onViewCreated(View view, Bundle savedInstanceState) {
childExerciseName = getArguments().getString("ExerciseName");
TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
title_tv.setText(childExerciseName);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
ViewPager mViewPager = (ViewPager) getView().findViewById(R.id.view_pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) getView().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.top_navigation_two, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment();
break;
case 1:
fragment = new ExerciseHistoryFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Exercise Form";
case 1:
return "Exercise History";
}
return null;
}
}
}
ExerciseHomeVideoFragment(viewPager 中的 2 个片段之一)|仅相关代码
public class ExerciseHomeVideoFragment extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onViewCreated(View view, Bundle savedInstanceState) {
Progress_Fragment2 parentFrag = ((Progress_Fragment2)ExerciseHomeVideoFragment.this.getParentFragment());
youTubeBtn = getView().findViewById(R.id.youTubeSearchButton);
parent_tv = getView().findViewById(R.id.textView_exerciseName8);
childExerciseViewModel = ViewModelProviders.of(this).get(ChildExerciseViewModel.class);
//
// HOW CAN I ACCESS THE CHILDEXERCISE NAME???
//
// childExerciseName = getArguments().getString("ExerciseName"); // THIS DOESNT WORK
// exerciseNameTV.setText(childExerciseName + " Demo");
}
}
您可以使用接口、设置参数或 newInstance 发送数据
在您的选项卡片段中
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseName);
break;
case 1:
fragment = new ExerciseHomeVideoFragment();
break;
}
ExerciseHomeVideoFragment
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ExerciseHomeVideoFragment extends Fragment {
public static ExerciseHomeVideoFragment newInstance(String ExerciseName) {
Bundle args = new Bundle();
args.putString("ExerciseName", ExerciseName);
ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_exercise_info_home_version, container, false);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onViewCreated(View view, Bundle savedInstanceState) {
TextView title_tv = getView().findViewById(R.id.textView_exerciseName8);
//
Bundle bundle = getArguments();
if(bundle!=null)
{
title_tv.setText(bundle.getString("ExerciseName"));
}
}
}
你也可以发送模型,但是模型应该使用Serializer实现
switch (position) {
case 0:
fragment = new ExerciseHomeVideoFragment().newInstance(childExerciseViewModel);
break;
case 1:
fragment = new ExerciseHomeVideoFragment();
break;
}
片段
public static ExerciseHomeVideoFragment newInstance(String ExerciseName,ChildExerciseViewModel childExerciseViewModel) {
Bundle args = new Bundle();
args.putString("ExerciseName", ExerciseName);
args.putSerializable("model", childExerciseViewModel);
ExerciseHomeVideoFragment fragment = new ExerciseHomeVideoFragment();
fragment.setArguments(args);
return fragment;
}
Bundle bundle = getArguments();
if(bundle!=null)
{
title_tv.setText(bundle.getString("ExerciseName"));
ChildExerciseViewModel model = (ChildExerciseViewModel) bundle.getSerializable("model");
title_tv.setText(model.gettourExerciseText());
}
我认为使用更好的架构是个好主意。例如,如果你使用单例 class 来存储你的数据,你将能够在不传递给片段的情况下使用它们。搜索单例。 Android 有一项新功能已在 3 个月后发布。你可以使用 setFragmentResultListener。 这是文档: https://developer.android.com/reference/androidx/fragment/app/FragmentManager#setfragmentresultlistener