2017-02-22 3 views
0

У меня проблема с запуском метода из фрагмента в другом фрагменте.Как начать метод из фрагмента b в фрагменте a, с specyfic show

У меня есть 2 фрагмента. На ящике и в ящике я могу выбрать dialogFragment с информацией. I ican открывает только фрагмент, но я хочу создать один фрагмент для всех параметров.

Таким образом, фрагментB является диалоговым фрагментом.

Это основной метод, который я хочу назвать

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; 
     } 
    }); 

и с etOnDrawerItemClickListener я хотите вызвать метод из фрагментаB

И мой вопрос КАК?

ответ

0

Попробуйте внутри onItemClick(...) обратного вызова:

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

Вы должны Type Cast класс, потому что в FragmentA вы держите ссылку на DialogFragment, который является абстракцией SettingsFragment класса и не содержит метод configureSettingsMenus(...).

Для того, чтобы получить доступ к методу configureSettingsMenus(...), вы должны направить свой экземпляр DialogFragment на бетон SettingsFragment.

- В качестве альтернативы -

Держите ссылку на бетонном SettingsFragment класса в пределах FragmentA.

заменить:

DialogFragment settingsFragment = SettingsFragment.newInstance(1); 

с:

SettingsFragment settingsFragment = SettingsFragment.newInstance(1); 

Тогда внутри onItemClick(...) обратного вызова вы можете просто вызвать метод непосредственно без типа литья:

settingsFragment.configureSettingsMenus(position);