2012-08-09 100 views

回答

1

您可以创建侦听器回调接口并在碎片中实现它们。事情是这样的:

@Override 
public void onSomeEvent(List<SomeData> data) { 
    //do something with data 
} 

在您的活动创建这个接口:

public interface OnSomeEventListener { 
    onSomeEvent(List<SomeData> data); 
} 

然后使用findFragmentById或findFragmentByTag获得您的片段,并将其分配给听众:

this.onSomeEventListener = fragment; 

你可以然后调用该接口的方法,你的片段将接收回调。

片段和活动之间的第二个也是更简单的通信方式是BroadcastReceivers。您可以在片段中注册一些BroadcastReceiver,然后从activity中调用sendBroadcast()。您的数据列表可以放在一组广播消息中。

+0

感谢,使用第二种方法,它很好地工作 – user1437481 2012-08-09 15:02:50

3

您可以使用Fragment中的setArguments。看看http://developer.android.com/guide/components/fragments.html,基本上,你在创建你的Fragment之前创建一个Bundle,然后设置为一个参数。从Android文档

例:

public static class DetailsActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (getResources().getConfiguration().orientation 
       == Configuration.ORIENTATION_LANDSCAPE) { 
      // If the screen is now in landscape mode, we can show the 
      // dialog in-line with the list so we don't need this activity. 
      finish(); 
      return; 
     } 

     if (savedInstanceState == null) { 
      // During initial setup, plug in the details fragment. 
      DetailsFragment details = new DetailsFragment(); 
      details.setArguments(getIntent().getExtras()); 
      getFragmentManager().beginTransaction().add(android.R.id.content, details).commit(); 
     } 
    } 
} 

而不是使用getIntent()getExtras(),您可以创建你捆绑,并设置参数

Bundle bundle = new Bundle(); 
bundle.putSerializable(YOUR_KEY, yourObject); 
fragment.setArguments(bundle); 

并为您的片段:

public static class DetailsFragment extends Fragment { 
    /** 
    * Create a new instance of DetailsFragment, initialized to 
    * show the text at 'index'. 
    */ 
    public static DetailsFragment newInstance(int index) { 
     DetailsFragment f = new DetailsFragment(); 

     // Supply index input as an argument. 
     Bundle args = new Bundle(); 
     args.putInt("index", index); 
     f.setArguments(args); 

     return f; 
    } 

    public int getShownIndex() { 
     return getArguments().getInt("index", 0); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if (container == null) { 
      // We have different layouts, and in one of them this 
      // fragment's containing frame doesn't exist. The fragment 
      // may still be created from its saved state, but there is 
      // no reason to try to create its view hierarchy because it 
      // won't be displayed. Note this is not needed -- we could 
      // just run the code below, where we would create and return 
      // the view hierarchy; it would just never be used. 
      return null; 
     } 

     ScrollView scroller = new ScrollView(getActivity()); 
     TextView text = new TextView(getActivity()); 
     int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
       4, getActivity().getResources().getDisplayMetrics()); 
     text.setPadding(padding, padding, padding, padding); 
     scroller.addView(text); 
     text.setText(Shakespeare.DIALOGUE[getShownIndex()]); 
     return scroller; 
    } 
} 
0

从Frament甲传递一个ArrayList<HashMap<String, String>>到Frament乙

Fragment fragment = new FragmentB(); 
FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.add(R.id.fragment_container, fragment); 
fragmentTransaction.addToBackStack(null); 
Bundle bundle = new Bundle(); 
bundle.putInt("position", 1); 
bundle.putSerializable("arrayList",arrayList); 
fragment.setArguments(bundle); 
fragmentTransaction.commit(); 

检索数据在片段B

int position = getArguments().getInt("position"); 
ArrayList<HashMap<String, String>> arrayList= (ArrayList<HashMap<String, String>>) 
getArguments().getSerializable("arrayList"); 
System.out.println(arrayList.size()); 
相关问题