我花了一些时间寻找一种解决方案,以在片段之间发送数据,而不是单击任何按钮,只能单击和/或单击底部导航栏,
因此,在我的情况下,我在同一活动中有一个Viewpager和三个Fragment。在第一个片段上,我有一个搜索栏,向我显示了Textview中的进度,在第二个片段上也是如此。我想将每个textview整数传递给第三个片段上的两个textview。但这应该在我滑动到最后一个片段时完成。希望您了解我想做的事并能提供帮助。
我尝试实现OnPause和OnStop,但是它不起作用。我可以调用其他方法吗?我了解,如果我将视图更改为其他片段,则应调用onswipe或onchangeview之类的调用以将数据发送到最后一个片段。
我的XML文件 fragment_holding(用于PageOne)具有一个搜索栏和一个显示进度的Textview fragment_wind(用于PageTwo)具有一个搜索栏和一个显示进度的Textview fragment_answer(用于PageTree)具有两个Textview来显示每个搜索栏的进度
FragmentAdapter
public class FragmentAdapter extends FragmentPagerAdapter {
private List<android.support.v4.app.Fragment> Fragment = new ArrayList<>(); //Fragment List
private List<String> NamePage = new ArrayList<>(); // Fragment Name List
public FragmentAdapter(FragmentManager manager) {
super(manager);
}
public void add(Fragment Frag, String Title) {
Fragment.add(Frag);
NamePage.add(Title);
}
@Override
public Fragment getItem(int position) {
return Fragment.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return NamePage.get(position);
}
@Override
public int getCount() {
return 3;
}
MainActivity
public class MainActivity extends AppCompatActivity{
ViewPager viewPager;
BottomNavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
viewPager = findViewById(R.id.viewpager); //Init Viewpager
setupFm(getSupportFragmentManager(), viewPager); //Setup Fragment
viewPager.setCurrentItem(0); //Set Currrent Item When Activity Start
viewPager.setOnPageChangeListener(new PageChange()); //Listeners For Viewpager When Page Changed
}
public static void setupFm(FragmentManager fragmentManager, ViewPager viewPager){
FragmentAdapter Adapter = new FragmentAdapter(fragmentManager);
//Add All Fragment To List
Adapter.add(new PageOne(), "Page One");
Adapter.add(new PageTwo(), "Page Two");
Adapter.add(new PageTree(), "Page Three");
viewPager.setAdapter(Adapter);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_dashboard:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_notifications:
viewPager.setCurrentItem(2);
return true;
}
return false;
}
};
public class PageChange implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
navigation.setSelectedItemId(R.id.navigation_home);
break;
case 1:
navigation.setSelectedItemId(R.id.navigation_dashboard);
break;
case 2:
navigation.setSelectedItemId(R.id.navigation_notifications);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
PageOne 公共类PageOne扩展了片段{
private SeekBar sbar;
private TextView tView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_holding, null);
sbar =(SeekBar) view.findViewById(R.id.seekBar5);
tView = (TextView) view.findViewById(R.id.textView6);
tView.setText(sbar.getProgress() + "/" + sbar.getMax());
sbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int pval = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pval = progress;
tView.setText(pval + "/" + seekBar.getMax());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//write custom code to on start progress
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return view;
}
PageTwo
public class PageTwo extends Fragment {
private SeekBar sbar4;
private TextView tView4;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_wind, null);
sbar4 =(SeekBar) view.findViewById(R.id.seekBar5);
tView4 = (TextView) view.findViewById(R.id.textView6);
tView4.setText(sbar4.getProgress() + "/" + sbar4.getMax());
sbar4.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int pval = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
pval = progress;
tView4.setText(pval + "/" + seekBar.getMax());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//write custom code to on start progress
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return view;
}
PageTree
公共类PageTree扩展了片段{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_answer, container, false);
return view;
}
答案 0 :(得分:0)
在您的适配器中执行此操作:-
@NonNull
@Override
public Fragment getItem(int position) {
GridFragment gridFragment = new GridFragment();
Bundle bundle = new Bundle();
bundle.putInt("position", position);
bundle.putString("survayType", survayType);
bundle.putInt("questionNo", questionNo);
gridFragment.setArguments(bundle);
return gridFragment;
}
在您的片段中是这样的:-
optionPosition = getArguments().getInt("position");
survayType = getArguments().getString("survayType");
questionNo = getArguments().getInt("questionNo");