兼容viewpager中嵌套的viewpager自定义控件

原创 2016年08月31日 10:35:21

项目中viewpager市经常使用的控件。

一旦项目大起来之后经常使用viewpager中嵌套viewpager 。

那么使用时候有很多地方都会出现冲突。

这里模仿viewpager写了一个自定义viewpager 具有回收功能的viewpager 可以减轻内存消耗。

代码如下:

public class ViewPagerHorizontalScrollView extends HorizontalScrollView {

	Context context;
	PagerScrollAdapter pagerScrollAdapter;
	LinearLayout linearLayout;
	boolean isOnce = true;
	int mWitdh;
	private FrameLayout.LayoutParams layoutParams;
	int xDown;
	long startTime, endTime;
	int lastIndex;
	int cacheNums = 1;// save cache
	FrameLayout.LayoutParams layoutParams2;

	public ViewPagerHorizontalScrollView(Context context,
			PagerScrollAdapter pagerScrollAdapter) {
		super(context);
		this.context = context;
		this.pagerScrollAdapter = pagerScrollAdapter;
		setHorizontalScrollBarEnabled(false);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);

		if (changed && isOnce) {
			isOnce = false;
			
			mWitdh = getWidth();
			linearLayout= new LinearLayout(context);
			layoutParams=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
			layoutParams2=new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			linearLayout.setLayoutParams(layoutParams);
			linearLayout.setOrientation(LinearLayout.HORIZONTAL);
			linearLayout.setBackgroundColor(context.getResources().getColor(android.R.color.holo_red_light));
			removeAllViews();
			addView(linearLayout);
			if (pagerScrollAdapter.getCount()<=0) {
				return;
			}
			//add child 
			linearLayout.removeAllViews();
			for (int i = 0; i < pagerScrollAdapter.getCount(); i++) {
				LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(mWitdh, getHeight());
				FrameLayout frameLayout=new FrameLayout(context);
				frameLayout.setLayoutParams(layoutParams);
				linearLayout.addView(frameLayout);
			}
			RecylerAddView();
			
		}
	}
	
	void RecylerAddView(){
		//clear prev
		int preNum = lastIndex - cacheNums;
		if (preNum>0) {//need recyler
			for (int i = 0; i < preNum; i++) {
				int childNum = ((FrameLayout)linearLayout.getChildAt(i)).getChildCount();
				if (childNum>0) {
					((FrameLayout)linearLayout.getChildAt(i)).removeAllViews();
				}
			}
		}
		//clear next
		int nextNum = lastIndex + cacheNums;
		if (nextNum < pagerScrollAdapter.getCount() - 1) {
			for (int i = nextNum+1; i <= pagerScrollAdapter.getCount()-1; i++) {
				int childNum = ((FrameLayout)linearLayout.getChildAt(i)).getChildCount();
				if (childNum>0) {
					((FrameLayout)linearLayout.getChildAt(i)).removeAllViews();
				}
			}
		}
		//add viewView
		for (int i = preNum; i <= nextNum; i++) {
			if (i<0 || i > pagerScrollAdapter.getCount()-1) {
				continue;
			}
			int childNum = ((FrameLayout)linearLayout.getChildAt(i)).getChildCount();
			if (childNum==0) {
				//add view
				View view=pagerScrollAdapter.getView(i);
				view.setLayoutParams(layoutParams2);
				((FrameLayout)linearLayout.getChildAt(i)).addView(view);
			}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (ev.getAction()==MotionEvent.ACTION_DOWN) {
			startTime = System.currentTimeMillis();
			xDown = (int)ev.getRawX();
		}
		if (ev.getAction()==MotionEvent.ACTION_UP || ev.getAction()==MotionEvent.ACTION_CANCEL) {
			StopScrollView(ev);
			return true;
		}
		return super.onTouchEvent(ev);
	}
	
	public void setCurrentView(int position){
		smoothScrollTo((position * mWitdh), 0);
		if (mListener!= null && lastIndex!=position) {
			setCurrentPage(position);
		}
	}
	
	void StopScrollView(MotionEvent ev){
		endTime = System.currentTimeMillis();
		int xUp = (int)ev.getRawX();
		int distance = Math.abs(xUp-xDown);
		int moveNums = xUp - xDown;
		int moveX = getScrollX();
		int leftorright = moveX%mWitdh;
		int nums = moveX/mWitdh;
		if ( (endTime-startTime)<470 && distance > 50 ) {
			if (moveNums<0) {
				// xiang left
				smoothScrollTo(((nums+1)*mWitdh), 0);
				if (mListener!= null && lastIndex!=(nums+1)) {
					setCurrentPage(nums+1);
				}
			}else {
				// xiang right
				smoothScrollTo((nums*mWitdh), 0);
				if (mListener!= null && lastIndex!=(nums)) {
					setCurrentPage(nums);
				}
			}
			return;
		}
		if (leftorright< mWitdh/2) {
			//left
			smoothScrollTo((nums*mWitdh), 0);
			if (mListener!= null && lastIndex!=(nums)) {
				setCurrentPage(nums);
			}
		}else {
			//right
			smoothScrollTo(((nums+1)*mWitdh), 0);
			if (mListener!= null && lastIndex!=(nums+1)) {
				setCurrentPage(nums+1);
			}
		}
	}
	
	void setCurrentPage(int index){
		mListener.currentPgae(index);
		lastIndex = index;
		RecylerAddView();
	}

	public interface PagerScrollAdapter {
		int getCount();
		View getView(int pos);
	}
	
	onPageChangeListener mListener;
	
	public void setPageListener(onPageChangeListener mChangeListener){
		this.mListener = mChangeListener;
	}
	
	public interface onPageChangeListener{
		void currentPgae(int index);
	}



}


使用代码:

        ViewPagerHorizontalScrollView view=new ViewPagerHorizontalScrollView(getApplicationContext(), new ViewPagerHorizontalScrollView.PagerScrollAdapter() {
			
			@Override
			public View getView(int pos) {
				//获取每个项目试图
				TextView textView=new TextView(getApplicationContext());
				textView.setText("编号"+pos);
				return textView;
			}
			
			@Override
			public int getCount() {
				// 获取数量
				return 10;
			}
		});
        //添加进入布局中 只支持代码添加
        //iblShopContentViewContent.addView(View);
        //view.setCurrentView(0);//选中每一个项目




版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Android自定义LinearLayout实现左右侧滑菜单,完美兼容ListView、ScrollView、ViewPager等滑动控件

国际惯例,先来效果图 在阅读本文章之前,请确定熟悉【Scroller】相关的知识,如果不熟悉,请小伙伴儿先百度后再来吧。 假如你已经知道【Scroller】了,那么就接着往下看吧。...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)