我有一个管理某些顺序片段实例的活动和一个ViewPager,其适配器的类型为FragmentStatePagerAdapter。这些片段是顺序因素,因为我想创建一种人们必须遵循的向导。一切正常,但我想在用户想要转到向导的下一页(片段)时禁用滑动,如果他还没有完成当前页面中的所有字段。
package edu.polimi.dima.home121;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import edu.polimi.dima.home121.model.Place;
/**
* Created by leo on 13/01/2015.
*/
public class NewPlaceActivity extends ActionBarActivity
implements NewPlaceSummaryFragment.OnFragmentInteractionListener,
NewPlaceWizardFirstStepFragment.OnFragmentInteractionListener,
NewPlaceWizardSecondStepFragment.OnFragmentInteractionListener,
NewPlaceWizardThirdStepFragment.OnFragmentInteractionListener,
NewPlaceWizardFourthStepFragment.OnFragmentInteractionListener {
private final String TAG = getClass().getSimpleName();
private static Place place;
private static final int NUM_PAGES = 7;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
place = new Place();
setContentView(R.layout.activity_new_place);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), mPager, place);
mPager.setAdapter(mPagerAdapter);
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.newPlaceContainer, NewPlaceSummaryFragment.newInstance(place))
.commit();
}*/
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
@Override
public void onFragmentInteraction(Uri uri) {
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
private Place place;
private ViewPager mPager;
public ScreenSlidePagerAdapter(FragmentManager fm, ViewPager mPager, Place place) {
super(fm);
this.place = place;
this.mPager = mPager;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new NewPlace0();
case 1:
NewPlaceWizardFirstStepFragment f1 = new NewPlaceWizardFirstStepFragment().newInstance(place);
f1.setPager(mPager);
return f1;
case 2:
NewPlaceWizardSecondStepFragment f2 = new NewPlaceWizardSecondStepFragment().newInstance(place);
f2.setPager(mPager);
return f2;
case 3:
NewPlaceWizardThirdStepFragment f3 = new NewPlaceWizardThirdStepFragment().newInstance(place);
f3.setPager(mPager);
return f3;
case 4:
NewPlaceWizardFourthStepFragment f4 = new NewPlaceWizardFourthStepFragment().newInstance(place);
f4.setPager(mPager);
return f4;
case 5:
NewPlace99 f5 = new NewPlace99();
return f5;
case 6:
NewPlaceSaving last = NewPlaceSaving.newInstance(place);
return last;
default:
return null;
}
}
@Override
public int getCount() {
//TODO sistemare il conteggio delle pagine
return NUM_PAGES;
}
}
}
例如我的第一个片段是:
public class NewPlaceWizardFirstStepFragment extends Fragment implements View.OnClickListener,
DialogInterface.OnClickListener {
private final String TAG = this.getClass().getSimpleName();
private static Place place;
private OnFragmentInteractionListener mListener;
private static View view;
private ViewPager pager;
public NewPlaceWizardFirstStepFragment() {
}
private TextView lblAvailableFrom;
private Button btnSingleRoom;
private Button btnDoubleRoom;
private Button btnStudioFlat;
private Button btnApartment;
private Button btnChangeDate;
private Button btnNext;
private EditText tfStreet;
private EditText tfCivic;
private EditText tfStair;
private EditText tfFloor;
private EditText tfCity;
private EditText tfPostalCode;
public static NewPlaceWizardFirstStepFragment newInstance(Place place) {
NewPlaceWizardFirstStepFragment fragment = new NewPlaceWizardFirstStepFragment();
fragment.place = place;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_new_place_wizard_first_step, container, false);
setupUI();
return view;
}
public void setupUI() {
btnNext = (Button) view.findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
btnSingleRoom = (Button) view.findViewById(R.id.btnSingleRoom);
btnSingleRoom.setOnClickListener(this);
btnDoubleRoom = (Button) view.findViewById(R.id.btnDoubleRoom);
btnDoubleRoom.setOnClickListener(this);
btnStudioFlat = (Button) view.findViewById(R.id.btnStudioFlat);
btnStudioFlat.setOnClickListener(this);
btnApartment = (Button) view.findViewById(R.id.btnApartment);
btnApartment.setOnClickListener(this);
btnChangeDate = (Button) view.findViewById(R.id.changeDate);
btnChangeDate.setOnClickListener(this);
lblAvailableFrom = (TextView) view.findViewById(R.id.lblAvailableFrom);
tfStreet = (EditText) view.findViewById(R.id.tfStreet);
tfCivic = (EditText) view.findViewById(R.id.tfCivic);
tfStair = (EditText) view.findViewById(R.id.tfStair);
tfFloor = (EditText) view.findViewById(R.id.tfFloor);
tfCity = (EditText) view.findViewById(R.id.tfCity);
tfPostalCode = (EditText) view.findViewById(R.id.tfPostalAddress);
}
AlertDialog availableFromDialog;
private boolean enoughDataToContinue() {
boolean ret = true;
ret &= tfStreet.getText().length() != 0;
ret &= tfCivic.getText().length() != 0;
ret &= tfCity.getText().length() != 0;
return ret;
}
private void collectUserData() {
Address a = new Address();
a.setStreet(String.valueOf(tfStreet.getText()));
a.setNumber(String.valueOf(tfCivic.getText()));
a.setFloor(String.valueOf(tfFloor.getText()));
a.setStair(String.valueOf(tfStair.getText()));
a.setCity(String.valueOf(tfCity.getText()));
a.setPostalCode(String.valueOf(tfPostalCode.getText()));
place.setApartment(new Apartment());
place.getApartment().setAddress(a);
MiscServices.setCoordinates(place.getApartment());
Log.d(TAG, "first step done:" + new ObjectMapper().convertValue(place, Map.class).toString());
}
//TODO aggionare il toast
private void notifyUserOfInputIssues() {
Toast.makeText(getActivity().getApplicationContext(), "Complete all the fields to continue",
Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext:
if (enoughDataToContinue()) {
collectUserData();
pager.setCurrentItem(pager.getCurrentItem() + 1, true);
}
else
notifyUserOfInputIssues();
break;
case R.id.changeDate:
AvailableFromDialogBuilder availableFromDialogBuilder = new AvailableFromDialogBuilder(getActivity());
if (place.getAvailableFrom() != null)
availableFromDialogBuilder.setValue(place.getAvailableFrom());
availableFromDialogBuilder.setPositiveButton(R.string.my_continue, this);
availableFromDialog = availableFromDialogBuilder.show();
break;
case R.id.btnSingleRoom:
Log.v(TAG, "SingleRoom pressed");
clearSelection();
btnSingleRoom.setSelected(true);
place.setAccomodationType(Place.SINGLE_ROOM);
break;
case R.id.btnDoubleRoom:
Log.v(TAG, "DoubleRoom pressed");
clearSelection();
btnDoubleRoom.setSelected(true);
place.setAccomodationType(Place.DOUBLE_ROOM);
break;
case R.id.btnApartment:
Log.v(TAG, "Apartment pressed");
clearSelection();
btnApartment.setSelected(true);
place.setAccomodationType(Place.STANDARD_RENT);
break;
case R.id.btnStudioFlat:
Log.v(TAG, "StudioFlat pressed");
clearSelection();
btnStudioFlat.setSelected(true);
place.setAccomodationType(Place.STUDIO_FLAT);
break;
default:
Log.e(TAG, getResources().getString(R.string.error_ID) + v.getId());
}
}
public void clearSelection() {
btnApartment.setSelected(false);
btnSingleRoom.setSelected(false);
btnDoubleRoom.setSelected(false);
btnStudioFlat.setSelected(false);
}
;
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialog == availableFromDialog) {
DatePicker datePicker = (DatePicker) availableFromDialog.findViewById(R.id.dpAvailableFrom);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
cal.set(Calendar.MONTH, datePicker.getMonth());
cal.set(Calendar.YEAR, datePicker.getYear());
place.setAvailableFrom(cal.getTime());
lblAvailableFrom.setText(datePicker.getDayOfMonth() + " - " + cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, getResources().getConfiguration().locale) + " - " + datePicker.getYear());
} else Log.e(TAG, getResources().getString(R.string.error_dialog));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void setPager(ViewPager pager) {
this.pager = pager;
}
public ViewPager getPager() {
return pager;
}
public interface OnFragmentInteractionListener {
// TODO: valutare se serve
public void onFragmentInteraction(Uri uri);
}
}
即使必须始终启用向后滑动(从左到右),如何禁用滑动到下一个片段(从右到左)?
答案 0 :(得分:1)
首先,一点建议;对于以下每一行,不需要新的,因为该方法是静态的:
NewPlaceWizardFirstStepFragment f1 = NewPlaceWizardFirstStepFragment.newInstance(place);
那就是说,我可能只是向你的寻呼机适配器添加一些逻辑来限制已完成页面的数量。即如果您在步骤4中,则计数为4.完成步骤4后,递增计数以允许用户滑动到下一个片段。
请参阅此堆栈溢出帖子以获取示例:
答案 1 :(得分:0)
阻止页面滑动的最佳方法是控制是否在PageViewActivity.java(我的MainActivity)中的onPageScrolled内填充编辑文本
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.i("test", "onPageScrolled - position = " + position);
currentPage = position;
txt1 = (EditText) findViewById(R.id.name_input);
if(position == 0) {
if(txt1.getText().toString().trim().length() == 0) {
pager.setCurrentItem(0);
} else if(txt1.getText().toString().trim().length() > 0) {
// DO NOTHING
}
}
}