本文實(shí)例講述了Android viewpager中動(dòng)態(tài)添加view并實(shí)現(xiàn)偽無(wú)限循環(huán)的方法。分享給大家供大家參考,具體如下:
viewpager的使用,大家都熟悉,它可以實(shí)現(xiàn)頁(yè)面之間左右滑動(dòng)的切換,這里有一個(gè)需求,就是viewpager里面加載的頁(yè)數(shù)不是確定的,而是根據(jù)數(shù)據(jù)的多少來(lái)確定的。常見(jiàn)的應(yīng)用就是在一個(gè)新聞的詳細(xì)頁(yè)面中,顯示與此新聞?dòng)嘘P(guān)的圖片。
下面我們來(lái)看一下代碼:
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)無(wú)限循環(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ù)量來(lái)判斷要有多少頁(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); // 剛開(kāi)始的時(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ì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選