這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)ViewPage輪播圖效果,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司主要從事網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)鄉(xiāng)寧,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
首先當(dāng)然是新建一個項(xiàng)目代碼如下:
activity_main.xml文件:
MainActivity
package com.yzp.viewpagedemo; import android.content.Context; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ViewPager ViewPage_Detail; private LinearLayout point_detail; private Context context; private View view; //輪播圖圖片資源 private final int[] viewpage_images = {R.mipmap.two, R.mipmap.three, R.mipmap.four, R.mipmap.fifve}; private ArrayListviewpage_imageList; //判斷是否自動滾動ViewPage private boolean isRunning = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Instantiation(); } public void Instantiation() { ViewPage_Detail = (ViewPager)findViewById(R.id.ViewPage_Detail); point_detail = (LinearLayout)findViewById(R.id.point_detail); //初始化圖片資源 viewpage_imageList = new ArrayList (); for (int i : viewpage_images) { // 初始化圖片資源 ImageView imageView = new ImageView(this); imageView.setBackgroundResource(i); viewpage_imageList.add(imageView); // 添加指示小點(diǎn) ImageView point = new ImageView(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15); params.rightMargin = 10; params.bottomMargin = 15; point.setLayoutParams(params); point.setBackgroundResource(R.mipmap.light_blue_point); if (i == R.mipmap.two) { //默認(rèn)聚焦在第一張 point.setBackgroundResource(R.mipmap.blue_point); point.setEnabled(true); } else { point.setEnabled(false); } point_detail.addView(point); } //首頁輪播 CarouselShow carouselShow = new CarouselShow(context, viewpage_imageList); carouselShow.CarouselShow_Info_Detail(this); handler.sendEmptyMessageDelayed(0, 3000); } private Handler handler = new Handler() { public void handleMessage(Message msg) { // 執(zhí)行滑動到下一個頁面 ViewPage_Detail.setCurrentItem(ViewPage_Detail.getCurrentItem() + 1); if (isRunning) { // 在發(fā)一個handler延時 handler.sendEmptyMessageDelayed(0, 3000); } } }; @Override public void onDestroy() { // 停止?jié)L動 isRunning = false; super.onDestroy(); } }
CarouselShow類(輪播功能實(shí)現(xiàn)):
package com.yzp.viewpagedemo; import android.content.Context; import android.support.v4.view.ViewPager; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; /** * 輪播展示公共類 * 作者: yzp on 2016-09-07. * E-mail: 15111424807@163.com * QQ: 486492302 */ public class CarouselShow { private final Context context; private LinearLayout point_group; private ArrayListviewpage_imageList; protected int lastPosition = 0; private ViewPager ViewPage_Detail; private LinearLayout point_detail; public CarouselShow(Context context, ArrayList viewpage_imageList) { this.context = context; this.viewpage_imageList = viewpage_imageList; } /** * 當(dāng)需要多個輪播功能的時候 建立一個類來調(diào)用 并實(shí)現(xiàn)此方法 */ public void CarouselShow_Info_Detail(MainActivity view){ ViewPage_Detail = (ViewPager)view.findViewById(R.id.ViewPage_Detail); point_detail = (LinearLayout) view.findViewById(R.id.point_detail); ViewPage_Detail.setAdapter(new TeaNetPageAdapter(viewpage_imageList)); // 設(shè)置當(dāng)前viewPager的位置 ViewPage_Detail.setCurrentItem(Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % viewpage_imageList.size())); ViewPage_Detail.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { // 頁面切換后調(diào)用, position是新的頁面位置 // 實(shí)現(xiàn)無限制循環(huán)播放 position %= viewpage_imageList.size(); // 把當(dāng)前點(diǎn)設(shè)置為true,將上一個點(diǎn)設(shè)為false;并設(shè)置point_group圖標(biāo) point_detail.getChildAt(position).setEnabled(true); point_detail.getChildAt(position).setBackgroundResource(R.mipmap.blue_point);//設(shè)置聚焦時的圖標(biāo)樣式 point_detail.getChildAt(lastPosition).setEnabled(false); point_detail.getChildAt(lastPosition).setBackgroundResource(R.mipmap.light_blue_point);//上一張恢復(fù)原有圖標(biāo) lastPosition = position; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); } }
TeaNetPageAdapter(適配器):
package com.yzp.viewpagedemo; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import java.util.ArrayList; /** * 作者: yzp on 2016-09-07. * E-mail: 15111424807@163.com * QQ: 486492302 */ public class TeaNetPageAdapter extends PagerAdapter { private ArrayListviewpage_imageList; public TeaNetPageAdapter(ArrayList viewpage_imageList) { this.viewpage_imageList = viewpage_imageList; } // 需要實(shí)現(xiàn)以下四個方法 @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { // 判斷view和Object對應(yīng)是否有關(guān)聯(lián)關(guān)系 if (view == object) { return true; } else { return false; } } @Override public Object instantiateItem(ViewGroup container, int position) { // 獲得相應(yīng)位置上的view; container view的容器,其實(shí)就是viewpage自身, // position: viewpager上的位置 // 給container添加內(nèi)容 container.addView(viewpage_imageList.get(position % viewpage_imageList.size())); return viewpage_imageList.get(position % viewpage_imageList.size()); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // 銷毀對應(yīng)位置上的Object // super.destroyItem(container, position, object); container.removeView((View) object); object = null; } }
簡單的幾個類,首頁輪播的功能就實(shí)現(xiàn)了,不過主類的圖片資源就沒有貼出來了,到時候可以自己下載幾張圖片,貼到mipmap文件夾下就OK了。
效果圖展示:
關(guān)于“Android如何實(shí)現(xiàn)ViewPage輪播圖效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。