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

首頁 > 學院 > 開發設計 > 正文

使用Handler+view實現輪播圖效果

2019-11-09 14:53:50
字體:
來源:轉載
供稿:網友

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

由于我介紹的這個輪播圖是利用給View或者繼承view的其他控件定期更換背景來實現的,所以在布局文件中,自然少不了這么一個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代碼中自定義一個Runnable類繼承自Runnable,通過handler發送延時消息

 PRivate int imgsRes [] = {R.drawable.xycz_news_img_1,R.drawable.xycz_news_img_2,R.drawable.xycz_news_img_3}; //輪播的圖片資源    private int imgIndex;   //當前imgRes的角標索引    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);    //通知線程更新輪播圖

當然還需要有個Handler對象。到此為止整個功能就簡單實現了,個人感覺在需求不是太苛刻時,用此方法比ViewPager要簡單很多,可以省去很多繁瑣的代碼
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴海县| 东阿县| 仪陇县| 南和县| 凯里市| 安义县| 临漳县| 元阳县| 新巴尔虎右旗| 池州市| 杭锦后旗| 乌拉特后旗| 扶风县| 加查县| 武义县| 彰武县| 万宁市| 万源市| 建瓯市| 容城县| 察雅县| 凌源市| 潮安县| 乡宁县| 垦利县| 若尔盖县| 鞍山市| 鱼台县| 山西省| 江永县| 武夷山市| 乌兰察布市| 贵州省| 萨嘎县| 四会市| 洪湖市| 丹寨县| 陵川县| 托克逊县| 灵川县| 承德市|