2014-04-02 97 views
1

您好我正在開發一個使用3片段(片段A,B,C)在viewpager和tabs內的android應用,viewpager可以正常工作。片段A包含列表視圖,當用戶單擊某個項目時,應用程序會打開一個包含所選項目信息的片段對話框。該對話框有一個名爲「添加到收藏夾」的按鈕。現在,我想這樣做,當用戶按下按鈕:從另一個片段在viewpager中顯示片段

  1. 關閉碎片對話框
  2. 顯示視圖尋呼機
  3. 裏面的片段B,從對話片段將信息發送到B片段

我怎樣才能做到這一點?

這是我的代碼部分:

* MainFragmentActivity *(這工作正常)

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tube); 

    // Set up the action bar. 
    final ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    // When swiping between different sections, select the corresponding 
    // tab. We can also use ActionBar.Tab#select() to do this if we have 
    // a reference to the Tab. 
    mViewPager 
      .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
       @Override 
       public void onPageSelected(int position) { 
        actionBar.setSelectedNavigationItem(position); 
       } 
      }); 


    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 

     actionBar.addTab(actionBar.newTab() 
       .setText(mSectionsPagerAdapter.getPageTitle(i)) 
       .setTabListener(this)); 
    } 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, switch to the corresponding page in 
    // the ViewPager. 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, 
     FragmentTransaction fragmentTransaction) { 
} 


public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
     case 0: 
      FragmentA a = new FragmentA(); 
      Bundle args1 = new Bundle(); 
      args1.putInt(FragmentA.ARG_SECTION_NAME , position + 1); 
      a.setArguments(args1); 
      return a; 

     case 1: 
      FragmentB b= new FragmentB(); 
      Bundle args2 = new Bundle(); 
      args2.putInt(FragmentB.ARG_SECTION_NAME , position + 2); 
      b.setArguments(args2); 
      return b; 

     case 2: 
      FragmentC c= new FragmentC(); 
      Bundle args3 = new Bundle(); 
      args3.putInt(FragmentC.ARG_SECTION_NAME , position + 3); 
      c.setArguments(args3); 
      return c; 

      default: 
       return null; 
     } 
    } 

這是片段對話

* FragmentDialogView *

public class FragmentDialogView extends DialogFragment implements OnClickListener { 

private static final int REAUTH_ACTIVITY_CODE = 0; 
private String videoId; 

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

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    Bundle mArgs = getArguments(); 

    View view = (View) inflater.inflate(R.layout.fragment_dialog_view, container, false); 

    //Buttons 
    Button button = (Button) view.findViewById(R.id.button_one); 
    button.setOnClickListener(this); 
    buttonDownload.setOnClickListener(this); 

    return view; 
} 

@Override 
public void onSaveInstanceState(Bundle bundle) { 
    super.onSaveInstanceState(bundle); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == REAUTH_ACTIVITY_CODE) { 

    } 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button_one: 

      //Here it should show the fragment B inside the viewpager 

     break; 
    default:     
     break; 
    } 

} 
} 

回答

3

要消除Dialog在您DialogFragment的類

private Dialog dialog; 

@Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     dialog = new Dialog(getActivity()); 
     return dialog; 
    } 

    @Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.button_one: 

      dismiss(); 

     break; 
    default:     
     break; 
    } 

} 

下,並創建一個接口

創建以下通訊。java的

public interface Communicator { 
    public void respond(int i); 
} 

MainAcitvity

片段實現通訊創建此通訊instance這樣

public class FragmentDialogView extends DialogFragment implements OnClickListener { 

    private Communicator com; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     com = (Communicator) getActivity(); 
     btn.setOnClickListener(this); 
     } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
     case R.id.btn: 
      com.respond(1); 
      break; 
     } 
    } 

每當你單擊按鈕它發送的int這是駐留在MainActivity

這將類似於以下

@Override 
public void respond(int i) { 


     // Receive a bundle here 
     // and pass the corresponding information to the FragmentB 
     // here i'm receving an int and pass it to the FragmentB as a String 

     FragmentManager fm = getFragmentManager(); 
     FragmentB fragment = (FragmentB) fm.findFragmentByTag("FragmentB"); 
     fragment.fromMainActivity(""+i); 

     // If the above the one doesn't work keep the instance as Static and then try 

     viewPager.invalidate(); 
     pagerAdapter.notifyDataSetChanged(); 
     viewPager.setCurrentItem(1, true); 

    // Inside the setCuttentItem() method 0 first tab 
    // 1 second tab 
    // 2 third tab and so on 
    } 

這裏我收到一個int裏面的方法。您可以使用一個包來傳遞相應的信息。這將改變viewPager顯示下一個選項卡,以及

,並保留任何簡單的方法insdie的FragmentB像下面

public void fromMainActivity(String sample) { 
    Toast.makeText(getActivity(), sample, duration).show(); 
} 

我希望這將有助於:)快樂編碼

+0

謝謝沙工作正常,如果試圖傳遞信息,我需要創建另一個實例FragmentB?如果我在OnActivityCreated上創建了一個Bundle,這將會得到Null,因爲當應用程序啓動這個片段的bundle時沒有任何值 – deleon

+0

是的。你需要創建一個實例,如果它不起作用,請嘗試使它成爲一個「靜態」。您將從「DialogFrament」儀式創建包。所以如果你傳遞任何東西,它不會爲空。否則檢查一個條件爲null @deleon –

2

1.試試這個:getDialog()。dismiss();

2.As我的理解正確的話,在你的片段創建這樣一個方法,

public static FirstFragment newInstance(String text){ 
    FirstFragment f= new FirstFragment(); 
    return f; 
} 

調用它在你的按鈕onClick()FirstFragment.newInstance("Fragment, Instance 1");

3.創建Interface在你DialogFragment方法可以調用將任何你想要的數據傳回給創建所述DialogFragment的Fragment。還要將您的片段設置爲目標,例如myFragmentDialog.setTargetFragment(this, 0)。然後在對話框中,使用getTargetFragment()獲取目標片段對象,並將其轉換爲您創建的接口。現在您可以使用((MyInterface)getTargetFragment()).methodToPassData(data)傳遞數據。 欲瞭解更多信息:link

+0

我這樣做。 ..看我的第一堂課。我沒有問題的視圖尋呼機中的片段,我的問題是如何顯示位置B(片段)時,用戶按下對話框片段內的按鈕 – deleon

+0

您是否嘗試建議的解決方案,調用您的按鈕內的給定方法onClick ()? –

+0

Zusee Weekin,是的,我做了,但沒有工作 – deleon