码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-11-27 14:30:21      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   io   ar   os   sp   java   for   

直接上代码:

<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>


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

标签:des   android   style   io   ar   os   sp   java   for   

原文地址:http://blog.csdn.net/coderinchina/article/details/41545265

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!