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

来源:互联网 发布:jq 判断数组是否包含 编辑:程序博客网 时间:2024/05/19 22:27

项目中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 cacheFrameLayout.LayoutParams layoutParams2;public ViewPagerHorizontalScrollView(Context context,PagerScrollAdapter pagerScrollAdapter) {super(context);this.context = context;this.pagerScrollAdapter = pagerScrollAdapter;setHorizontalScrollBarEnabled(false);}@Overrideprotected 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 prevint preNum = lastIndex - cacheNums;if (preNum>0) {//need recylerfor (int i = 0; i < preNum; i++) {int childNum = ((FrameLayout)linearLayout.getChildAt(i)).getChildCount();if (childNum>0) {((FrameLayout)linearLayout.getChildAt(i)).removeAllViews();}}}//clear nextint 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 viewViewfor (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 viewView view=pagerScrollAdapter.getView(i);view.setLayoutParams(layoutParams2);((FrameLayout)linearLayout.getChildAt(i)).addView(view);}}}@Overridepublic 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 leftsmoothScrollTo(((nums+1)*mWitdh), 0);if (mListener!= null && lastIndex!=(nums+1)) {setCurrentPage(nums+1);}}else {// xiang rightsmoothScrollTo((nums*mWitdh), 0);if (mListener!= null && lastIndex!=(nums)) {setCurrentPage(nums);}}return;}if (leftorright< mWitdh/2) {//leftsmoothScrollTo((nums*mWitdh), 0);if (mListener!= null && lastIndex!=(nums)) {setCurrentPage(nums);}}else {//rightsmoothScrollTo(((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() {@Overridepublic View getView(int pos) {//获取每个项目试图TextView textView=new TextView(getApplicationContext());textView.setText("编号"+pos);return textView;}@Overridepublic int getCount() {// 获取数量return 10;}});        //添加进入布局中 只支持代码添加        //iblShopContentViewContent.addView(View);        //view.setCurrentView(0);//选中每一个项目




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果8手机弯了怎么办 苹果8p手机弯了怎么办? 美版iphone弯了怎么办 oppor9手机听筒声音小怎么办 苹果7摔弯了屏幕怎么办 苹果手机压弯了怎么办 京东买的电脑坏了怎么办 拼多多低价乱价怎么办 厂家不通过经销商直接发货怎么办 媳妇吵架说我全家有病怎么办 全家都不尊重媳妇怎么办 招商闪电贷有额度不通过怎么办 闪电贷页面登录不进去怎么办 91借钱极速逾期怎么办 论文没过拿不到毕业证怎么办 大学论文不过拿不到毕业证怎么办 转店被黑中介骗了钱怎么办? 被星外转铺骗了怎么办 店铺转了后悔了怎么办 商铺转让不出去怎么办? 和包券密码丢失怎么办 天猫购物卷兑换不了怎么办 淘宝新店每天只有几个访客怎么办 注册淘宝企业店铺需要审核怎么办 淘宝店铺被投诉知识产权怎么办 一般违规扣48分怎么办 金税盘处于报税期不能开票怎么办 小规模税率开错了怎么办 我是代购卖家被买家投诉偷税怎么办 天猫盒子内存不够怎么办 天猫品牌申请不通过怎么办 天猫商家发货发个空包裹怎么办 无限流量怎么办没有4g 海外直邮身份证过期了怎么办 买车的人不过户怎么办 天猫精灵球泡离线怎么办 花呗被骗了2万怎么办 天猫公司变更地址发票怎么办 支付宝自助解限怎么办 支付宝16岁限额怎么办 支付宝提不了现怎么办