我正在开发一个我希望迁移到多个平台的应用程序。为方便移植,我使用Fragments
简化了流程。所以我现在所拥有的只有一个Activity
和几个fragments
。在一种情况下,我有几个相同fragment
的实例,具有不同的数据。显然,同一片段的每个实例都将使用相同的接口函数。
我添加了参数" tag"接口函数识别片段的每个实例(使用创建片段时使用的标签)。我想也可以使用resource ID
。
我的问题:这是一个很好的方法吗?是否有更好或更容易接受的方法?
活动代码段:
public class SailboatRaceActivity extends FragmentActivity
implements SR_CommandsFragment.OnSR_CommandsSelectedListener,
SR_SubCommands_Fragment.OnSR_SubCommandsSelectedListener,
FleetListFragment.FleetList_WidthChange,
FleetListFragment.FleetListChange,
FleetScoringFragment.OnFS_SelectedListener,
FleetSpinnerFragment.FleetSpinner_Selection {
// State Variables
private int mSubCommandPositon;
private int mCommandPosition;
private int mFleetSelection;
private int mFleetListColumnWidth = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mSubCommandPositon = savedInstanceState.getInt("mSubCommandPositon");
mCommandPosition = savedInstanceState.getInt("mCommandPosition");
mFleetSelection = savedInstanceState.getInt("mFleetSelection");
mFleetListColumnWidth = savedInstanceState.getInt("mFleetListColumnWidth");
}
setContentView(R.layout.sailboat_race_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}
public void onSR_CommandSelected(int position) {
SR_SubCommands_Fragment SR_SubCFrag = (SR_SubCommands_Fragment) getFragmentManager().findFragmentById(R.id.subcommands);
mCommandPosition = position;
if (SR_SubCFrag != null) {
SR_SubCFrag.update_SR_subcommands(position);
SR_SubCFrag.ChangeListViewWidth(mFleetListColumnWidth);
}
}
public void onSR_SubCommandSelected(int position) {
mSubCommandPositon = position;
FleetScoringFragment FSFrag = (FleetScoringFragment) getFragmentManager().findFragmentById(R.id.fleet_scoring);
if (FSFrag != null) {
FSFrag.update_commands(mCommandPosition, mSubCommandPositon);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("mSubCommandPositon", mSubCommandPositon);
outState.putInt("mCommandPosition", mCommandPosition);
outState.putInt("mFleetSelection", mFleetSelection);
outState.putInt("mFleetListColumnWidth", mFleetListColumnWidth);
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void FleetList_WidthChange(int width) {
mFleetListColumnWidth = width;
// can we set width of other two lists
SR_SubCommands_Fragment SR_SubCFrag = (SR_SubCommands_Fragment) getFragmentManager().findFragmentById(R.id.subcommands);
if (SR_SubCFrag != null) {
SR_SubCFrag.ChangeListViewWidth(width);
}
SR_CommandsFragment SR_CFrag = (SR_CommandsFragment) getFragmentManager().findFragmentById(R.id.commands);
if (SR_CFrag != null) {
SR_CFrag.ChangeListViewWidth(width);
}
}
@Override
public void FleetListChange(int position) {
FleetScoringFragment FSFrag = (FleetScoringFragment) getFragmentManager().findFragmentById(R.id.fleet_scoring);
if (FSFrag != null) {
mFleetSelection = position;
FSFrag.update_FleetScoringList(position);
}
FleetScoringHeaderFragment FSHFrag = (FleetScoringHeaderFragment) getFragmentManager().findFragmentById(R.id.scoring_header);
if (FSHFrag != null) {
mFleetSelection = position;
FSHFrag.update_FleetScoringHeader(position);
}
}
@Override
public void onFS_Selected(int position) {
}
@Override
public void FSpinner_selection(int selection, String tag) {
int fs = selection;
String thistag =tag;
// do something
}
FSpinner_selection界面是我正在处理的界面。我设置此实现只是为了检查数据是否有效。
FleetSpinner片段:
public class FleetSpinnerFragment extends Fragment {
private int nPresentFleetPosition;
public ArrayList<FleetItem> mListArray = new ArrayList<FleetItem>();
FleetSpinnerAdapter adapter;
FleetSpinner_Selection mCallback;
Spinner spinner;
String tag;
public interface FleetSpinner_Selection {
public void FSpinner_selection(int selection, String tag);
}
public FleetSpinnerFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (FleetSpinner_Selection) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FSpinner_Selection");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
nPresentFleetPosition = 0;
for (int i = 0; i<Fleet.fleetbit.length;i++) {
mListArray.add(Fleet.fleetbit[i]);
}
} else {
nPresentFleetPosition = savedInstanceState.getInt("nPresentFleetPosition");
ArrayList temp = savedInstanceState.getParcelableArrayList("thisFleetList");
mListArray.addAll(temp);
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("nPresentFleetPosition", nPresentFleetPosition);
ArrayList<FleetItem> temp = new ArrayList<FleetItem>();
for (int i =0 ; i< adapter.getCount();i++){
temp.add(adapter.getItem(i));
}
savedInstanceState.putParcelableArrayList("thisFleetList", temp);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = new View(getActivity());
adapter = new FleetSpinnerAdapter(getActivity(), R.layout.fleet_list_item);
spinner = new Spinner(getActivity());
spinner.setAdapter(adapter);
adapter.addAll(Fleet.fleetbit);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
tag = getTag();
spinner.setSelection(nPresentFleetPosition);
return spinner;
}
public void update_FleetScoringHeader(int position) {
nPresentFleetPosition = position;
FrameLayout FLScoringHeader = (FrameLayout) getActivity().findViewById(R.id.scoring_header);
TextView v1 = (TextView) FLScoringHeader.findViewById(R.id.fleet_list_item_textview);
v1.setText(Fleet.fleetbit[nPresentFleetPosition].name);
ImageView v0 = (ImageView) FLScoringHeader.findViewById(R.id.fleet_list_item_icon);
v0.setImageResource(Fleet.fleetbit[nPresentFleetPosition].id);
}
static class FleetHolder {
ImageView imgIcon;
TextView txtTitle;
}
public class CustomOnItemSelectedListener extends Activity implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
FleetItem temp = (FleetItem) parent.getItemAtPosition(position);
nPresentFleetPosition = position;
mCallback.FSpinner_selection(position, tag);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
我刚刚开始学习android编程,所以这可能是一个非常棒的问题。