2017-02-22 80 views
0

從另一個片段中的片段啓動方法有問題。如何從片段a中的片段b開始使用specyfic顯示

我有2個片段。在抽屜和抽屜中,我可以選擇帶信息的dialogFragment。我ican打開一個片段,但我想爲所有選項創建一個片段。

所以fragmentB是DialogFragment。

這是我想叫

public void configureSettingsMenus(int position) { 
    switch (position) { 
     case TRADING_HISTORY: 

      break; 
     case LEADER_BOARD: 

      break; 
     case SPECIAL_OFFER: 

      break; 
     case VIDEO_TUTORIALS: 

      break; 
     case FAQ: 

      break; 
     case CONTACT: 

      break; 
     default: 
      Toast.makeText(getContext(), "Something went wrong, try again...", Toast.LENGTH_SHORT).show(); 
      break; 
    } 

主要方法這是該類

public class SettingsFragment extends DialogFragment { 


    private static final int TRADING_HISTORY = 1; 
    private static final int LEADER_BOARD = 2; 
    private static final int SPECIAL_OFFER = 3; 
    private static final int VIDEO_TUTORIALS = 4; 
    private static final int FAQ = 5; 
    private static final int CONTACT = 6; 


    String[] data = {"HERE", "WILL", "BE", "GAME", "HISTORY"}; 

    public SettingsFragment() { 
    } 

    public static SettingsFragment newInstance(int num) { 
     SettingsFragment f = new SettingsFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("num", num); 
     f.setArguments(args); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_game_history, container, false); 
     RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.history_games_recycler); 
     recyclerView.setHasFixedSize(true); 
     RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext()); 
     recyclerView.setLayoutManager(layoutManager); 
     RecyclerView.Adapter adapter = new WinLostHistoryAdapter(data); 
     recyclerView.setAdapter(adapter); 
     return view; 
    } 

    public void configureSettingsMenus(int position) { 
     switch (position) { 
      case TRADING_HISTORY: 

       break; 
      case LEADER_BOARD: 

       break; 
      case SPECIAL_OFFER: 

       break; 
      case VIDEO_TUTORIALS: 

       break; 
      case FAQ: 

       break; 
      case CONTACT: 

       break; 
      default: 
       Toast.makeText(getContext(), "Something went wrong, try again...", Toast.LENGTH_SHORT).show(); 
       break; 
     } 


    } 

} 

而且在FragmentA我創建這個:

DialogFragment settingsFragment = SettingsFragment.newInstance(1); 

private void configureDrawer(){ 
    result = new DrawerBuilder() 
      .withSliderBackgroundColor(Color.GRAY) 
      .withActivity(getActivity()) 
      .withDisplayBelowStatusBar(false) 
      .withDrawerGravity(Gravity.LEFT) 
      .withHeaderPadding(true) 
      .addDrawerItems(
        new SectionDrawerItem().withName("Options"), 
        new PrimaryDrawerItem().withName("Trading History").withIcon(R.drawable.trading_history).withIdentifier(1), 
        new PrimaryDrawerItem().withName("Leader Board").withIcon(R.drawable.leade_board).withIdentifier(2), 
        new PrimaryDrawerItem().withName("Special offer").withIcon(R.drawable.special_icon).withIdentifier(3), 
        new PrimaryDrawerItem().withName("Video tutorials").withIcon(R.drawable.video_tutorials).withIdentifier(4), 
        new PrimaryDrawerItem().withName("FAQ").withIcon(R.drawable.faq_icon).withIdentifier(5), 
        new PrimaryDrawerItem().withName("CONTACT").withIcon(R.drawable.contact_icon).withIdentifier(6) 
      ) 
      .buildForFragment(); 

    result.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { 
     @Override 
     public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { 
      FragmentTransaction ft = getChildFragmentManager().beginTransaction(); 
      ft.addToBackStack(null); 
      if(position == 6) 
      { 
       result.closeDrawer(); 
       settingsFragment.show(ft, DIALOG_LOST); 
      } 

      return true; 
     } 
    }); 

,並以s etOnDrawerItemClickListener我想從片段B調用方法B

而我的問題是如何?

回答

0

試試這個onItemClick(...)回調中:

if (settingsFragment != null && settingsFragment instanceof SettingsFragment) { 
    ((SettingsFragment) settingsFragment).configureSettingsMenus(position); 
} 

你必須Type Cast類,因爲FragmentA你持有一個參照DialogFragment,這是SettingsFragment類的抽象和不包含的方法configureSettingsMenus(...)

您必須將DialogFragment實例投射到具體的SettingsFragment才能訪問方法configureSettingsMenus(...)

- 可選地 -

握住內FragmentA到混凝土SettingsFragment類的引用。

取代:

DialogFragment settingsFragment = SettingsFragment.newInstance(1); 

有:

SettingsFragment settingsFragment = SettingsFragment.newInstance(1); 

然後onItemClick(...)回調裏面,你可以簡單地調用該方法的情況下直接類型轉換:

settingsFragment.configureSettingsMenus(position);