刚忙完了公司的项目,总算有些时间了,所以自己模仿公司的项目做了一些小demo,以后用。轮播图的效果,在Android的项目当中是比较常见的,其实现原理还是非常简单的。主要是利用timer不停的发送消息,通过handler处理消息并修改界面。

首先看一下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"    android:orientation="vertical" >    <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="150dp"        android:id="@+id/viewpager" />    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"        android:layout_marginTop="-10dp" android:layout_gravity="center_horizontal">     <ImageView android:id="@+id/img1" android:layout_width="5dp" android:layout_height="5dp" android:background="@drawable/dot_selector" />     <ImageView android:id="@+id/img2" android:layout_width="5dp" android:layout_height="5dp" android:background="@drawable/dot_selector"         android:layout_marginLeft="10dp" android:layout_marginRight="10dp"/>     <ImageView android:id="@+id/img3" android:layout_width="5dp" android:layout_height="5dp" android:background="@drawable/dot_selector"/>       </LinearLayout>    <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content"         android:orientation="horizontal" android:layout_marginTop="10dp" >        <RadioButton android:id="@+id/recommend_rb" android:layout_width="0dp" android:layout_height="match_parent"            android:layout_weight="1" android:text="推荐" android:button="@null" android:gravity="center"            android:textColor="#999999" android:padding="5dp"/>        <View android:layout_width="0.5dp" android:layout_height="match_parent" android:background="#cccccc"/>        <RadioButton android:id="@+id/circle_rb" android:layout_width="0dp" android:layout_height="match_parent"            android:layout_weight="1" android:text="圈子" android:button="@null" android:gravity="center"            android:textColor="#999999" android:padding="5dp"/>    </RadioGroup></LinearLayout>
xml文件中主要就是一个viewpager,下面的和轮播图都没有关系。

然后我们再看Java代码:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.tab_activity);listview=(ListView) findViewById(R.id.listview);imageviews=new ArrayList<ImageView>();headview=getLayoutInflater().inflate(R.layout.headview,null);listview.addHeaderView(headview);list=new ArrayList<String>();dots=new ArrayList<ImageView>();loadData();adapter=new MyAdapter();listview.setAdapter(adapter);initIV();viewpager=(ViewPager) headview.findViewById(R.id.viewpager);recommendRB=(RadioButton) headview.findViewById(R.id.recommend_rb);circleRB=(RadioButton) headview.findViewById(R.id.circle_rb);dot1=(ImageView) headview.findViewById(R.id.img1);dot2=(ImageView) headview.findViewById(R.id.img2);dot3=(ImageView) headview.findViewById(R.id.img3);dots.add(dot1);dots.add(dot2);dots.add(dot3);pageadapter=new PageAdapter();viewpager.setAdapter(pageadapter);viewpager.setOnPageChangeListener(this);dot1.setSelected(true);timer=new Timer();timertask=new TimerTask() {@Overridepublic void run() {Message msg=new Message();msg.what=1;handler.sendMessage(msg);}};timer.schedule(timertask,1000,1000);viewpager.setCurrentItem(0);}
这里我们定义了一个timer对象,通过timertask不停的往looper中发送消息。

 private Handler handler=new Handler(){    public void handleMessage(android.os.Message msg) {    if(msg.what==1)    {    if(currenItem==imageviews.size()-1)    {    viewpager.setCurrentItem(0);    }else{    viewpager.setCurrentItem(currenItem+1%imageviews.size());    }        }    };    };
当我们从消息队列中取到消息之后就开始修改界面。

@Overridepublic void onPageScrollStateChanged(int arg0) {}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageSelected(int arg0) {currenItem=arg0%imageviews.size();dots.get(oldPosition).setSelected(false);        dots.get(arg0).setSelected(true);        oldPosition=arg0;}
当然,我们还要实现OnPageChangeListener接口,改变小圆点的状态。主要的代码就是这些了,是不是很简单。

更多相关文章

  1. Android(安卓)界面类 Activity 和 Fragment 互相传递返回值解析
  2. 17 位谷歌 Android(安卓)开发专家是如何看待 Kotlin 的?
  3. 【Android】Handler机制源码解析
  4. android耗时任务_handler
  5. Android(安卓)判断应用程序获取通知栏权限是否开启,以及如何跳转
  6. Android界面绘制_canvas解析
  7. cocos2d jsb 打包 Android(安卓)APK
  8. 【凯子哥带你学Android】Android专用Log开源项目——KLog
  9. Android(安卓)Studio解决依赖文件冲突

随机推荐

  1. 一个提供Android各历史版本在线源码的网
  2. Android:This text field does not speci
  3. Android教程之Android(安卓)SDK1.5模拟器
  4. Android高手进阶教程(七)之----Android(
  5. android 定时器的实现 (转)
  6. ImageView的android:maxHeight,android:mi
  7. Android基础概念
  8. Android(安卓)众多的布局属性详解
  9. 系出名门Android(9) - 数据库支持(SQLite
  10. 转载——android 有用的知识