类似于BaseAdapter,其中instantiateItem方法用来得到每个View,destroyItem用以控制当某个View不需要的时候的回收处理。isViewFromObject用来实现判断View和Object是否为同一个View。
先看一下效果图:
第一步:首先是在布局文件里添加viewPager布局。代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" > </android.support.v4.view.ViewPager> <Button android:id="@+id/deleteBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:text="删除" /> </RelativeLayout>第二步:创建item布局用于填充在ViewPager里,可以自定义也可以加载写好的xml布局文件。代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="@drawable/item_bg" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/view_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:singleLine="true" android:textSize="20sp" /> <ImageView android:id="@+id/view_image" android:layout_width="240dp" android:layout_height="220dp" android:layout_gravity="center_horizontal" android:layout_marginLeft="8dp" android:layout_marginTop="2dp" /> <TextView android:id="@+id/view_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="20dp" android:layout_marginTop="2dp" android:ellipsize="end" android:maxLines="2" android:text="很不错哦!嘻嘻,嘿嘿,O(∩_∩)O哈哈哈~……" android:textSize="15sp" /> </LinearLayout> </LinearLayout>第三步:然后就是Activity了,主要写了左右滑动切换页面。代码如下:
package net.loonggg.viewpager; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private ViewPager viewPager; private Button deleteBtn; private List<View> listViews = null; private int[] imgs = { R.drawable.img0, R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, }; private int index = 0; private ViewPagerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewpager); deleteBtn = (Button) findViewById(R.id.deleteBtn); listViews = new ArrayList<View>(); for (int i = 0; i < imgs.length; i++) { View view = LayoutInflater.from(getApplicationContext()).inflate( R.layout.viewpager_item, null); TextView title = (TextView) view.findViewById(R.id.view_title); title.setText("头像"); ImageView iv = (ImageView) view.findViewById(R.id.view_image); iv.setBackgroundResource(imgs[i]); listViews.add(view); } adapter = new ViewPagerAdapter(listViews); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new PageChangeListener()); deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (listViews.size() > 0) { listViews.remove(index); adapter.notifyDataSetChanged(); } } }); } private class PageChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int arg0) { Toast.makeText(getApplicationContext(), arg0 + "", 0).show(); index = arg0; } } }第四步:是有关ViewPager的适配器的重写。代码如下:
package net.loonggg.viewpager; import java.util.List; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; public class ViewPagerAdapter extends PagerAdapter { private List<View> list; public ViewPagerAdapter(List<View> list) { this.list = list; } @Override public int getCount() { if (list != null && list.size() > 0) { return list.size(); } else { return 0; } } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public int getItemPosition(Object object) { return POSITION_NONE; } }到这里有关ViewPager的使用就讲完了, 其实更重要的是想讲:如何解决Android下ViewPager和PagerAdapter中调用notifyDataSetChanged失效的问题 。
具体讲解如下:
Google在Android 3.0SDK中推出的ViewPager控件很大程度上满足了开发者开发页面左右移动切换的功能,使用非常方便。但是使用中发现,在删除或者修改数据的时候,PagerAdapter无法像BaseAdapter那样仅通过notifyDataSetChanged方法通知刷新View。
最基本的方法:
针对于child view比较简单的情况(例如仅有TextView、ImageView等,没有ListView等展示数据的情况),可以在自己的Adapter中加入代码:
@Override public int getItemPosition(Object object) { return POSITION_NONE; }这样既可达到一般情况下要求的效果。