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

项目中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);//选中每一个项目




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值