gpt4 book ai didi

java - 通过 FragmentPagerAdapter 将数据从 Activity 传递到 ViewPager 的 Fragment

转载 作者:行者123 更新时间:2023-12-02 11:45:59 25 4
gpt4 key购买 nike

如何通过FragmentPagerAdapter将数据从Activity发送到ViewPagerFragment

谢谢!

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 fragment

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;
}
}
}

最佳答案

这是一个示例,我通过 viewpager 适配器将 int 值从 Activity 发送到 fragment

 public class MyJobsHistoryServiceProviderPagerAdapter extends FragmentPagerAdapter {

int type;

public MyJobsHistoryServiceProviderPagerAdapter(FragmentManager fm) {
super(fm);
}

public MyJobsHistoryServiceProviderPagerAdapter(FragmentManager fm,int type) {
super(fm);
this.type=type;
}

@Override
public Fragment getItem(int position) {

MyJobsHistoryFragment fragment = new MyJobsHistoryFragment();
Bundle bundle = new Bundle();
bundle.putInt(Constants.POSITION, status);
bundle.putInt(Constants.TYPE,type);
fragment.setArguments(bundle);

return fragment;
}
}

设置 ViewPager 适配器

pagerAdapter = new MyJobsHistoryServiceProviderPagerAdapter(getSupportFragmentManager(), Constants.USER_TYPE_SERVICE_PROVIDER);
mViewPager.setAdapter(pagerAdapter);

和 fragment

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

status = getArguments().getInt(Constants.POSITION, 0);
type = getArguments().getInt(Constants.TYPE, 0);
return inflater.inflate(R.layout.fragment_myjobs_history, container, false);

}

关于java - 通过 FragmentPagerAdapter 将数据从 Activity 传递到 ViewPager 的 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200011/

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