国产探花免费观看_亚洲丰满少妇自慰呻吟_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要簡單很多,可以省去很多繁瑣的代碼
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霸州市| 浦东新区| 平潭县| 阳高县| 徐汇区| 新巴尔虎右旗| 河南省| 鹰潭市| 大足县| 孝义市| 吉林省| 孙吴县| 东源县| 东兰县| 蓬溪县| 克山县| 土默特右旗| 莱阳市| 忻城县| 乌兰浩特市| 英吉沙县| 游戏| 巴塘县| 中牟县| 惠来县| 喀什市| 金湖县| 万安县| 新龙县| 临江市| 永春县| 浠水县| 兰州市| 南木林县| 上饶市| 伊春市| 庆云县| 孟连| 海阳市| 桂东县| 西贡区|