ViewPager滑动监听

VIewPager不动的时候是这样的



手指拖动时是这样的ViewPager滑动监听_第1张图片

还有这样的


可以看到Tab下划线随着VIewPager的滑动而做平移。


要达到这种效果

需要setOnPageChangeListener

@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			int s = arg0 * lineWdith + (int) (arg1 * lineWdith);
			Log.i("haha", s + "");
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT);
			layoutParams.width = lineWdith;
			layoutParams.setMargins(s, 0, 0, 0);
			tv_line.setLayoutParams(layoutParams);

		}



代码:

public class MainActivity extends Activity {

	private TextView tv1, tv2, tv3, tv4, tv5;
	private TextView tv_line;
	private ViewPager vp;

	private int lineWdith;
	private ArrayList views;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lineWdith = getResources().getDisplayMetrics().widthPixels / 5;
		initView();
	}

	private void initView() {
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		tv3 = (TextView) findViewById(R.id.tv3);
		tv4 = (TextView) findViewById(R.id.tv4);
		tv5 = (TextView) findViewById(R.id.tv5);
		tv_line = (TextView) findViewById(R.id.tv_line);

		vp = (ViewPager) findViewById(R.id.vp);
		views = new ArrayList();
		LayoutInflater inflater = getLayoutInflater();

		views.add(inflater.inflate(R.layout.page_batman, null));
		views.add(inflater.inflate(R.layout.page_flash, null));
		views.add(inflater.inflate(R.layout.page_spiderman, null));
		views.add(inflater.inflate(R.layout.page_superman, null));
		views.add(inflater.inflate(R.layout.page_wonderwoman, null));

		vp.setAdapter(new MyViewPager(views));
		vp.setOnPageChangeListener(new MyOnPagerListener());

		tv1.setOnClickListener(new MyOnClick());
		tv2.setOnClickListener(new MyOnClick());
		tv3.setOnClickListener(new MyOnClick());
		tv4.setOnClickListener(new MyOnClick());
		tv5.setOnClickListener(new MyOnClick());

	}

	private class MyOnPagerListener implements OnPageChangeListener {

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

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			int s = arg0 * lineWdith + (int) (arg1 * lineWdith);
			Log.i("haha", s + "");
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT);
			layoutParams.width = lineWdith;
			layoutParams.setMargins(s, 0, 0, 0);
			tv_line.setLayoutParams(layoutParams);

		}

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

		}

	}

	class MyViewPager extends PagerAdapter {
		public ArrayList list;

		public MyViewPager(ArrayList list) {
			this.list = list;
		}

		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(list.get(position), 0);

			return list.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(list.get(position));
		}

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

	}

	protected class MyOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tv1:
				vp.setCurrentItem(0, false);
				break;
			case R.id.tv2:
				vp.setCurrentItem(1, false);
				break;
			case R.id.tv3:
				vp.setCurrentItem(2, false);
				break;
			case R.id.tv4:
				vp.setCurrentItem(3, false);
				break;
			case R.id.tv5:
				vp.setCurrentItem(4, false);
				break;

			default:
				break;
			}

		}

	}

}


DEMO


你可能感兴趣的:(ViewPager滑动监听)