国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android viewpager中動(dòng)態(tài)添加view并實(shí)現(xiàn)偽無(wú)限循環(huán)的方法

2020-04-11 10:59:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 炉霍县| 晋城| 罗山县| 栾城县| 通辽市| 白水县| 滦平县| 肇州县| 河源市| 兰考县| 高陵县| 水富县| 仁布县| 海丰县| 湖州市| 镇赉县| 买车| 吴堡县| 上林县| 宁安市| 平江县| 武义县| 湘潭县| 建宁县| 洞口县| 会东县| 永登县| 丰顺县| 阜康市| 丹江口市| 桑日县| 丁青县| 闽清县| 兴和县| 北碚区| 新乐市| 鸡泽县| 青浦区| 孝感市| 炉霍县| 邵阳市|