如何在片段a中从片段b开始方法,具体展示
How to start method from fragment b in fragment a, with specyfic show
我在从另一个片段中的片段开始方法时遇到问题。
我有2个片段。在抽屉和抽屉中,我可以选择带有信息的 dialogFragment。我只能打开一个片段,但我想为所有选项创建一个片段。
所以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;
}
这是 class
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;
}
});
并且在 setOnDrawerItemClickListener
中我想从 fragmentB
调用方法
我的问题是怎么做?
在 onItemClick(...)
回调中试试这个:
if (settingsFragment != null && settingsFragment instanceof SettingsFragment) {
((SettingsFragment) settingsFragment).configureSettingsMenus(position);
}
您必须 Type Cast class 因为在 FragmentA
中您持有对 DialogFragment
的引用,它是 SettingsFragment
的抽象class 并且不包含方法 configureSettingsMenus(...)
.
您必须将 DialogFragment
实例转换为具体的 SettingsFragment
才能访问方法 configureSettingsMenus(...)
.
-- 或者--
在 FragmentA
.
中保留对具体 SettingsFragment
class 的引用
替换:
DialogFragment settingsFragment = SettingsFragment.newInstance(1);
与:
SettingsFragment settingsFragment = SettingsFragment.newInstance(1);
然后在 onItemClick(...)
回调中,您可以直接调用该方法而无需类型转换:
settingsFragment.configureSettingsMenus(position);
我在从另一个片段中的片段开始方法时遇到问题。
我有2个片段。在抽屉和抽屉中,我可以选择带有信息的 dialogFragment。我只能打开一个片段,但我想为所有选项创建一个片段。
所以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;
}
这是 class
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;
}
});
并且在 setOnDrawerItemClickListener
中我想从 fragmentB
我的问题是怎么做?
在 onItemClick(...)
回调中试试这个:
if (settingsFragment != null && settingsFragment instanceof SettingsFragment) {
((SettingsFragment) settingsFragment).configureSettingsMenus(position);
}
您必须 Type Cast class 因为在 FragmentA
中您持有对 DialogFragment
的引用,它是 SettingsFragment
的抽象class 并且不包含方法 configureSettingsMenus(...)
.
您必须将 DialogFragment
实例转换为具体的 SettingsFragment
才能访问方法 configureSettingsMenus(...)
.
-- 或者--
在 FragmentA
.
SettingsFragment
class 的引用
替换:
DialogFragment settingsFragment = SettingsFragment.newInstance(1);
与:
SettingsFragment settingsFragment = SettingsFragment.newInstance(1);
然后在 onItemClick(...)
回调中,您可以直接调用该方法而无需类型转换:
settingsFragment.configureSettingsMenus(position);