2

我面临 ViewPager 使其循环的问题。我有一个动态数据列表,每次滑动时,都必须使用动态数据更新视图。例如,我在列表中有大约 5 个对象说 A、B、C、D、EI 能够从 A -> B -> C -> D -> E 再次从 E -> D -> C 滑动 - > B -> A. 但我应该让它像在 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。

4

0 回答 0