扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章给大家分享的是有关如何使用MVC编写广告条轮播效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、重庆小程序开发公司、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了铜鼓免费建站欢迎大家使用!
代码如下:
逻辑:
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编写广告条轮播效果”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流