选项卡片段按钮单击“将数据发送到多个tabFragment”

时间:2017-05-31 12:00:37

标签: android android-fragments android-viewpager

我有三个标签片段TAB1,TAB2和TAB3。在TAB1上有一个按钮,当点击它时,它必须使用相应片段中textView的接口和显示数据将数据发送到TAB2和TAB3。到目前为止,我已设法从TAB1发送到TAB2,如下所示

 //  interface  codes
  public interface IFragmentToActivity {
  void Tab1ToTab2(String value);
 }

 //  in activity 

    @Override
  public void Tab1ToTab2(String value) {
TabFragment2 fragment = (TabFragment2) adapter.getFragment(1);
if (fragment != null) {
    fragment.fragmentCommunicationFragment(value);
} else {
    // Log.i(LOG_TAG, "Fragment 2 is not initialized");
}
   }


  ///   in TAB1 i send data

          @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button2:
            mCallback.FragmentToFragment("hi from tab 1");
            break;
             }
                   }

我怎样才能使它适用于所有碎片?这就是我想要发送给TAB2和3时的尝试。

接口代码

public interface IFragmentToActivity {
void Tab1ToTab2(String value);
void Tab1ToTab3(String value);
     }

TAB1发送数据

    @Override
      public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button2:
            mCallback.Tab1ToTab2("hi from tab 1");
            mCallback.Tab1ToTab3(hi from tab 1");
            break;
    }

主要活动中的代码

       @Override
        public void Tab1ToTab2(String value) {
    TabFragment2 fragment = (TabFragment2) adapter.getFragment(1);
    if (fragment != null) {
        fragment.fragmentCommunicationFragment(value);
    } else {
        // Log.i(LOG_TAG, "Fragment 2 is not initialized");
    }
}

@Override
public void Tab1ToTab3(String value) {
    TabFragment3 fragment = (TabFragment3) adapter.getFragment(1);
    if (fragment != null) {
        fragment.FromFragmentOne(value);
    }

    else {
        // Log.i(LOG_TAG, "Fragment 3 is not initialized");
    }
}

/// TAB 2方法接收数据

  public void fragmentCommunicationFragment(String value){
      mTextView1.setText(value);
      }

/// TAB 3方法接收数据

   public void FromFragmentOne(String kabila){
    mTextView1.setText(kabila);
}

// adapter

 public class PagerAdapter extends FragmentStatePagerAdapter {
private final SparseArray<WeakReference<Fragment>> instantiatedFragments = new SparseArray<>();
private ArrayList<String> mTabHeader;
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;
        case 3:
            TabFragment4 tab4 = new TabFragment4();
            return tab4;
        default:
            return null;
    }
}

2 个答案:

答案 0 :(得分:1)

在你的Tab1ToTab3方法中,你用你的第二个片段实现TabFragment3:

TabFragment3 fragment = (TabFragment3) adapter.getFragment(1);

替换为

TabFragment3 fragment = (TabFragment3) adapter.getFragment(2);

答案 1 :(得分:1)

  

//用这个

更新你的getItem()方法
  @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Bundle bundle =  new Bundle();
                bundle.putString("YOUR_KEY","YOUR VALUE");
                TabFragment1 tab1 = new TabFragment1();
                tab1.setArguments(bundle);
                return tab1;
            case 1:
                Bundle bundle2 =  new Bundle();
                bundle2.putString("YOUR_KEY","YOUR VALUE");
                TabFragment2 tab2 = new TabFragment2();
                tab2.setArguments(bundle2);
                return tab2;
            case 2:
                Bundle bundle3 =  new Bundle();
                bundle3.putString("YOUR_KEY","YOUR VALUE");
                TabFragment3 tab3 = new TabFragment3();
                tab3.setArguments(bundle3);
                return tab3;
            case 3:
                Bundle bundle4 =  new Bundle();
                bundle4.putString("YOUR_KEY","YOUR VALUE");
                TabFragment4 tab4 = new TabFragment4();
                tab4.setArguments(bundle4);
                return tab4;
            default:
                return null;
        }
  

//在TabFragment1,TabFragment2,TabFragment3,TabFragment4下   覆盖onActivityCreated()以获取数据String和setMenuVisibility(boolean menuVisible)以更新视图

     public static String data;


     @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Bundle bundle = getArguments();
            String yourValue = bundle.getString("YOUR_KEY");
imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   TabFragment1.data="Update";
                   TabFragment2.data="Update";
                   TabFragment3.data="Update";
                   TabFragment4.data="Update";
    //update your current View from here
                }
            });
        }



     @Override
        public void setMenuVisibility(boolean menuVisible) {
            super.setMenuVisibility(menuVisible);
            if(menuVisible){
                //update your current View from here
            }
        }

通过使用它,您可以从任何片段更新所有片段数据视图。点击