ViewPager(可滑動頁面視圖)簡單使用


功能:滑動圖片android


效果圖示例:緩存






一、在res/drawable導入須要的圖片ide


二、res/layout界面 activity_main.xml 佈局佈局


代碼this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"spa

    xmlns:tools="http://schemas.android.com/tools"xml

    android:layout_width="match_parent"對象

    android:layout_height="match_parent"繼承

    tools:context="${relativePackage}.${activityClass}" >接口


    <TextView

        android:id="@+id/pageNo"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="" 

        android:background="#000"

        android:textColor="#fff"

        android:textSize="30sp"/>

    

     <android.support.v4.view.ViewPager

        android:id="@+id/viewPager"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@+id/pageNo" />

    


</RelativeLayout>




======================


三、MainActivity 類


代碼

//要實現接口OnPagerChangeListener

public class MainActivity extends Activity implements OnPageChangeListener{


private TextView text_pageNo;

private ViewPager viewpager;

private MyAdapter adapter;

private List<View> list = new ArrayList<View>();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

this.text_pageNo = (TextView) this.findViewById(R.id.pageNo);

this.viewpager = (ViewPager) this.findViewById(R.id.viewPager);

//數據初始化 -- 用array方法

TypedArray array = getResources().obtainTypedArray(R.array.imgarray);

for(int i = 0;i<array.length();i++){

ImageView imageView = new ImageView(this);

imageView.setImageDrawable(array.getDrawable(i));

list.add(imageView);

}

adapter = new MyAdapter(list);

viewpager.setAdapter(adapter);

text_pageNo.setText(1 + "/" +  array.length()); 

viewpager.setOnPageChangeListener(this);

}

//繼承PagerAdapter類的適配器 

class MyAdapter extends PagerAdapter{

//圖片數據

private int[] imgs = {R.drawable.car0,R.drawable.car1,

R.drawable.car2,R.drawable.car3,

R.drawable.car4,R.drawable.car5,

R.drawable.car6,R.drawable.car7,};

private List<View> list;

public MyAdapter(List<View> list) {

this.list = list;

}


@Override//數據大小

public int getCount() {

return this.list.size();

}

@Override//判斷視圖是不是對象

public boolean isViewFromObject(View view, Object object) {

return view == object;

}

@Override//實例化視圖 -- 把某一個view放到當前的ViewPager位置中

public Object instantiateItem(ViewGroup container, int position) {

//方法1

/*ImageView imageView = new ImageView(container.getContext());

//把當前圖片設置到動態建立的ImageView佈局控件上

imageView.setImageResource(imgs[position]);

//設置圖片數據到container

container.addView(imageView);*/

//方法2

container.addView(this.list.get(position));

return this.list.get(position);

}

@Override//銷燬滑出去的view 滑出去的view和當前的位置相差1(內部有緩存功能)

public void destroyItem(ViewGroup container, int position, Object object) {

//super.destroyItem(container, position, object);

//方法2必定要銷燬緩存的視圖 否則往回滑動會蹦

container.removeView(this.list.get(position));

}

}


//------------實現接口OnPageChangeListener監聽要重寫的方法-----------

@Override//滾動時狀態的改變

public void onPageScrollStateChanged(int state) {

switch(state){

case ViewPager.SCROLL_STATE_DRAGGING://拖動狀態

break;

case ViewPager.SCROLL_STATE_IDLE://中止狀態

break;

case ViewPager.SCROLL_STATE_SETTLING://中止滾動狀態

break;

}

}


@Override//拖動時

public void onPageScrolled(int position, float arg1, int arg2) {

}


@Override//選中時

public void onPageSelected(int position) {

text_pageNo.setText((position + 1) + "/" +  adapter.getCount());

}

}

相關文章
相關標籤/搜索