如何修复“使用viewPager进行的一项活动中的3个片段,需要先验证前两页,然后才能进入第三页”

时间:2019-04-01 08:52:44

标签: android android-fragments android-viewpager fragmentpageradapter

我想创建一个createOrderActivity,其中包含三个片段,例如服务信息,ScheduleInfo,确认

服务信息片段具有editText 如果单击NextButton(位于CreateOrderActivity中),请首先检查验证editText。然后移动ScheduleFragment页面。 如果前两页都可以通过验证,则转到“确认片段”页面。

这是FragmentViewpagerAdapter类

public class FragmentViewPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public FragmentViewPagerAdapter(FragmentManager manager) {
    super(manager);
}

@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}

@Override
public int getCount() {
    return fragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
    fragmentList.add(fragment);
    fragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
    return fragmentTitleList.get(position);
}

}

在CreateOrderActivity类中

        @Override
        public void onPageSelected(int position) {

            boolean checkSch= false;
            if (position == 1) {

                ServiceInfoFragment serviceInfoFragment = new ServiceInfoFragment();

                //checking validation from ServiceInfoFragment fragment Class
                if (serviceInfoFragment.checkServiceValidation()) {


                    checkSch = true;
                    //Toast.makeText(CreateOrderActivity.this, "Validation okay", Toast.LENGTH_SHORT).show();
                }else {
                    checkSch = false;
                   // Toast.makeText(CreateOrderActivity.this, "Please check validation", Toast.LENGTH_SHORT).show();
                    pagerCreateOrder.setCurrentItem(position-1);
                }
            }
            if (position == 2) {

                if (checkSch){
                    ScheduleFragment scheduleFragment = new ScheduleFragment();
                    if (scheduleFragment.checkScheduleValidation()) {
                        Toast.makeText(CreateOrderActivity.this, "Validation okay", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(CreateOrderActivity.this, "Please check S validation", Toast.LENGTH_SHORT).show();
                        pagerCreateOrder.setCurrentItem(position-1);
                    }

                }else {

                    Toast.makeText(CreateOrderActivity.this, "Please check validation", Toast.LENGTH_SHORT).show();
                    pagerCreateOrder.setCurrentItem(position-2);
                }


            }


        }
  

//检查ScheduleFragment类中的checkScheduleValidation()。返回空异常

*在片段中清除波纹管方法*

    public boolean checkServiceValidation(){


    return true;
}

我上传了这张图片

I did like this image

I used this Reference

1 个答案:

答案 0 :(得分:0)

我以自己的方式找到了解决方案。我已经在viewpager中加载了三个片段。在第二个片段中,有一个edittext。在单击下一步按钮时,将进行验证以检查电子邮件。根据验证,将加载下一个片段。所有片段都作为单例加载。您可能由于多个片段实例而导致了空指针异常。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private SectionsPagerAdapter mSectionsPagerAdapter;
private FragmentTwo fragmentTwo;

private ViewPager mViewPager;
Button back, next;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    back = findViewById(R.id.back);
    next = findViewById(R.id.next);
    next.setOnClickListener(this);
    back.setOnClickListener(this);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.vp_viewpager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            if (i==2){
                if (!fragmentTwo.checkEditText()) {
                    Toast.makeText(getApplicationContext(),"False",Toast.LENGTH_LONG).show();
                    mViewPager.setCurrentItem(i-1);
                    return;
                }
            }


        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });


}

private void changeViewPagerPosition(int position) {
    int totalCount = mViewPager.getAdapter().getCount();
    if (position < 0 || position >= totalCount) {
        return;
    }
    mViewPager.setCurrentItem(position);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public void onClick(View view) {
    int currentViewpagerPosition = mViewPager.getCurrentItem();

    switch (view.getId()) {
        case R.id.back:
            changeViewPagerPosition(currentViewpagerPosition - 1);
            break;
        case R.id.next:
            if (currentViewpagerPosition==1){
                if (!fragmentTwo.checkEditText()) {
                    Toast.makeText(getApplicationContext(),"Falsee",Toast.LENGTH_LONG).show();
                    return;
                }

            }
            changeViewPagerPosition(currentViewpagerPosition + 1);
            break;
    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {

            case 0:
                return FragmentOne.newInstance(position);
            case 1:
                return fragmentTwo=FragmentTwo.getInstance();
            case 2:
                return FragmentThree.newInstance(position);
            default:
                return FragmentThree.newInstance(position);

        }

    }

    @Override
    public int getCount() {
        return 3;
    }
}

}

片段2

public  class FragmentTwo extends Fragment  {

EditText email;
private static FragmentTwo fragment=null;


public FragmentTwo() {
}

public static FragmentTwo getInstance() {
    if (fragment == null){
        fragment = new FragmentTwo();
    }
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_two, container, false);
   return rootView;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    email = getView().findViewById(R.id.editText);

}


public boolean emailValidator()
{
    Pattern pattern;
    Matcher matcher;
    final String EMAIL_PATTERN = "^[_A-Za-z0-" +
            "9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    pattern = Pattern.compile(EMAIL_PATTERN);
    matcher = pattern.matcher(email.getText().toString());
    return matcher.matches();
}


public Boolean checkEditText(){
    if (emailValidator()){
        return true;
    }
    return false;
}

}