国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用Handler+view實(shí)現(xiàn)輪播圖效果

2019-11-09 16:05:38
字體:
供稿:網(wǎng)友

輪播圖在項(xiàng)目開發(fā)中也是屬于比較常見的UI實(shí)現(xiàn),一般會采用ViewPager來實(shí)現(xiàn),今天的博客我就不再隨大流了,給大家介紹個(gè)簡單的 實(shí)現(xiàn)方式,在此方式下你只需做簡單的配置即可。下面有通過代碼來帶著大家一起來實(shí)現(xiàn)下這個(gè)簡單的UI效果。

由于我介紹的這個(gè)輪播圖是利用給View或者繼承view的其他控件定期更換背景來實(shí)現(xiàn)的,所以在布局文件中,自然少不了這么一個(gè)View或者其子類(TextView,Imageview)等

布局

<?xml version="1.0" encoding="utf-8"?><FrameLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        >        <!--***********顯示輪播圖的View*************-->    <TextView        android:id="@+id/header_layout"        android:layout_width="match_parent"        android:layout_height="110dp"        android:background="@drawable/xycz_news_img_2"        />    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_refresh"        android:layout_width="match_parent"        android:layout_height="match_parent">    <ListView        android:id="@+id/listView"        android:dividerHeight="2dp"        android:padding="5dp"        android:layout_width="match_parent"        android:layout_height="match_parent"        />    </android.support.v4.widget.SwipeRefreshLayout>    </LinearLayout><include layout="@layout/load_animation"/></FrameLayout>

java代碼中自定義一個(gè)Runnable類繼承自Runnable,通過handler發(fā)送延時(shí)消息

 PRivate int imgsRes [] = {R.drawable.xycz_news_img_1,R.drawable.xycz_news_img_2,R.drawable.xycz_news_img_3}; //輪播的圖片資源    private int imgIndex;   //當(dāng)前imgRes的角標(biāo)索引    private MyRunable myRunable = new MyRunable();    class MyRunable implements Runnable {        @Override        public void run() {            imgIndex++;            imgIndex=imgIndex%3;            ((TextView) view.findViewById(R.id.header_layout)).setBackgroundResource(imgsRes[imgIndex]);            mHandler.postDelayed(myRunable,3000);        }    }在Java類的入口方法(onCreate、onCreateView)讓該Runnable對象跑起來

 mHandler.postDelayed(myRunable,3000);    //通知線程更新輪播圖

當(dāng)然還需要有個(gè)Handler對象。到此為止整個(gè)功能就簡單實(shí)現(xiàn)了,個(gè)人感覺在需求不是太苛刻時(shí),用此方法比ViewPager要簡單很多,可以省去很多繁瑣的代碼
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 仙桃市| 揭阳市| 贞丰县| 八宿县| 桑日县| 文水县| 左权县| 平远县| 大关县| 瑞丽市| 浦县| 田东县| 渭源县| 康马县| 鹤山市| 福鼎市| 台东县| 阿瓦提县| 治县。| 常州市| 上蔡县| 巴中市| 交口县| 兴隆县| 鹤山市| 周口市| 章丘市| 会昌县| 勐海县| 威信县| 永修县| 石楼县| 阿尔山市| 阿拉尔市| 民权县| 通州区| 金堂县| 临泽县| 区。| 廊坊市| 邢台县|