有条理地刷每一个片段

时间:2017-09-19 06:14:59

标签: android android-viewpager fragmentpageradapter

我有16个执行不同功能的片段。我想加载像Fragment1,Fragment16,Fragment2,Fragment16,Fragment3,Fragment16等系列片段。我每次都重复片段16。 Fragment16有30个复选框,我希望每次进入下一个片段之前都要检查它们。我的代码段在下面。

public class MainActivity extends AppCompatActivity {

private MyPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

TinyDB tinydb;
ArrayList<Model> mList;
ArrayList<Model> CheckList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tinydb = new TinyDB(this);
    mList = retrieveModel();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), this);
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
}
}

适配器是:

public class MyPagerAdapter extends FragmentPagerAdapter {

private Context context;
public MyPagerAdapter(FragmentManager fm) {
    super(fm);
}
@Override
public Fragment getItem(int position) {

    Fragment fragment = null;

    switch (position) {
        case 0:
            fragment = new Fragment1();
            break;
        case 1:
            fragment = new Fragment1A();
            break;
        case 2:
            fragment = new Fragment2();
            break;
        case 3:
            fragment = new Fragment1A();
            break;
        case 4:
            fragment = new Fragment3();
            break;
        case 5:
            fragment = new Fragment1A();
            break;
        case 6:
            fragment = new Fragment4();
            break;
        case 7:
            fragment = new Fragment1A();
            break;
        case 8:
            fragment = new Fragment5();
            break;
        case 9:
            fragment = new Fragment1A();
            break;
        case 10:
            fragment = new Fragment6();
            break;
        case 11:
            fragment = new Fragment1A();
            break;
        case 12:
            fragment = new Fragment7();
            break;
        case 13:
            fragment = new Fragment1A();
            break;
        case 14:
            fragment = new Fragment8();
            break;
        case 15:
            fragment = new Fragment1A();

            break;
        case 16:
            fragment = new Fragment9();

            break;
        case 17:
            fragment = new Fragment1A();

            break;
        case 18:
            fragment = new Fragment10();

            break;
        case 19:
            fragment = new Fragment1A();

            break;
        case 20:
            fragment = new Fragment11();

            break;
        case 21:
            fragment = new Fragment1A();

            break;
        case 22:
            fragment = new Fragment12();

            break;
        case 23:
            fragment = new Fragment1A();

            break;
        case 24:
            fragment = new Fragment13();

            break;
        case 25:
            fragment = new Fragment1A();

            break;
        case 26:
            fragment = new Fragment14();

            break;
        case 27:
            fragment = new Fragment1A();

            break;
        case 28:
            fragment = new Fragment15();

            break;
        case 29:
            fragment = new Fragment1A();

            break;
    }
    return fragment;
}

Fragment1是

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_images, container, false);
    ImageView img = view.findViewById(R.id.section_label);
    img.setImageResource(R.mipmap.one);
    return view;
}
}

1 个答案:

答案 0 :(得分:0)

I have not tried this yet
But you should try this...may be it will work :

1) Just check in your 16th fragment that all the checkboxes are checked or not using their ids
2) If they all are checked then user is able to swipe next page otherwise they can't swipe the page
3) Now you have to implement the functionality of disabling/enabling swiping...For that :
    a) yourviewpager.setOnTouchListener(new OnTouchListener() {           
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            yourpager.setCurrentItem(yourpager.getCurrentItem());
            return true;
        }
      });
    b) make it return false; otherwise

    Hope this will work for you...:-)