将数据传递给片段并从另一个片段刷新它

时间:2017-05-31 22:42:51

标签: android android-fragments

我有3个Tab片段和一个菜单列表片段:

MainActivity:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div1">
  <h2>Nonono...</h2>
  <br>
  <p>bla bla bla bla... </p>
  <button class="lalala-btn">Go to lalala</button>
</div>

<div class="div2">
  <h2>lalala..</h2>
  <br>
  <ul>
    <li><p>some text</p></li>
    <li><p>some text</p></li>
    <li><p>some text</p></li>
    <li><p>some text</p></li>
    <li><p>some text</p></li>
    <li><p>some text</p></li>
  </ul>
  <button class="lalala-btn">Go to nonono</button>
</div>

PagerAdapter:

public class MainActivity extends AppCompatActivity {
    private FlowingDrawer mDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("TAB1"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB2"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB3"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

这是一个标签片段,我想要的是更改此类中的“cat”字符串并从另一个片段重新加载标签,下面的菜单片段:

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                TabFragment1 tab1 = new TabFragment1();
                return tab1;
            case 1:
                TabFragment2 tab2 = new TabFragment2();
                return tab2;
            case 2:
                TabFragment3 tab3 = new TabFragment3();
                return tab3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

MenuList Fragment,这是我需要重新加载标签的地方,并传递“cat”值:

public class TabFragment2 extends Fragment {

    RecyclerView recyclerView;
    public AVLoadingIndicatorView indicatorView;
    List<AdModel> Ads;
    AdsAdapter adapter;
    AdsApi api;
    int page = 1;
    String TAG = "TabFragment2";
    String cat = "0";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.tab_fragment_2, container, false);
        final FragmentActivity c = getActivity();

        recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        indicatorView= (AVLoadingIndicatorView) c.findViewById(R.id.indicator);
        Ads = new ArrayList<>();


        adapter = new AdsAdapter(c, Ads);
        adapter.setLoadMoreListener(new AdsAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        loadMore(page, cat);
                    }
                });
            }
        });

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, GridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);

        api = ServiceGenerator.createService(AdsApi.class);
        load(1, cat);

        return rootView;
    }

如何将片段中的值传递给另一个片段并重新加载?

修改 通过检查:Refresh data in ViewPager Fragment

回答

0 个答案:

没有答案