本文介绍了通过FragmentPagerAdapter将数据从Activity传递到ViewPager的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过 FragmentPagerAdapter
将数据从 Activity
发送到 ViewPager
的 Fragment
?
How is possible to send data to from Activity
to Fragment
of ViewPager
via FragmentPagerAdapter
?
谢谢!
AppCompatActivity
public class UnitDetailsActivity extends AppCompatActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unit_details);
viewPager = (android.support.v4.view.ViewPager)findViewById(R.id.container);
// Getting unitID via and pass it to Fragment ???
int unitID = getIntent().getStringExtra("SelectedUnitDeviceId");
// And how to pass it to Fragment?
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
adapter = new ViewPagerUnitDetailsAdapter(this.getSupportFragmentManager());
fragmentReportHauls = new FragmentReportHauls();
fragmentReportsDowntime = new FragmentReportsDowntime();
adapter.addFragment(fragmentReportHauls);
adapter.addFragment(fragmentReportsDowntime);
viewPager.setAdapter(adapter);
}
}
FragmentPagerAdapter
public class ViewPagerUnitDetailsAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
public ViewPagerUnitDetailsAdapter (FragmentManager manager) {
super(manager);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
@Override
public Fragment getItem(int position) {
if(position == 0){
// How to pass here unitID ???
return FragmentReportHauls.newInstance(????);
}
else {
return FragmentReportsDowntime.newInstance(???);
}
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0){
return FragmentReportHauls.PAGE_TITLE;
}
else {
return FragmentReportsDowntime.PAGE_TITLE;
}
}
}
ViewPager片段
public class FragmentReportHauls extends Fragment {
public static final String PAGE_TITLE = "Shift";
private final String unitID;
public FragmentReportHauls() {
}
// Here I want to get unitID
public static FragmentReportHauls newInstance(String unitID) {
FragmentReportHauls fragment = new FragmentReportHauls();
this.unitID = unitID;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_report_hauls, container, false);
return view;
}
}
}
推荐答案
好.我找到了解决方法.
Ok. I found solution.
AppCompatActivity
public class UnitDetailsActivity extends AppCompatActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_unit_details);
viewPager = (android.support.v4.view.ViewPager)findViewById(R.id.container);
// Getting unitID via and pass it to Fragment ???
int unitID = getIntent().getStringExtra("SelectedUnitDeviceId");
// And how to pass it to Fragment?
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
adapter = new ViewPagerUnitDetailsAdapter(this.getSupportFragmentManager());
fragmentReportHauls = new FragmentReportHauls();
fragmentReportsDowntime = new FragmentReportsDowntime();
adapter.addFragment(fragmentReportHauls, unitID);
adapter.addFragment(fragmentReportsDowntime, unitID);
viewPager.setAdapter(adapter);
}
}
FragmentPagerAdapter
public class ViewPagerUnitDetailsAdapter extends FragmentPagerAdapter {
private final String unitID;
private final List<Fragment> mFragmentList = new ArrayList<>();
public ViewPagerUnitDetailsAdapter (FragmentManager manager) {
super(manager);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String unitID) {
mFragmentList.add(fragment);
this.unitID = unitID;
}
@Override
public Fragment getItem(int position) {
if(position == 0){
return FragmentReportHauls.newInstance(this.unitID);
}
else {
return FragmentReportsDowntime.newInstance(this.unitID);
}
}
@Override
public CharSequence getPageTitle(int position) {
if(position == 0){
return FragmentReportHauls.PAGE_TITLE;
}
else {
return FragmentReportsDowntime.PAGE_TITLE;
}
}
}
ViewPager片段
public class FragmentReportHauls extends Fragment {
public static final String PAGE_TITLE = "Shift";
private final String unitID;
public FragmentReportHauls() {
}
// Here I want to get unitID
public static FragmentReportHauls newInstance(String unitID) {
FragmentReportHauls fragment = new FragmentReportHauls();
this.unitID = unitID;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_report_hauls, container, false);
return view;
}
}
}
这篇关于通过FragmentPagerAdapter将数据从Activity传递到ViewPager的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!