当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How can I communicate/pass data through different Fragments with Swipe Tab?

问题描述:

Hi I have a Swipe Tab Fragment Activity that contains Three Fragments. I created my own adapter that extends FragmentStatePageAdapter.

Now here I'll explain how each tab (Fragments) works.

In Tab 1 and Tab 2, I have a list with practically the same type, when I mean type, I mean they are Symptoms (fever, pain in the eyes, etc.).

Now Tab 3 serves as a tray. Whenever I select an item from the listview in either Tab 1 or Tab 2, I want to transfer that item to Tab 3 and remove that Item from the List where it was selected.

I have tried implementing an Interface but it didn't work for me since I added the fragments on the Main FragmentActivity through an Adapter. MY questions is

  1. How can I communicate the three Fragments? Meaning how can I pass data between these fragment.

Now if you want to take a look at what I've done for now, here it is:

I depicted this as Tab 2 in my question

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);

}

}

My Tab 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));

}

}

My MainActivity which Extends 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);

}

}

Finally my Interface

public interface Tray {

public void respond(String string);

}

网友答案:

what I usually do is:

1.- Define the interface. It's done, fine.

2.- Your FragmentGeneralSymptoms must have an initializated listener. Yuou're that but you forgot to set the listener in the class.

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.- Your FragmentTray class must implement the interface:

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.- And last but not least. When you build the fragments you must register the listener:

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();
    }

}

I hope that it helps.

分享给朋友:
您可能感兴趣的文章:
随机阅读: