具有动态数据的循环视图寻呼机

时间:2013-02-14 04:50:40

标签: android android-viewpager android-pageradapter

我正面临ViewPager的问题,使其循环播放。我有一个动态的数据列表,每次刷卡时都必须使用动态数据更新视图。例如,我在列表中有大约5个对象,例如A,B,C,D,E我可以从A - >中滑动。 B - > C - > D - > E再次来自E - > D - > C - > B - >答:但是我应该像E之后那样循环,它应该在向右滑动时转到A,在A之后它应该在向左滑动时转到E.请有人告诉我如何做到这一点。非常感谢。

这是我到目前为止尝试过的代码片段。

public class FlingActivity extends BaseActivity implements OnClickListener{

private ViewPager pager;
private int position;
private int total;
private ArrayList<DataBean> beanList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.main);

    if (this.getIntent().hasExtra("position")) {
        position = (int) this.getIntent().getExtras().getInt("position");
    }
    if(getIntent().hasExtra("listDetails")) {
        beanList = (ArrayList<DataBean>)getIntent().getSerializableExtra("listDetails");
    }

    if(beanList != null){
        total = beanList.size();
        pager = (ViewPager) findViewById(R.id.pager);
        ((RelativeLayout) findViewById(R.id.right_arrow)).setOnClickListener(this);
        ((RelativeLayout) findViewById(R.id.left_arrow)).setOnClickListener(this);  
        pager.setVisibility(View.VISIBLE);

        pager.setAdapter(new MyPagerAdapter(beanList));
        pager.setOnPageChangeListener(new OnPageChangeListener() {
            int lastPosition;
            int posOffset=0;
            public void onPageSelected(int arg0) {
                position=arg0;
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
                if(arg0!=0 && arg1==0 && arg2 == 0){
                    lastPosition=arg0;
                }
            }

            public void onPageScrollStateChanged(int arg0) {
                if(position == total - 1){
                    position=total + 1;
                }
                if(arg0 == ViewPager.SCROLL_STATE_IDLE && position > total){
                    pager.setCurrentItem(0, true);
                }
            }
        });
        pager.setCurrentItem(position, true);

    }

}   

public void onClick(View v) {
    switch(v.getId()) {
    case R.id.left_arrow:
        if(pager != null) {
            pager.setCurrentItem(pager.getCurrentItem() - 1, true);
        }

        break;
    case R.id.right_arrow:
        if(pager != null) {             
            pager.setCurrentItem(pager.getCurrentItem() + 1, true);
        }

        break;
    }       
}

private class MyPagerAdapter extends PagerAdapter {
    private ArrayList<DataBean> beanList;
    public MyPagerAdapter(ArrayList<DataBean> list) {
        this.beanList = list;
    }

    public int getCount() {
        return beanList.size();
    }

    public Object instantiateItem(View collection, int position) {
        View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_view, null);

        DataBean dataBean = beanList.get(position); 
        TextView name = (TextView) view.findViewById(R.id.name);
        name.setText(dataBean.getName());

        TextView description = (TextView) view.findViewById(R.id.desc);
        description.setText("Description " 
                                + dataBean.getDesc());

        ((ViewPager) collection).addView(view, 0);
        return view;            
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }       
}

}

在上面的代码中,在转到E后,页面会自动滑动到A,但实际上只有在用户滑动时才会转到A.

0 个答案:

没有答案