2012-08-09 130 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()); 
相關問題