将数据从 Activity 发送到 TabLayout 片段

Send Data from Activity to TabLayout fragments(将数据从 Activity 发送到 TabLayout 片段)

本文介绍了将数据从 Activity 发送到 TabLayout 片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个我遇到问题的应用程序,

I am designing a application in which i am facing problem,

我在 MainActivity.class 中有一个 String date; 并且有两个片段.

I have a String date; in MainActivity.class and there are two fragments.

入站日期 &出境日期

我是由 TabLayout & 设计的ViewPager,当我将日期发送到 Fragments & 时出现问题必须在两个片段上通过 POST 请求调用 WebServices.

I designed it by TabLayout & ViewPager, I am getting issue when i sending date to fragments & have to call WebServices by POST request on both the fragments.

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

Below is the date i have in MainActivity.class, How i send this date to both the fragments & call jsonRequest and set Recyclerview to both the fragments Or if i call the jsonRequest to MainActivity itself then how can i display to both the fragments.

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

ViewPager &标签布局代码:

ViewPager & TabLayout Code:

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

推荐答案

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

I got the answer of this question by searching over the internet. In this question i have to send string to MainActivity.java to fragments present on same Activity.

注意:我们必须创建捆绑对象 &必须通过捆绑发送字符串(任何数据类型).

Note : We have to create bundle object & have to send string(any dataType) by bundle.

我有两个片段 InBoundDate &OutBoundDate.

I have two fragments InBoundDate & OutBoundDate.

MainActivity.java中的setupViewpager()

In MainActivity.java in setupViewpager ()

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

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

In your Fragments get String by this way :

    String dateTxt;
    private Bundle bundle;

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

这篇关于将数据从 Activity 发送到 TabLayout 片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将数据从 Activity 发送到 TabLayout 片段

基础教程推荐