时间:2021-05-19
实现轮播效果有2种方法:
(1)使用ViewPager+ImageView,通过Handler进行间隔发送消息,实现自动轮播效果
xml代码:
<android.support.v4.view.ViewPager android:id="@+id/fragment_viewPager" android:layout_width="match_parent" android:layout_height="200dp" /> private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); list= (List<BannerBean>) msg.obj; //进行下载图片 for (int i = 0; i < 3; i++) { Picasso.with(getActivity()).load(list.get(i).getImgs()).resize(640,400).into(imgs.get(i)); } //展示图片 MyAdapter adapter=new MyAdapter(); viewPager.setAdapter(adapter); } }; private Handler handler2=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //获取当前viewPager的位置 int pos=viewPager.getCurrentItem(); //如果是最后一张图片就进行设置为第一张,实现循环 if(pos==imgs.size()-1){ pos=0; //否则进行下一张图片展示 }else{ pos++; } //设置viewPager的位置 viewPager.setCurrentItem(pos); //定时发送消息 handler2.sendEmptyMessageDelayed(1,2000); } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_recreation, container, false); initview(view); load(); //发送消息给handler2,实现自动轮播 handler2.sendEmptyMessageDelayed(1,2000); initListener(); return view; } private void load() { //下载图片地址 new Thread(){ @Override public void run() { super.run(); String json= Utils.getJsonFromNet(ContentUrls.headLine_head+0); if(!TextUtils.isEmpty(json)){ List<BannerBean>data= ParserBanner.getData(json); Message msg=new Message(); msg.obj=data; handler.sendMessage(msg); } } }.start(); data=new ArrayList<>(); headlineAdapter=new HeadlineAdapter(data,getActivity()); lv.setAdapter(headlineAdapter); new MyAsyncTask(data,headlineAdapter).execute(ContentUrls.headLine_head+num); } private void initview(View view) { lv = ((ListView) view.findViewById(R.id.lv)); headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_layout,null,false); viewPager = ((ViewPager)headerView.findViewById(R.id.fragment_viewPager)); lv.addHeaderView(headerView); list=new ArrayList<>(); imgs=new ArrayList<>(); for (int i = 0; i < 3; i++) { ImageView iv=new ImageView(getActivity()); imgs.add(iv); } }adapter:
class MyAdapter extends PagerAdapter{ @Override public int getCount() { return imgs.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imgs.get(position)); return imgs.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) {// super.destroyItem(container, position, object); container.removeView(imgs.get(position)); } }(2)使用SliderLayout第三方进行实现轮播
添加依赖:
compile 'com.squareup.picasso:picasso:2.5.2' compile 'com.daimajia.slider:library:1.1.5@aar' compile 'com.nineoldandroids:library:2.4.0'xml代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.daimajia.slider.library.SliderLayout android:id="@+id/header_slider" android:layout_width="match_parent" android:layout_height="160dp"></com.daimajia.slider.library.SliderLayout></LinearLayout>Java代码:
private ListView lv; private String[] title={"烟","超标","标准","审车合格"}; private int[] imgs={R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d}; private SliderLayout slider; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_home_page, container, false); lv = ((ListView) view.findViewById(R.id.homePage_lv)); View headerView=LayoutInflater.from(getActivity()).inflate(R.layout.header_layout,null,false); slider = ((SliderLayout) headerView.findViewById(R.id.header_slider)); lv.addHeaderView(headerView); initSlider(); initData(); return view; } private void initSlider() { for (int i = 0; i < 4; i++) { TextSliderView textSlider=new TextSliderView(getActivity()); textSlider.image(imgs[i]).description(title[i]); slider.addSlider(textSlider); textSlider.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() { @Override public void onSliderClick(BaseSliderView slider) { Toast.makeText(getActivity(),slider.getDescription(),Toast.LENGTH_SHORT).show(); } }); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
先来看看效果图其实写一个轮播图还是蛮简单的,我想了两种种方法,来实现轮播图(实际上细分是5种,但是其中两种是操作dom原生,三种是利用AngularJs的动画,
Android实现按两次返回键退出程序(两种方法)第一种方法://是否退出程序privatestaticBooleanisExit=false;//定时触发器p
JQuery和HTML5Canvas两种方法实现弹幕效果:方法一,JQuery实现。源码:JQuery弹幕.ctxt{background:#666;width
本文实例讲述了Android开发实现的自动换图片、轮播图效果。分享给大家供大家参考,具体如下:50行代码急速实现轮播图:先看效果:两个例子没太大区别,不过i是布
Android打电话有两种实现方法:第一种方法,拨打电话跳转到拨号界面。源代码如下:Intentintent=newIntent(Intent.ACTION_D