将数据从Activity发送到TabLayout片段

时间:2017-10-18 13:25:13

标签: android android-viewpager android-tablayout

我正在设计一个我面临问题的应用程序,

我在 MainActivity.class 中有一个字符串日期,并且有两个片段。

InboundDate &的 OutboundDate

我是通过TabLayout& amp; ViewPager,我发送日期到片段&必须通过POST请求在两个片段上调用WebServices。

以下是我在MainActivity.class中的日期,我如何将这个日期发送给片段和片段调用jsonRequest并将Recyclerview设置为两个片段或者如果我将jsonRequest调用到MainActivity本身,那么我如何显示两个片段。

 date = getIntent().getStringExtra("DATE");
    Log.e("DATE",""+date);

ViewPager& TabLayout代码:

 private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new InBoundDate(),"INBOUND");
    adapter.addFrag(new OutBoundDate(),"OUTBOUND");
    viewPager.setAdapter(adapter);

}

//viewPager adapter ----- inbound & outbound
public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(android.support.v4.app.Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

1 个答案:

答案 0 :(得分:2)

我通过互联网搜索得到了这个问题的答案。 在这个问题中,我必须将 MainActivity.java 的字符串发送到同一个Activity上的片段。

注意:我们必须创建捆绑对象&amp;必须通过bundle发送字符串(任何dataType)。

我有两个片段 InBoundDate &amp;的 OutBoundDate

在setupViewpager()

中的 MainActivity.java
 private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    //bundle
    bundle =new Bundle();
    bundle.putString("DATE","2017-10-19");

    InBoundDate frag_in = new InBoundDate();
    OutBoundDate frag_out = new OutBoundDate();

    frag_in.setArguments(bundle);
    frag_out.setArguments(bundle);
    adapter.addFrag(frag_in,"INBOUND");
    adapter.addFrag(frag_out,"OUTBOUND");
    viewPager.setAdapter(adapter);

}

ViewPager适配器:

  //viewPager adapter ----- inbound & outbound
public class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(android.support.v4.app.Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

在你的片段中通过这种方式获取字符串:

    String dateTxt;
    private Bundle bundle;

    //within OncreateView
     bundle = this.getArguments();
        dateTxt = bundle.getString("DATE");
        Log.e("DATE_FRAG_INBOUND",""+dateTxt);