Android 開源組件 ----- Android LoopView無限自動輪轉控制項

來源:互聯網
上載者:User

標籤:

一、組件介紹

  App產品中資訊列表頭部都會有自動輪轉的廣告圖片,使用ViewPager可以實現但編碼比較麻煩,我們可以採用使用LoopView開原始檔控制來完成, LoopView是一個強大的輪轉大圖控制項,並且提供了許多配置方法足以滿足你的應用需求

二、環境配置

  如果您的項目使用 Gradle 構建, 只需要在您的build.gradle檔案添加下面一行到 dependencies :

  compile ‘com.kevin:loopview:1.0.4‘

三、如何使用

1、在layout.xml 中配置LoopView

  在Layout檔案添加<com.kevin.loopview.AdLoopView>代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:kevin="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <com.kevin.loopview.AdLoopView        android:id="@+id/adloop_act_adloopview"        android:layout_width="match_parent"        android:layout_height="192dp"        kevin:loop_interval="5000"        kevin:loop_dotMargin="5dp"        kevin:loop_autoLoop="true"        kevin:loop_dotSelector="@drawable/ad_dots_selector"        kevin:loop_layout="@layout/ad_loopview_layout">    </com.kevin.loopview.AdLoopView></RelativeLayout>

2、在Activity添加代碼:

public class AdLoopActivity extends Activity implements BaseLoopAdapter.OnItemClickListener{    AdLoopView mLoopView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_adloopview);        initViews();        initEvents();    }    private void initViews() {        mLoopView = (AdLoopView)                         this.findViewById(R.id.adloop_act_adloopview);        initRotateView();    }    /**     * 初始化LoopView     */    private void initRotateView() {        // 設定自訂布局//        mLoopView.setLoopLayout(R.layout.ad_loopview_layout);        // 設定資料          String json = LocalFileUtils.getStringFormAsset(this,                                 "loopview_date.json");        LoopData loopData = JsonTool.toBean(json, LoopData.class);        if(null != loopData) {            mLoopView.refreshData(loopData);        }        // 設定頁面切換過度事件        mLoopView.setScrollDuration(2000);        // 設定頁面切換時間間隔        mLoopView.setInterval(3000);    }    /**     * 初始化事件*/    private void initEvents() {        mLoopView.setOnClickListener(this);    }    @Override    public void onItemClick(PagerAdapter parent, View view, int position, int realPosition) {        LoopData loopData = mLoopView.getLoopData();        String url = loopData.items.get(position).link;        Intent intent = new Intent();        intent.setData(Uri.parse(url));        intent.setAction(Intent.ACTION_VIEW);        startActivity(intent);    }    @Override    protected void onDestroy() {        super.onDestroy();    }}         

3、所涉及LocalFileUtils的主要方法

public class LocalFileUtils {        /**     * 擷取Asset下常值內容     */public static String getStringFormAsset(Context context, String str) {        BufferedReader in = null;        try {      in = new BufferedReader(new InputStreamReader(context.getAssets().open(str)));            String line;            StringBuilder buffer = new StringBuilder();            while ((line = in.readLine()) != null) {                buffer.append(line).append(‘\n‘);            }            return buffer.toString();        } catch (IOException e) {            e.printStackTrace();            return "";        } finally {            if (in != null) {                try {                    in.close();                    in = null;                } catch (IOException e) {                    e.printStackTrace();                }            }        }}}
四、LoopView主要方法
// 設定ViewPager頁面切換時間mLoopView.setScrollDuration(1000);// 設定輪轉時間間隔mLoopView.setInterval(3000);// 以集合的方式初始化資料mLoopView.setLoopViewPager(List<Map<String, String>> data);// 以JSON的方式初始化資料mLoopView.setLoopViewPager(String jsonData);// 以資料實體的方式初始化資料mLoopView.setLoopViewPager(LoopData rotateData);// 以集合的方式重新整理資料mLoopView.refreshData(final List<Map<String, String>> data);// 以資料實體的方式重新整理資料mLoopView.refreshData(LoopData loopData);// 以JSON的方式重新整理資料mLoopView.refreshData(String jsonData);// 擷取配置的輪轉大圖資料mLoopView.getLoopData();// 開始自動輪轉mLoopView.startAutoLoop();// 在指定時間延遲後自動輪轉mLoopView.startAutoLoop(long delayTimeInMills);// 停止自動輪轉mLoopView.stopAutoLoop();// 設定自訂布局mLoopView.setLoopLayout(int layoutResId);

 

傑瑞教育
出處:http://www.cnblogs.com/jerehedu/ 
著作權聲明:本文著作權歸煙台傑瑞教育科技有限公司和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。
技術諮詢: 

Android 開源組件 ----- Android LoopView無限自動輪轉控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.