我理解的安卓中MVC思想是利用Holder进行编程,展示布局,提供视图,将视图和数据进行绑定起来,在很多App中我们能见到广告条,广告条这里我们可以使用FramLayout进行填充,然后利用MVC的思想将提供的布局添加到FrameLayout中即可; 广告条的实现也可以利用Banner等开源框架等,这里就不列举了,感兴趣的朋友可以去搜下
当然由于我这里的项目的图片是设计师那边提供好的,并不是从网上获取的,所以我这里就直接写了VIEwPager来进行填充了
<!--轮播图--> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="328px"> <androID.support.v4.vIEw.VIEwPager androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:ID="@+ID/fragment_work_pic_vIEwpager"/> <!-- 指针容器 --> <relativeLayout androID:background="@androID:color/transparent" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:padding="5dp" androID:layout_alignBottom="@ID/fragment_work_pic_vIEwpager"> <!--<TextVIEw androID:textSize="12sp" androID:layout_gravity="left" androID:layout_marginleft="5dp" androID:ID="@+ID/tv_desc" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textcolor="@androID:color/white" androID:text="图片的描述"/>--> <linearLayout androID:layout_centerHorizontal="true" androID:layout_centerVertical="true" androID:ID="@+ID/ll_dots" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> </linearLayout> </relativeLayout>
逻辑:
package com.hanzheng.znxl.fragment; import androID.support.v4.vIEw.VIEwPager; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.Widget.ImageVIEw; import androID.Widget.linearLayout; import androID.Widget.relativeLayout; import com.hanzheng.znxl.R; import com.hanzheng.znxl.adapter.OnPagerchangelistenerImp; import com.hanzheng.znxl.adapter.WorkFragmentBannerAdapter; import com.hanzheng.znxl.base.BaseFragment; import com.hanzheng.znxl.base.MyApplication; import com.hanzheng.znxl.utils.ToastUtil; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; /** * Created by zmybi on 29/12/2016. */ public class WorkFragment extends BaseFragment { @Bind(R.ID.fragment_work_pic_vIEwpager) VIEwPager mFragmentWorkPicVIEwpager; @Bind(R.ID.ll_dots) linearLayout mLLDots; // @Bind(R.ID.tv_desc) // TextVIEw mTvDesc; @Bind(R.ID.ll_kaoqing) linearLayout mLlKaoqing; @Bind(R.ID.ll_execute) linearLayout mLlExecute; @Bind(R.ID.ll_daohang) linearLayout mLlDaohang; @Bind(R.ID.ll_sudden) linearLayout mLlSudden; @Bind(R.ID.ll_policething) linearLayout mLlPolicething; @Bind(R.ID.ll_collect) linearLayout mLlCollect; @Bind(R.ID.ll_search) linearLayout mLlSearch; @Bind(R.ID.ll_noticevoice) linearLayout mLlNoticevoice; @Bind(R.ID.iv_book) ImageVIEw mIvBook; @Bind(R.ID.rl_shouce) relativeLayout mRlShouce; @Bind(R.ID.ibtn_yuan) ImageVIEw mIbtnYuan; @Bind(R.ID.ibtn_helpbook) ImageVIEw mIbtnHelpbook; private int[] imageResIDs = {R.drawable.banner1,R.drawable.banner2}; private String[] descs = {"武汉保安集团","智能巡逻系统"}; private autoScrollTask mautoScrollTask; @OverrIDe public voID initData() { mFragmentWorkPicVIEwpager.addOnPagechangelistener(new OnPagerchangelistenerImp() { @OverrIDe public voID onPageSelected(int position) { changeDotAndDesc(position); } }); mFragmentWorkPicVIEwpager.setAdapter(new WorkFragmentBannerAdapter(imageResIDs)); initDot(); changeDotAndDesc(0); //默认选择第一页 mFragmentWorkPicVIEwpager.setCurrentItem(mFragmentWorkPicVIEwpager.getAdapter().getCount() / 2); if(mautoScrollTask == null) { mautoScrollTask = new autoScrollTask(); mautoScrollTask.start(); } //按下去停止轮播 mFragmentWorkPicVIEwpager.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw vIEw,MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: mautoScrollTask.stop(); break; case MotionEvent.ACTION_UP: mautoScrollTask.start(); break; } return false; } }); } private voID changeDotAndDesc(int position) { position = position % imageResIDs.length; // mTvDesc.setText(descs[position]); for(int i = 0; i < mLLDots.getChildCount();i++) { ImageVIEw iv_dot = (ImageVIEw) mLLDots.getChildAt(i); iv_dot.setSelected(false); if(position == i) { iv_dot.setSelected(true); } } } private voID initDot() { for(int i = 0; i < imageResIDs.length;i++) { int _5dp = dp2px(5); linearLayout.LayoutParams params = new linearLayout.LayoutParams(_5dp,_5dp); params.leftmargin = _5dp; ImageVIEw dot = new ImageVIEw(mActivity); dot.setLayoutParams(params); dot.setBackgroundResource(R.drawable.selector_dot); mLLDots.addVIEw(dot); } } public int dp2px(int dp) { float density = getResources().getdisplayMetrics().density; return (int) (dp * density + .5f); } @OverrIDe public VIEw initVIEw() { // Todo: inflate a fragment vIEw VIEw rootVIEw = VIEw.inflate(mActivity,R.layout.fragment_work,null); ButterKnife.bind(this,rootVIEw); return rootVIEw; } @OverrIDe public voID onDestroyVIEw() { super.onDestroyVIEw(); ButterKnife.unbind(this); } @OnClick({R.ID.rl_shouce,R.ID.ibtn_yuan,R.ID.ibtn_helpbook,R.ID.ll_kaoqing,R.ID.ll_execute,R.ID.ll_daohang,R.ID.ll_sudden,R.ID.ll_policething,R.ID.ll_collect,R.ID.ll_search,R.ID.ll_noticevoice}) public voID onClick(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.ll_kaoqing: ToastUtil.showToast(mActivity,"考勤"); break; case R.ID.ll_execute: ToastUtil.showToast(mActivity,"勤务"); break; case R.ID.ll_daohang: ToastUtil.showToast(mActivity,"网店"); break; case R.ID.ll_sudden: ToastUtil.showToast(mActivity,"突发"); break; case R.ID.ll_policething: ToastUtil.showToast(mActivity,"警情"); break; case R.ID.ll_collect: ToastUtil.showToast(mActivity,"认证"); break; case R.ID.ll_search: ToastUtil.showToast(mActivity,"信息查询"); break; case R.ID.ll_noticevoice: ToastUtil.showToast(mActivity,"发布公告"); break; case R.ID.rl_shouce: ToastUtil.showToast(mActivity,"手册"); break; case R.ID.ibtn_yuan: ToastUtil.showToast(mActivity,"预案流程"); break; case R.ID.ibtn_helpbook: ToastUtil.showToast(mActivity,"帮助手册"); break; } } private class autoScrollTask implements Runnable{ public voID start() { stop(); MyApplication.getMainThreadHandler().postDelayed(this,2000); } public voID stop() { MyApplication.getMainThreadHandler().removeCallbacks(this); } @OverrIDe public voID run() { int currentItem = mFragmentWorkPicVIEwpager.getCurrentItem(); currentItem++; mFragmentWorkPicVIEwpager.setCurrentItem(currentItem); start(); } } }
这里轮播图下方的指示器(小点)或者图片描述都是可以加上的,具体依据公司美工的要求即可
做了个按下去停止轮播的优化; 给vIEwpager设计点击监听,重写三个方法,抬起则start(),走run方法vIEwpager跳到下一页,并走autoScrollTask中的start(),方法,主线程的Handler发送延时消息,则继续循环run方法, 实现轮播图的无线循环
对了,说下轮播图的PagerAdapter的写法
package com.hanzheng.znxl.adapter; import androID.support.v4.vIEw.PagerAdapter; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.ImageVIEw; /** * Created by zmybi on 31/12/2016. */ public class WorkFragmentBannerAdapter extends PagerAdapter { private int[] imageResIDs; public WorkFragmentBannerAdapter(int[] imageResIDs) { this.imageResIDs = imageResIDs; } @OverrIDe public int getCount() { return imageResIDs.length * 1000000; } @OverrIDe public boolean isVIEwFromObject(VIEw vIEw,Object object) { return vIEw == object; } @OverrIDe public Object instantiateItem(VIEwGroup container,int position) { ImageVIEw iv = new ImageVIEw(container.getContext()); position = position % imageResIDs.length; iv.setBackgroundResource(imageResIDs[position]); iv.setScaleType(ImageVIEw.ScaleType.FIT_XY); container.addVIEw(iv); return iv; } @OverrIDe public voID destroyItem(VIEwGroup container,int position,Object object) { container.removeVIEw((ImageVIEw)object); } }
这里返回一个很大的数,其实也没必要返回Integer.MAX_VALUE; 因为这样写的话还要对余数进行判断,让下方的Indicator和上方的图片切换保持一致, 当然,这里由于美工直接给的图,所以我直接放在工程下了,实际如果是从网上获取的,则使用Picasso或者GlIDe图片加载框架即可
至此,一个简单的图片轮播功能就已经实现了。
总结以上是内存溢出为你收集整理的利用MVC编写广告条轮播效果全部内容,希望文章能够帮你解决利用MVC编写广告条轮播效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)