为什么我的滚动条没有滚动?

代码如下:
public class MainActivity extends ActionBarActivity {
private ViewPager viewPager;
private ImageView cursor;
private Listlists=new ArrayList<>();
private TextView t1,t2,t3;
private int currindex=0;//当前页卡编号
private int tpwidth;//滚动条宽度
private int offest;//滚动条偏移量

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    viewPagerView();
    initImageView();
}

private void initImageView() {
    cursor= (ImageView) findViewById(R.id.cursor);
    tpwidth= BitmapFactory.decodeResource(getResources(),R.drawable.slidings).getWidth();//获得滚动条宽度
    DisplayMetrics dm=new DisplayMetrics();
    System.out.println(tpwidth + "aaaaaaaaaa");
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenWidth=dm.widthPixels;//获得屏幕分辨率
    System.out.println(screenWidth + "!!!!!!!!!!!!!!!!!!!");
    offest=(screenWidth/3-tpwidth);
    System.out.println(offest + "1111111");
    Matrix matrix=new Matrix();
    matrix.postTranslate(0,0);
    cursor.setImageMatrix(matrix);//设置滚动条开始位置
}

private void viewPagerView() {
    viewPager= (ViewPager) findViewById(R.id.viewPager);
    cursor= (ImageView) findViewById(R.id.cursor);
    LayoutInflater inflater =getLayoutInflater();
    lists.add(inflater.inflate(R.layout.fragment1,null));
    lists.add(inflater.inflate(R.layout.fragment2,null));
    lists.add(inflater.inflate(R.layout.fragment3, null));
    viewPager.setAdapter(new MyPageAdapter(lists));
    viewPager.setCurrentItem(0);//默认第一个
    viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
}

private void initView() {
    t1= (TextView) findViewById(R.id.TextView1);
    t2= (TextView) findViewById(R.id.TextView2);
    t3= (TextView) findViewById(R.id.TextView3);
    t1.setOnClickListener(new MyOnClickListener(0));
    t2.setOnClickListener(new MyOnClickListener(1));
    t3.setOnClickListener(new MyOnClickListener(2));
}


private class MyOnClickListener implements View.OnClickListener {
    private int index=0;
    public MyOnClickListener(int i) {
        index=i;
    }


    public void onClick(View v) {
        viewPager.setCurrentItem(index);


    }
}

private class MyPageAdapter extends PagerAdapter {
    private List<View>mlists;
    public MyPageAdapter(List<View> lists) {
          this.mlists=lists;
    }

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

    public void finishUpdate(View container) {

    }

    public Object instantiateItem(ViewGroup container, int position) {
        ((ViewPager) container).addView(mlists.get(position), 0);
        return mlists.get(position);
    }


    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView(mlists.get(position));
    }

    public boolean isViewFromObject(View view, Object o) {
        return view==o;
    }


    public void restoreState(Parcelable state, ClassLoader loader) {

    }


    public void startUpdate(ViewGroup container) {

    }

    public Parcelable saveState() {
        return null;
    }
}


private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
    int one=offest+tpwidth;//页卡一到页卡二的偏移量
    int two=one*2;//页卡一到页卡三的偏移量

    public void onPageSelected(int i) {
        Animation animation=null;
        switch (i){
            case 0 :
                if (currindex==1){
                    animation=new TranslateAnimation(one,0,0,0);
                }
                else if (currindex==2){
                    animation=new TranslateAnimation(two,0,0,0);
                }
                break;
            case 1:
                if (currindex==0){
                    animation=new TranslateAnimation(offest,one,0,0);
                }
                else if (currindex==2){
                    animation=new TranslateAnimation(two,one,0,0);
                }
                break;
            case 2:
                if (currindex==0){
                    animation=new TranslateAnimation(offest,two,0,0);
                }
                else if (currindex==1){
                    animation=new TranslateAnimation(one,two,0,0);
                }
                break;
        }
        currindex=i;
        animation.setFillAfter(true);//true:图片停在滚动结束的位置
        animation.setDuration(300);
        cursor.startAnimation(animation);
    }
    public void onPageScrolled(int i, float v, int i1) {

    }


    public void onPageScrollStateChanged(int i) {

    }
}

}
运行效果截图:
图片说明

2个回答

你的adapter没有getItem啊

qq_27214725
qq_27214725 可以说的详细一点吗?谢谢!
大约 4 年之前 回复

可以说的具体一点吗?谢谢

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!