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

时间:2018-01-11 04:57:06

标签: java android android-fragments android-activity

如何通过Activity将数据从Fragment ViewPager发送到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;
    }
}
}

2 个答案:

答案 0 :(得分:1)

这是一个示例,我通过viewpager adapter

从Activity向片段发送一个int值
 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);

和片段

@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);

}

答案 1 :(得分:0)

确定。我找到了解决方案。

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