片段在FragmentActivity TabListener中解析数据

时间:2014-06-30 08:38:39

标签: android interface tabs fragment android-fragmentactivity

我有一些片段作为标签,我创建了一个接口类,用于将片段中的字符串解析为同一片段活动中的另一个片段。但问题是,当我开始切换标签时,我的手机和标签中有一个强制关闭。这是在片段之间进行通信的最佳方式,也是帮助找出问题的最佳方法。谢谢......

这是接口类

public interface FragmentCommunicater {
    public void getData(String data);
}

FragmentActivity Class

public class Welcome extends FragmentActivity implements TabListener,FragmentCommunicater{

    private ActionBar actionbar;
    private ViewPager viewPager;


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

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new fragAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageSelected(int arg0) {
                actionbar.setSelectedNavigationItem(arg0);
            }
        });

        actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab dashboard_tab = actionbar.newTab();
        dashboard_tab.setText("Home");
        dashboard_tab.setTabListener(this);
        actionbar.addTab(dashboard_tab);

        ActionBar.Tab addPatient_tab = actionbar.newTab();
        addPatient_tab.setText("Add Patient");
        addPatient_tab.setTabListener(this);
        actionbar.addTab(addPatient_tab);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void getData(String data) {
        FragmentManager fm = getSupportFragmentManager();
        ViewPatient vpFrag = (ViewPatient) fm.findFragmentById(R.id.view_patient_fragment);
        vpFrag.setData(data);
    }   
}

class fragAdapter extends FragmentPagerAdapter{

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

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

        if(arg0 == 0){
            fragment = new Dashboard();
        }
        if(arg0 == 1){
            fragment = new AddPatient();
        }       
        return fragment;
    }

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

发送片段

public class Dashboard extends Fragment {

    private Button btn_searchPatient;   
    private FragmentCommunicater fragComm;

    public Dashboard() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);            btn_searchPatient = (Button) view.findViewById(R.id.btn_searchPatient);     

        fragComm = (FragmentCommunicater) getActivity();        
        initializeActions();

        return view;
    }

    private void initializeActions(){

        btn_searchPatient.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {

                if(!result.isEmpty()){
                    fragComm.getData("test parse string"));
                }
            }
        });     
    }
}

接收片段

public class ViewPatientextends Fragment {

    public ViewPatient() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view_patient, container, false); 
        return view;
    }

    public void setData(String data){
        Toast.makeText(getActivity().getApplicationContext(), data, Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:0)

FragmentActivity 中的 getData 功能需要像这样改变,

@Override
public void getData(String data) {
    FragmentManager fm = getSupportFragmentManager();
    ViewPatient vpFrag = (ViewPatient) fm.findFragmentByTag(R.id.view_patient_fragment);
    vpFrag.setData(data);
}

findFragmentById 需要更改为 findFragmentByTag ,并且在创建时必须传递片段标记,

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
    ((Welcome)getActivity()).setViewPatientFragmentTagData(getTag());
}

通过在FragmentActivity中实现一个名为setViewPatientFragmentTagData的方法,

public void setViewPatientFragmentTag(String tag){
    viewPatientFragmentTag = tag;
}

带有 String viewPatientFragmentTag 变量。这种方法效果很好。