Android - 将父活动中的参数连续发送到片段

时间:2014-04-05 18:32:27

标签: android android-fragments android-actionbar

我开发了一个带有acion bar的android应用程序,它有3个标签,包含3个不同的片段,我试图将参数从父活动传递给片段,我使用了THIS qusetion中给出的解决方案,并且每件事情都是正确的,唯一的问题是,现在活动只在启动时将此参数发送到片段一次,我需要在每次用户选择选项卡时获取此参数,我想我应该在onTabSelected方法上执行此操作但不知道怎么做,有什么建议吗?

2 个答案:

答案 0 :(得分:0)

试试这个!!

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

                    @Override
                    public void onTabSelected(Tab tab, FragmentTransaction ft) {
                        // Pass the position on tab click to ViewPager
                        mPager.setCurrentItem(tab.getPosition());
                        Log.d("tab selected",""+tab.getPosition());
                                    if(tab.getPosition==0)
                                 {
                                     Fragment fragA=new FrgmentA();
                                     fragA.getData("data");
                                 }
                    }

                    @Override
                    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                        // TODO Auto-generated method stub
                        Log.d("tab Unselected",""+tab.getPosition());
                    }

                    @Override
                    public void onTabReselected(Tab tab, FragmentTransaction ft) {
                        // TODO Auto-generated method stub
                        Log.d("tab Reselected",""+tab.getPosition());
                    }
                };

                // Create first Tab
                tab = mActionBar.newTab().setText("Profile").setTabListener(tabListener);
                mActionBar.addTab(tab);

                // Create second Tab
                tab = mActionBar.newTab().setText("Picture").setTabListener(tabListener);
                mActionBar.addTab(tab);

                // Create third Tab
                tab = mActionBar.newTab().setText("Help").setTabListener(tabListener);
                mActionBar.addTab(tab);




  FragmentA extends Fragment{

           public FragmentA(){            
          }
          @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view =inflater.inflate(R.layout.fragment_image, container, false);


        return view;
    }
    public void getData(String s)
{   // your implementation
    }
}

答案 1 :(得分:0)

嘿,请在创建片段实例时传递值,请查看以下代码 我只是在通过viewpager加载片段后传递一个列表

  

private void setupViewPager(ViewPager viewPager, List<AccountDetail> listAc) {
        Homepage.ViewPagerAdapter adapter = new Homepage.ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new AllFragment("", listAc), "All");
        adapter.addFrag(new AllFragment("1", listAc), "Credit");
        adapter.addFrag(new AllFragment("2", listAc), "Debit");
        viewPager.setAdapter(adapter);
    }

然后在你的片段中写一个构造函数就像这样

 public AFragment(String text, List<AccountDetail> manufactist) {
        mText = text;
        manufactureListAcoonut = manufactist;
    }