如何使用viewpager限制页面数并拖动到最后一条记录?

时间:2015-07-24 07:23:24

标签: android

假设我在数据库中有1000条记录,我想在viewpager上加载它们。我也希望我的viewpager仅限于20页。如你所见,如果viewpager到达最后位置(20),它就不能再拖动了。如果最后一个位置不是最后一个记录,我该如何拖动更多?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPagerHelper mViewPagerHelper = ViewPagerHelper.getInstance();
        mViewPagerHelper.dateBarFunction(this);

    }

}

ViewPagerHelper类

public class ViewPagerHelper {
    private static ViewPagerHelper sViewPagerHelper = null;
    private String[] mNumberList= new String[1001];
    private ViewPager mViewPager;

    public ViewPagerHelper() {
    }

    public static ViewPagerHelper getInstance() {
        if (sViewPagerHelper == null) {
            sViewPagerHelper = new ViewPagerHelper();
        }
        return sViewPagerHelper;
    }

    public void dateBarFunction(Activity activity){
        mViewPager= (ViewPager) activity.findViewById(R.id.date_area);
        mViewPager.setBackgroundColor(Color.WHITE);
        final ViewPagerAdapter adapter;
        adapter = new ViewPagerAdapter(activity, getNumberList());
        mViewPager.setAdapter(adapter);
        mViewPager.setCurrentItem(20);
        mViewPager.setOffscreenPageLimit(5);


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }


    private String[] getNumberList() {
        Calendar currentDate = Calendar.getInstance();
        for (int i = 0; i <= 1000; i++) {
            mNumberList[i] = ""+i;
        }

        return mNumberList;

    }


}

ViewPagerAdapter类

public class ViewPagerAdapter extends PagerAdapter {
    Context context;
    LayoutInflater inflater;
    String[] mNumberList;


    public ViewPagerAdapter(Context context, String[] mNumberList) {
        this.context = context;
        this.mNumberList= mNumberList;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_adapter, container, false);


        TextView dateArea = (TextView) itemView.findViewById(R.id.dateArea);
        dateArea.setText(mNumberList[position]);


        ((ViewPager) container).addView(itemView);
        return itemView;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 20;
    }

}

0 个答案:

没有答案