2014-03-27 96 views
0

我有FragmentStatePagerAdapter應用程序。其中一個片段包含一些線性佈局。我也有ActionBar。當我點擊ActionBar上的圖標(按鈕)時,我將用字符串更新我的ArrayList。在更新ArrayList之後,我想更新片段中的LinearLayout。通過ActionBar按鈕點擊更改FragmentPagerAdapter中的片段內容

這是FragmentPagerAdapter我的Java代碼:

public class SectionsPagerAdapter extends FragmentStatePagerAdapter{ 

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

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      switch (position) { 
      case 0: { 
       Fragment fragment = new NejblizsiBary(); 
       Bundle args = new Bundle(); 
       args.putInt(NejblizsiBary.ARG_SECTION_NUMBER, position + 1); 
       fragment.setArguments(args); 
       return fragment; 
      } 

      case 1: { 
       Fragment fragment3 = new Mapa(); 
       Bundle args = new Bundle(); 
       args.putInt(Mapa.ARG_SECTION_NUMBER, position + 2); 
       fragment3.setArguments(args); 
       return fragment3; 
      } 

      case 2: { 
       Fragment fragment2 = new OblibeneBary(); 
       Bundle args = new Bundle(); 
       args.putInt(OblibeneBary.ARG_SECTION_NUMBER, position + 3); 
       fragment2.setArguments(args); 
       return fragment2; 
      } 

      default: 
       return null; 
      } 
     } 

     @Override 
     public int getItemPosition(Object object) { 
      return POSITION_NONE; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section3).toUpperCase(l); 

      case 2: 
       return getString(R.string.title_section2).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

這是我與創建片段的內容類:

public class NejblizsiBary extends Fragment implements LocationListener { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public NejblizsiBary() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main_dummy, 
        container, false); 

      LinearLayout mainLayout = (LinearLayout) rootView.findViewById(R.id.hl); 
      getApplicationContext(); 
      LayoutInflater inflater_layout = (LayoutInflater) getApplicationContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      for(int i = 0; i < pocet_baru;i++){ 
      View itemBox = inflater_layout.inflate(R.layout.jedenbar,null); 
      itemBox.setId(i); 
      TextView nazev = (TextView)itemBox.findViewById(R.id.nazev); 
      nazev.setText(bary.get(i).getNazev()); 
      View.OnClickListener handler = new View.OnClickListener(){ 

       @Override 
       public void onClick(View arg0) { 
        Detaily dialog = new Detaily(); 
        Bundle args = new Bundle(); 
        int id = arg0.getId(); 
        args.putDouble("hlat", mLatitude); 
        args.putDouble("hlong", mLongitude); 
        args.putDouble("lat", bary.get(id).getLatitude()); 
        args.putDouble("long", bary.get(id).getLongtitude()); 
        args.putString("nazev", bary.get(id).getNazev()); 
        args.putString("adresa", bary.get(id).getAdresa()); 
        args.putString("mesto", bary.get(id).getMesto()); 
        args.putString("popis", bary.get(id).getPopis()); 
        dialog.setArguments(args); 
        dialog.show(getActivity().getFragmentManager(), "MyDialog"); 
       } 

      }; 
      itemBox.setOnClickListener(handler); 
      mainLayout.addView(itemBox); 
      } 
      return rootView; 
     } 

u能幫助我與我的問題請?

+0

問題是否引用了要更改片段的內容? –

+0

是的。我絕對不知道如何。 (Kdyžtakmůžetečesky)。 – user3407775

+0

保持Google語言的英語;-) –

回答

1

最簡單和最直接的方法是在創建它時在其活動中註冊每個片段。然後通過活動將由操作欄按鈕所做的事件委託給適當的片段。其他方法是使用某種事件分佈觀察者模式或事件總線模式。 我使用由RoboGuice personaly實現的事件總線。其他實現可以在Guava或Otto庫中找到。