我正在创建一个使用主要活动和几个片段的Android应用。主要活动是min" control"对于一切,每个片段显示具有不同信息的不同屏幕。以下是我主要活动的一些代码:
public class MainMenu extends FragmentActivity implements FirstMenu.FragmentListener, Checklist.FragmentListener,
Tool.FragmentListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
FirstMenu firstMenu = new FirstMenu();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
firstMenu).commit();
}
public void changeScreen() {
Checklist checklist = new Checklist();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, checklist);
transaction.addToBackStack(null);
transaction.commit();
}
public void changeScreenTwo() {
ChemicalCheckList chemChecklist = new ChemicalCheckList();
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, chemChecklist);
transaction.addToBackStack(null);
transaction.commit();
}
这是我的第一个显示菜单的片段的代码:
public class FirstMenu extends Fragment {
FragmentListener fragmentChanger;
public interface FragmentListener {
public void changeScreen();
public void changeScreenFour();
public void changeScreenFive();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
fragmentChanger = (FragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
container.removeAllViews();
View view = inflater.inflate(R.layout.menu, container, false);
Button checkListButton = (Button) view.findViewById(R.id.checklistButton);
Button toolButton = (Button) view.findViewById(R.id.toolButton);
Button timeFuseButton = (Button) view.findViewById(R.id.timeFuseCalculator);
Button blastFragButton = (Button) view.findViewById(R.id.blastFragButton);
Button demoButton = (Button) view.findViewById(R.id.demoButton);
checkListButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonClicked(v);
}
}
);
toolButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
toolButtonClicked(v);
}
}
);
demoButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
demoButtonClicked(v);
}
}
);
blastFragButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), BlastFrag.class);
startActivity(intent);
}
});
timeFuseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), TimeFuse.class);
startActivity(intent);
}
});
return view;
}
public void buttonClicked( View v ) {
fragmentChanger.changeScreen();
}
public void toolButtonClicked( View v ) {
fragmentChanger.changeScreenFour();
}
public void demoButtonClicked( View v ) {
fragmentChanger.changeScreenFive();
}
}
这是第二个片段的代码,它使用方法changeScreen()显示在屏幕上;
public class Checklist extends Fragment{
FragmentListener fragmentAlternator;
public interface FragmentListener {
public void changeScreenTwo();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
fragmentAlternator = (FragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
container.removeAllViews();
View view = inflater.inflate(R.layout.checklists, container, false);
Button chemButton = (Button) view.findViewById(R.id.chemButton);
chemButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonClicked(v);
}
}
);
return view;
}
public void buttonClicked( View v ) {
fragmentAlternator.changeScreenTwo();
}
}
我有更多的片段使用更多方法显示在屏幕上,例如:changeScreenTwo(),changeScreenThree(),changeScreenFour()等。方法的代码几乎完全相同。唯一的区别是它是从不同的类创建不同的对象。我想知道我现在这样做的方式是否合适,或者我是否有办法制作所有这些" changeScreen"将方法转换为一个方法,并简单地将每种类型的类作为参数传递。我看了一遍,似乎无法找到我在这种特殊情况下寻找的答案。
答案 0 :(得分:0)
传入片段索引并使用它来选择片段类型。
public void changeScreen(int position) {
switch(position){
case 0:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new Checklist())
.addToBackStack(null)
.commit();
break;
case 1:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new ChemicalCheckList())
.addToBackStack(null)
.commit();
break;
}
}