问题描述
- 为什么我的滚动条没有滚动?
- 代码如下:
public class MainActivity extends ActionBarActivity {
private ViewPager viewPager;
private ImageView cursor;
private Listlists=new ArrayList<>();
private TextView t1t2t3;
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(00); 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.fragment1null)); lists.add(inflater.inflate(R.layout.fragment2null)); 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(one000); } else if (currindex==2){ animation=new TranslateAnimation(two000); } break; case 1: if (currindex==0){ animation=new TranslateAnimation(offestone00); } else if (currindex==2){ animation=new TranslateAnimation(twoone00); } break; case 2: if (currindex==0){ animation=new TranslateAnimation(offesttwo00); } else if (currindex==1){ animation=new TranslateAnimation(onetwo00); } 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) { }}
}
运行效果截图:
开发-为什么我的滚动条没有滚动?-android滚动条">
解决方案
你的adapter没有getItem啊
解决方案二:
可以说的具体一点吗?谢谢
解决方案三:
没有滚动条的ScrollView
时间: 2024-09-15 11:22:05