android – 在ViewPager中的片段之间的通信

前端之家收集整理的这篇文章主要介绍了android – 在ViewPager中的片段之间的通信前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图这样做: http://android-er.blogspot.com/2012/06/communication-between-fragments-in.html
除了我使用FragmentStatePagerAdapter

我有一个有两个片段的活动(FragmentA& FragmentB)

FragmentA有一个edittext和一个按钮,FragmentB有一个textview

现在我想要的是,每当我在edittext中输入某些内容并点击按钮时,我的文字视图就会出现.

主要活动:

public class MainActivity extends FragmentActivity {


    ViewPager viewPager = null;
    String TabFragmentB;

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

        viewPager = (ViewPager)findViewById(R.id.pager);    
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));

    }

    public class MyAdapter extends FragmentStatePagerAdapter {  

        public MyAdapter (FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;

            if (i == 0)
            {
                fragment = new FragmentA();
            }
            if (i == 1)
            {
                fragment = new FragmentB();
            }
            return fragment;
        }

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

    public void setTabFragmentB(String t) {
        TabFragmentB = t;   
    }

    public String getTabFragmentB() { 
        return TabFragmentB;
    }

}

FragmentA:

public class FragmentA extends Fragment {

    EditText et;
    Button bt;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fraga,container,false);

        et = (EditText)v.findViewById(R.id.edit1);
        bt = (Button)v.findViewById(R.id.button1);
        bt.setOnClickListener(Click);

        return v;
    }

    OnClickListener Click = new OnClickListener(){

        @Override
        public void onClick(View v) {

            String textPassToB = et.getText().toString();

            String TabOfFragmentB = ((MainActivity)getActivity()).getTabFragmentB();

            FragmentB fragmentB = (FragmentB)getActivity()
               .getSupportFragmentManager()
               .findFragmentByTag(TabOfFragmentB);

            fragmentB.updateText(textPassToB);          
        }   
    };

}

FragmentB:

public class FragmentB extends Fragment {

    TextView tv;

    @Override
    public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragb,false);    

        tv = (TextView)v.findViewById(R.id.text1);
        String myTag = getTag();

        ((MainActivity)getActivity()).setTabFragmentB(myTag);

        return v;
    }

    public void updateText(String t){
          tv.setText(t);
         }

}

logcat的:

FATAL EXCEPTION: main
 java.lang.NullPointerException
        at lmf.sample1.FragmentA$1.onClick(FragmentA.java:43)
        at android.view.View.performClick(View.java:4212)
        at android.view.View$PerformClick.run(View.java:17476)
        at android.os.Handler.handleCallback(Handler.java:800)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5371)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

每当我点击我的第一个片段上的按钮,我的应用程序崩溃.这是什么问题?

解决方法

>您可以使用Intents(在片段B中注册广播接收者,并从片段A发送广播.
>使用EventBus: https://github.com/greenrobot/EventBus.这是我最喜欢的方法.非常方便使用,方便任何组件之间的通信(例如活动和服务).

要做的事情:

首先,当您的文本更改时,创建一些类来表示事件:

public class TextChangedEvent {
  public String newText;
  public TextChangedEvent(String newText) {
      this.newText = newText;
  }
}

然后,在片段A:

//when text changes
EventBus bus = EventBus.getDefault();
bus.post(new TextChangedEvent(newText));

片段B:

EventBus bus = EventBus.getDefault();

//Register to EventBus
@Override
public void onCreate(SavedInstanceState savedState) {
 bus.register(this);
}

//catch Event from fragment A
public void onEvent(TextChangedEvent event) {
 yourTextView.setText(event.newText);
}
原文链接:https://www.f2er.com/android/311478.html

猜你在找的Android相关文章