android ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法

直接上代码:

<span style="font-size:18px;">package com.example.viewpagertest;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.EdgeEffectCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnPageChangeListener {
	private ViewPager viewpager;
	private List<ImageView> views;
	private EdgeEffectCompat leftEdge;
	private EdgeEffectCompat rightEdge;
	private int[] ids = {R.drawable.iframe_4,R.drawable.iframe_5,R.drawable.iframe_6,R.drawable.iframe_7,R.drawable.iframe_8};
	private MyAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}
	private void initView() {
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		initViewPager();
		initData();
	}
	private void initData() {
		views = new ArrayList<ImageView>();
		for(int i=0;i<ids.length;i++){
			ImageView iv = new ImageView(this);
			iv.setImageResource(ids[i]);
			views.add(iv);
		}
		adapter = new MyAdapter();
		viewpager.setAdapter(adapter);
		viewpager.setOnPageChangeListener(this);
	}
	
	class MyAdapter extends PagerAdapter{
		@Override
		public int getCount() {
			return views.size();
		}

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

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

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			((ViewPager)container).addView(views.get(position));
			return views.get(position);
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		if (leftEdge != null && rightEdge != null) {
			leftEdge.finish();
			rightEdge.finish();
			leftEdge.setSize(0, 0);
			rightEdge.setSize(0, 0);
		}
	}
	@Override
	public void onPageSelected(int arg0) {
		
	}
	private void initViewPager() {
		try {
			Field leftEdgeField = viewpager.getClass().getDeclaredField("mLeftEdge");
			Field rightEdgeField = viewpager.getClass().getDeclaredField("mRightEdge");
			if (leftEdgeField != null && rightEdgeField != null) {
				leftEdgeField.setAccessible(true);
				rightEdgeField.setAccessible(true);
				leftEdge = (EdgeEffectCompat) leftEdgeField.get(viewpager);
				rightEdge = (EdgeEffectCompat) rightEdgeField.get(viewpager);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
</span>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。