Android viewpager中動(dòng)態(tài)添加view并實(shí)現(xiàn)偽無限循環(huán)的方法
本文實(shí)例講述了Android viewpager中動(dòng)態(tài)添加view并實(shí)現(xiàn)偽無限循環(huán)的方法。分享給大家供大家參考,具體如下:
viewpager的使用,大家都熟悉,它可以實(shí)現(xiàn)頁(yè)面之間左右滑動(dòng)的切換,這里有一個(gè)需求,就是viewpager里面加載的頁(yè)數(shù)不是確定的,而是根據(jù)數(shù)據(jù)的多少來確定的。常見的應(yīng)用就是在一個(gè)新聞的詳細(xì)頁(yè)面中,顯示與此新聞?dòng)嘘P(guān)的圖片。
下面我們來看一下代碼:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/list_pager" android:layout_width="fill_parent" android:layout_height="150dp" /> </RelativeLayout>
fragment_page.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#ffff44"> <TextView android:id="@+id/txt_num" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="1" android:textSize="22dp" android:gravity="center"/> </LinearLayout>
viewpageAdapter.java
import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * Created by cg on 2015/10/28. */ public class viewpageAdapter extends PagerAdapter { private List<View> list_view; public viewpageAdapter(List<View> list_view) { this.list_view = list_view; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list_view.get(position % list_view.size())); return list_view.get(position % list_view.size()); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list_view.get(position % list_view.size())); } }
MainActivity.java
import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * 動(dòng)態(tài)在viewpager中添加view,并實(shí)現(xiàn)無限循環(huán) */ public class MainActivity extends AppCompatActivity { private ViewPager list_pager; private List<View> list_view; private viewpageAdapter adpter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_pager = (ViewPager)findViewById(R.id.list_pager); list_view = new ArrayList<>(); //這里只設(shè)置了4.因?yàn)樵趯?shí)現(xiàn)應(yīng)用中,我們?cè)陧?yè)面加載的時(shí)候,你會(huì)根據(jù)數(shù)據(jù)的多少,而知道這個(gè)頁(yè)面的數(shù)量 //一般情況下,我們會(huì)根據(jù)list<>或是string[]這樣的數(shù)組的數(shù)量來判斷要有多少頁(yè) for(int i=0;i<4;i++) { View view = LayoutInflater.from(this).inflate(R.layout.fragment_page,null); TextView txt_num = (TextView)view.findViewById(R.id.txt_num); txt_num.setText(i + ""); list_view.add(view); } adpter = new viewpageAdapter(list_view); list_pager.setAdapter(adpter); // 剛開始的時(shí)候 吧當(dāng)前頁(yè)面是先到最大值的一半 為了循環(huán)滑動(dòng) int currentItem = Integer.MAX_VALUE / 2; // 讓第一個(gè)當(dāng)前頁(yè)是 0 //currentItem = currentItem - ((Integer.MAX_VALUE / 2) % 4); list_pager.setCurrentItem(currentItem); } }
效果圖:
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android 使用viewpager實(shí)現(xiàn)無限循環(huán)(定時(shí)+手動(dòng))
- Android ViewPager無限循環(huán)實(shí)現(xiàn)底部小圓點(diǎn)動(dòng)態(tài)滑動(dòng)
- Android ViewPager無限循環(huán)滑動(dòng)并可自動(dòng)滾動(dòng)完整實(shí)例
- Android無限循環(huán)RecyclerView的完美實(shí)現(xiàn)方案
- Android ViewPager實(shí)現(xiàn)無限循環(huán)效果
- Android實(shí)現(xiàn)ViewPager無限循環(huán)效果(一)
- Android實(shí)現(xiàn)帶指示點(diǎn)的自動(dòng)輪播無限循環(huán)效果
- Android實(shí)現(xiàn)基于ViewPager的無限循環(huán)自動(dòng)播放帶指示器的輪播圖CarouselFigureView控件
- Android實(shí)戰(zhàn)打飛機(jī)游戲之無限循環(huán)的背景圖(2)
- Android TV 3D卡片無限循環(huán)效果
相關(guān)文章
Android studio實(shí)現(xiàn)PopupWindow彈出框效果
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)PopupWindow彈出框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Android進(jìn)程運(yùn)行中權(quán)限被收回導(dǎo)致關(guān)閉的問題解決
在Android開發(fā)中我們可能會(huì)遇到這樣的問題,進(jìn)程還在運(yùn)行著某些權(quán)限卻被收回了,這就導(dǎo)致進(jìn)程崩潰被迫關(guān)閉,本篇文章將帶你了解這個(gè)問題的發(fā)生與解決方法2021-10-10Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字
本文主要介紹了Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04Android 中Notification彈出通知實(shí)現(xiàn)代碼
NotificationManager 是狀態(tài)欄通知的管理類,負(fù)責(zé)發(fā)通知、清除通知等操作。接下來通過本文給大家介紹Android 中Notification彈出通知實(shí)現(xiàn)代碼,需要的的朋友參考下吧2017-08-08Android使用Kotlin API實(shí)踐WorkManager
這篇文章主要介紹了Android使用Kotlin API實(shí)踐WorkManager的步驟,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android 單例模式的四種實(shí)現(xiàn)方式
單例模式作為設(shè)計(jì)模式之一,使用場(chǎng)景非常多。本文講述了Android實(shí)現(xiàn)單例模式的幾種方式2021-05-05android中圖片的三級(jí)緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))
實(shí)現(xiàn)圖片緩存也不難,需要有相應(yīng)的cache策略。這里我采用 內(nèi)存-文件-網(wǎng)絡(luò) 三層cache機(jī)制,其中內(nèi)存緩存包括強(qiáng)引用緩存和軟引用緩存(SoftReference),其實(shí)網(wǎng)絡(luò)不算cache,這里姑且也把它劃到緩存的層次結(jié)構(gòu)中2013-06-06Android中使用AsyncTask實(shí)現(xiàn)下載文件動(dòng)態(tài)更新進(jìn)度條功能
這篇文章主要介紹了AsyncTask用法解析-下載文件動(dòng)態(tài)更新進(jìn)度條,需要的朋友可以參考下2017-08-08