使用AndroidScreenSlidePager開源庫

來源:互聯網
上載者:User

標籤:

一、

https://github.com/LyndonChin/AndroidScreenSlidePager

點擊右側的Download ZIp按鈕進行下載。然後解壓縮到本地。

二、使用方法

1、複製AndroidScreenSlidePager-master\SlidePageIndicator\src\main\java\com\liangfeizc\slidepageindicator目錄下的PageIndicator.java檔案到項目中任意一個包中。以及AndroidScreenSlidePager-master\SlidePageIndicator\src\main\res目錄中的drawable檔案夾中的三個檔案複製到項目中相應的位置

然後將AndroidScreenSlidePager-master\SlidePageIndicator\src\main\res目錄下的values檔案夾中的styles裡面的代碼複製到項目的styles.xml檔案中。

2、我使用的布局架構是ViewPager+fragment,然後在fragment中使用volley(NetworkImageView)展現圖片。

關鍵代碼如下:

private static final String[] IMAGES = new String[] {        "http://img1.cache.netease.com/catchpic/B/B2/B2F274C1CCD5A89133261E6252A0C8E9.jpg",        "http://img5.duitang.com/uploads/item/201408/09/20140809204759_CwtQN.jpeg",        "http://img5q.duitang.com/uploads/item/201204/06/20120406151343_HefME.jpeg",        "http://images.qianlong.com/mmsource/imghylanda/201108/25/21/7131919880274907937.jpg",        "http://imgsrc.baidu.com/forum/pic/item/78370e46f91582056a63e576.jpg"};

布局檔案:

CirclePageIndicator and ViewPager shoud be used as child views of a Framelayout. But here we used merge instead, because the root view in any activity is a FrameLayout. You can use android:gravity to position the CirclePageIndicator and use app:indicator_spacing to adjust the spacing between two adjencent circle indicators.

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent" >        <!-- viewPager:圖片展現 -->    <android.support.v4.view.ViewPager        android:id="@+id/pager"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <!-- 指標圓點 -->    <com.why.screenslidepagerdemo.custom.PageIndicator        android:id="@+id/indicator"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginBottom="20dp"        android:layout_marginLeft="20dp"        android:layout_marginStart="20dp"        android:gravity="bottom|center_horizontal"        app:indicator_spacing="5dp"        app:indicator_type="fraction" />        </merge>
activity_slidepager


Java代碼:

private ViewPager pager;//圖片ViewPagerprivate SlidePagerAdapter adapter;//ViewPager適配器    private PageIndicator indicator;//指標private String[] picsArray;//圖片URL地址的數組//初始化控制項    private void initView(){        pager = (ViewPager) findViewById(R.id.pager);        indicator = (PageIndicator) findViewById(R.id.indicator);    }        //初始化適配器    private void initAdapter(){        adapter = new SlidePagerAdapter(getSupportFragmentManager(),Arrays.asList(picsArray));        pager.setAdapter(adapter);    }            //初始化監聽事件    private void initEvent(){        indicator.setViewPager(pager);//這個是關聯指標類的關鍵代碼        indicator.setIndicatorType(PageIndicator.IndicatorType.CIRCLE);    }

 

右側的菜單代碼:

@Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.slidepager, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        int id = item.getItemId();        if (id == R.id.action_indicator_circle) {            indicator.setIndicatorType(PageIndicator.IndicatorType.CIRCLE);        }else if(id == R.id.action_indicator_fraction){            indicator.setIndicatorType(PageIndicator.IndicatorType.FRACTION);        }        return super.onOptionsItemSelected(item);    }

 


:

 

 

使用AndroidScreenSlidePager開源庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.