如何使用“滑动”选项卡通过不同的片段进行数据通信/传递?

时间:2014-02-28 19:53:17

标签: android android-fragments tabs

您好我有一个包含三个片段的滑动选项卡片段活动。我创建了自己的适配器,扩展了FragmentStatePageAdapter。

现在,我将解释每个标签(片段)的工作原理。

在标签1和标签2中,我有一个实际上相同类型的列表,当我的意思是类型时,我的意思是它们是症状(发烧,眼睛疼痛等)。

现在Tab 3用作托盘。每当我从选项卡1或选项卡2中的列表视图中选择一个项目时,我想将该项目转移到选项卡3,并从列表中删除该项目。

我尝试过实现一个接口,但它不适合我,因为我通过适配器在主FragmentActivity上添加了片段。我的问题是

  
      
  1. 我如何传达这三个碎片?这意味着我如何在这些片段之间传递数据。
  2.   

现在,如果你想看一下我现在所做的事情,现在就是:

我在问题中将其描述为选项卡2

public class FragmentGeneralSymptoms extends ListFragment implements OnItemClickListener {
ArrayList<String> symptomList;
String name;
String selected;
Tray tray;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return inflater.inflate(R.layout.fragment_generalsymptoms, container,
            false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    Bundle args = getArguments();
    name = args.getString("key");
    tray = (Tray) getActivity();

    new LoadSymptoms().execute();
}

class LoadSymptoms extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        KnowledgeBaseHelper helper = new KnowledgeBaseHelper(getActivity());
        ProfileHandler handler = new ProfileHandler(getActivity());

        Profiles profile = handler.getProfile(name);
        String age = profile.getAge() + "";
        String gender = profile.getGender();

        symptomList = helper.getSymptomList(name, age, gender);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.general_list, symptomList));
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    selected = symptomList.get(arg2);
    tray.respond(selected);
}

}

我的标签3

public class FragmentTray extends ListFragment{
private ArrayList<String> selectedList;

public FragmentTray(){}

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

public void addToTray(String symptom){
    selectedList.add(symptom);
    setListAdapter(new ArrayAdapter<String>(getActivity(),
            R.layout.general_list, selectedList));
}

public void removeSymptom(String string){
    selectedList.remove(selectedList.indexOf(string));
}

}

我的MainActivity扩展了FragmentActivity

public class MainActivity extends FragmentActivity implements TabListener, OnPageChangeListener, Tray {
private String selectedProfileName;
ActionBar actionBar;
ViewPager viewPager;

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.main_activity);

    Bundle args = getIntent().getExtras();
    selectedProfileName = args.getString("key_name");

    viewPager = (ViewPager) findViewById(R.id.main_pager);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(this);

    InitializeActionBar();

}

public void InitializeActionBar() {
    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tabBodyPart = actionBar.newTab();
    tabBodyPart.setText("Body Parts");
    tabBodyPart.setTabListener(MainActivity.this);

    ActionBar.Tab tabGeneralSymptoms = actionBar.newTab();
    tabGeneralSymptoms.setText("General Symptoms");
    tabGeneralSymptoms.setTabListener(MainActivity.this);

    ActionBar.Tab tabSelectedSymptoms = actionBar.newTab();
    tabSelectedSymptoms.setText("Selected Symptoms");
    tabSelectedSymptoms.setTabListener(MainActivity.this);

    actionBar.addTab(tabBodyPart);
    actionBar.addTab(tabGeneralSymptoms);
    actionBar.addTab(tabSelectedSymptoms);
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    viewPager.setCurrentItem(tab.getPosition());
}

class MyAdapter extends FragmentStatePagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Fragment fragment = null;
        Bundle bundle = new Bundle();
        bundle.putString("key", selectedProfileName);
        if (arg0 == 0) {
            fragment = new FragmentBodyPart();
            fragment.setArguments(bundle);
        }
        if (arg0 == 1) {
            fragment = new FragmentGeneralSymptoms();
            fragment.setArguments(bundle);
        }
        if (arg0 == 2) {
            fragment = new FragmentTray();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

@Override
public void onPageSelected(int arg0) {
    // TODO Auto-generated method stub
    actionBar.setSelectedNavigationItem(arg0);
}

@Override
public void respond(String string) {
    // TODO Auto-generated method stub
    FragmentTray frag2 = new FragmentTray();
    frag2.addToTray(string);
}
}

最后我的界面

public interface Tray {

public void respond(String string);
}

1 个答案:

答案 0 :(得分:1)

我通常做的是:

1.-定义界面。它完成了,很好。

2.- FragmentGeneralSymptoms 必须具有初始化侦听器。你是那个但是你忘了把听众放在课堂上。

public class FragmentGeneralSymptoms extends ListFragment implements OnItemClickListener {
    ArrayList<String> symptomList;
    String name;
    String selected;
    Tray tray;

    (your code)

    public void setTrayListener(Tray listener)
    {
        tray = listener;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        selected = symptomList.get(arg2);
        if(tray != null)
        {
           symptomList.remove(selected); // if you want to remove this element from this list
           tray.respond(selected);
        }
    } }

3.-您的 FragmentTray 类必须实现接口:

public class FragmentTray extends ListFragment implements Tray{
private ArrayList<String> selectedList;

public FragmentTray(){}

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

public void addToTray(String symptom){
    selectedList.add(symptom);
    setListAdapter(new ArrayAdapter<String>(getActivity(),
            R.layout.general_list, selectedList));
}

public void removeSymptom(String string){
    selectedList.remove(selectedList.indexOf(string));
}

public void respond(String name)
{

// do stuffs!!!!
selectedList.add(new Symtom(name)); // for example
System.out.println("Inside the respond method respond in FragmentTray class");

}
}

4.-最后但并非最不重要。构建片段时,必须注册侦听器:

class MyAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> fragmentList;

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
        fragmentList = new ArrayList<Fragment>();

        Fragment fragmentBodyPart = new FragmentBodyPart();
        Fragment fragmentTray = new FragmentTray();
        Fragment fragmentGeneralSymptoms = new FragmentGeneraSymptoms();

        // this is important*********************************
        fragmentGeneraSmptoms.setTrayListener(fragmentTray);

        fragmentList.add(fragmentBodyPart);
        fragmentList.add(fragmentGeneralSymptoms);
        fragmentList.add(fragmentTray);
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Fragment fragment = null;
        Bundle bundle = new Bundle();
        bundle.putString("key", selectedProfileName);
        if (arg0 == 0) {
            fragment = (Fragment) fragmentList.get(0);
            fragment.setArguments(bundle);
        }
        if (arg0 == 1) {
            fragment = (Fragment) fragmentList.get(1);
            fragment.setArguments(bundle);
        }
        if (arg0 == 2) {
            fragment = (Fragment) fragmentList.get(2);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return fragmentList.size();
    }

}

我希望它有所帮助。