我正在尝试使用ViewPager
创建注册屏幕,用户将在最后一个片段中提供所有详细信息并点击Submit
按钮。
在刷每个片段时,我必须验证数据并将其保存到Activity中具有ViewPager
的对象。我找不到一个完美的方法来做到这一点。
以下是我的代码段
RegistrationActivity.java
public class RegistrationActivity extends FragmentActivity implements MultiPurposeDialogListener {
private ViewPager viewPager;
private static final int NUM_PAGES = 4;
private PagerAdapter mPagerAdapter;
private boolean isActivityVisible;
private ProgressDialog progressDialog;
// Have to save data in these object
public Employee Employee = newEmployee();
public EmpDetails empDetails = new EmpDetails();
public Devices devices = new Devices();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
viewPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
viewPager.setAdapter(mPagerAdapter);
}
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return RegistrationSlideFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
RegistrationSlideFragment.java
public static RegistrationSlideFragment create(int pageNumber) {
RegistrationSlideFragment fragment = new RegistrationSlideFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_registraion_slide, container, false);
TextView tx = (TextView) viewGroup.findViewById(R.id.tv_heading);
LinearLayout ln_slide1 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide1);
LinearLayout ln_slide2 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide2);
LinearLayout ln_slide3 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide3);
LinearLayout ln_slide4 = (LinearLayout) viewGroup.findViewById(R.id.ln_slide4);
Button btn_leftarrow = (Button) viewGroup.findViewById(R.id.btn_leftarrow);
Button btn_rightarrow = (Button) viewGroup.findViewById(R.id.btn_rightarrow);
Button btn_submit = (Button) viewGroup.findViewById(R.id.btn_submit);
if ((mPageNumber + 1) == 1) {
btn_leftarrow.setVisibility(View.INVISIBLE);
ed_dob = (EditText) viewGroup.findViewById(R.id.et_dob);
ed_fname = (EditText) viewGroup.findViewById(R.id.et_dob);
ed_lname = (EditText) viewGroup.findViewById(R.id.et_dob);
ed_email = (EditText) viewGroup.findViewById(R.id.et_dob);
ed_country = (EditText) viewGroup.findViewById(R.id.et_dob);
ed_dob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new SelectDateFragment();
FragmentManager fm = getActivity().getFragmentManager();
newFragment.show(fm, "DatePicker");
}
});
tx.setText("Personal Details");
ln_slide1.setVisibility(View.VISIBLE);
} else if ((mPageNumber + 1) == 2) {
sp_availability = (Spinner) viewGroup.findViewById(R.id.sp_availability);
ArrayAdapter<CharSequence> adapter_availability = ArrayAdapter.createFromResource(getActivity(),
R.array.proj_availability, android.R.layout.simple_spinner_item);
sp_availability.setAdapter(adapter_availability);
chk_tot_performance = (CheckBox) viewGroup.findViewById(R.id.chk_tot_performance);
chk_tot_functional = (CheckBox) viewGroup.findViewById(R.id.chk_tot_functional);
chk_tot_localozation = (CheckBox) viewGroup.findViewById(R.id.chk_tot_localization);
chk_tot_security = (CheckBox) viewGroup.findViewById(R.id.chk_tot_security);
chk_tom_native = (CheckBox) viewGroup.findViewById(R.id.chk_tom_native);
chk_tom_hybrid = (CheckBox) viewGroup.findViewById(R.id.chk_tom_hybrid);
chk_tom_web = (CheckBox) viewGroup.findViewById(R.id.chk_tom_web);
tx.setText("Project Prefetance");
ln_slide2.setVisibility(View.VISIBLE);
} else if ((mPageNumber + 1) == 3) {
sp_functional = (Spinner) viewGroup.findViewById(R.id.sp_functional);
ArrayAdapter<CharSequence> adapter_functional = ArrayAdapter.createFromResource(getActivity(),
R.array.testing_expertise, android.R.layout.simple_spinner_item);
sp_functional.setAdapter(adapter_functional);
sp_security = (Spinner) viewGroup.findViewById(R.id.sp_security);
ArrayAdapter<CharSequence> adapter_security = ArrayAdapter.createFromResource(getActivity(),
R.array.testing_expertise, android.R.layout.simple_spinner_item);
sp_security.setAdapter(adapter_security);
sp_performance = (Spinner) viewGroup.findViewById(R.id.sp_performance);
ArrayAdapter<CharSequence> adapter_performance = ArrayAdapter.createFromResource(getActivity(),
R.array.testing_expertise, android.R.layout.simple_spinner_item);
sp_performance.setAdapter(adapter_performance);
sp_localization = (Spinner) viewGroup.findViewById(R.id.sp_localization);
ArrayAdapter<CharSequence> adapter_localization = ArrayAdapter.createFromResource(getActivity(),
R.array.testing_expertise, android.R.layout.simple_spinner_item);
sp_localization.setAdapter(adapter_localization);
tx.setText("Test Expertise");
ln_slide3.setVisibility(View.VISIBLE);
} else if ((mPageNumber + 1) == 4) {
btn_rightarrow.setVisibility(View.INVISIBLE);
btn_submit.setVisibility(View.VISIBLE);
sp_network = (Spinner) viewGroup.findViewById(R.id.sp_network_type);
ArrayAdapter<CharSequence> adapter_networkstatus = ArrayAdapter.createFromResource(getActivity(),
R.array.networktype, android.R.layout.simple_spinner_item);
sp_network.setAdapter(adapter_networkstatus);
sp_manufaturer = (Spinner) viewGroup.findViewById(R.id.sp_manufacturer);
sp_device_name = (Spinner) viewGroup.findViewById(R.id.sp_device_name);
sp_version = (Spinner) viewGroup.findViewById(R.id.sp_os_version);
sp_manufaturer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setDeviceList("devicecompactability_devicelist.json", parent.getItemAtPosition(position).toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
tx.setText("Your Device Details");
ln_slide4.setVisibility(View.VISIBLE);
setManufacturerList("devicecompactability_devicelist.json");
setVersionList("devicecompactability_platformversion.json");
}
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Object> finalObject = new ArrayList<Object>();
finalObject.add(registrationActivity.Employee);
finalObject.add(registrationActivity.empDetails);
finalObject.add(registrationActivity.devices);
Gson gson = new Gson();
String resultJson = gson.toJson(finalObject);
System.out.println(resultJson);
}
});
return viewGroup;
}
答案 0 :(得分:3)
我们的应用程序中有相同的(我想)向导系统。
因此我们决定不在刷卡上检索数据,而是在数据更改本身。因此,我们为每个片段创建了一个接口,该活动确实实现了(例如)
onDataChange(long datetime)
或
onTitleChange(String newTitle)
传递给活动(我们将接口绑定到片段的方法onAttach
中的片段),当它在片段中通过验证时(通过TextWatcher编辑编辑文本的结尾,通过他们的更改侦听器编辑单选按钮和复选框等)。我们有一个模型对象,所以接口实现在我们的活动中看起来像这样
public void onTitleChange(String newTitle) {
model.setTitle(newTitle);
}
最后一个片段将“提交按钮事件”传递为onDataSubmit()
,在活动中我们执行逻辑(保存到数据库,然后通过网络发送)。
此外,我们将模型对象保存在onSaveInstanceState
中的活动中,以便在将更改提交到网络/数据库之前保持对案例活动被破坏的更改。