首页 > 代码库 > android ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法
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>
android ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。