MainAcitivity类:
public class MainActivity extends FragmentActivity {
private int currentPage = 0;
private String[] abc = {"A","B","C"};
private String [] abc2 = {"B","C","D"};
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private ScreenSlidePagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
ScreenSlidePageFragment mScreenSlidePageFragment = new ScreenSlidePageFragment();
Bundle bundle = new Bundle();
bundle.putString("test", abc[position]);
if (position == 2){
bundle.putString("test", abc2[position]);
}
mScreenSlidePageFragment.setArguments(bundle);
return mScreenSlidePageFragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
if (currentPage == 2){
mPager.setCurrentItem(1,false);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
currentPage = position;
}
}
}
ScreenSlidePageFragment类:
public class ScreenSlidePageFragment extends Fragment {
public TextView txt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
Bundle bundle = getArguments();
String result = bundle.getString("test");
txt = (TextView) rootView.findViewById(R.id.content);
txt.setText(result);
return rootView;
}
}
我想要的是当用户拖动到C(位置2)时,它将成为中间页面(位置1),以便用户可以再次拖动到" D"但我根本不知道如何让它发挥作用:(。
我还想要,如果位置0有文字" A"或者位置2有文字" D",用户不能再拖动了。我是否正确使用上述方法?
答案 0 :(得分:0)
您可以使用https://github.com/pozitiffcat/cyclicview代替 例如:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CyclicView cyclicView = (CyclicView) findViewById(R.id.cyclic_view);
cyclicView.setAdapter(new CyclicAdapter() {
@Override
public int getItemsCount() {
return 10;
}
@Override
public View createView(int position) {
TextView textView = new TextView(MainActivity.this);
textView.setText(String.format("TextView #%d", position + 1));
return textView;
}
@Override
public void removeView(int position, View view) {
// Do nothing
}
});
}
}