ViewPager简单使用步骤

时间:2021-12-26 05:11:05

前言:感觉和listView使用一样。

1.xml文件中布局

2.Actvity中实例化ViewPager

3.自定义Adapter,这里具体实现稍有不同,也有些暂时不懂的地方

4.Adapter实例化,并填充到ViewPager

5.ViewPager事件监听

好了,直接上代码(本代码借鉴自xiaanming

①.xml布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>

</FrameLayout>

②.JAVA的Actvity

package com.example.viewpagerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
* ViewPager滑动Demo
* @ClassName: ViewPagerDemo1
* @Description: TODO(可以用来做引导页和风光展示页,本例未解决问题: 无限滚动;轮播(估计要开线程实现))
* @author FlyTMSky flytmsky@163.com
* @date 2015-9-2 下午4:18:38
*
*/
public class ViewPagerDemo1Actvity extends Activity implements OnPageChangeListener{

private ViewPager viewPager;

/**
* 装点点的ImageView数组
*/
private ImageView[] tips;

private ImageView imageView;

private int[] imgIdArray ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager_demo1);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.viewGroup);
viewPager = (ViewPager) findViewById(R.id.viewPager);

//载入图片资源ID
imgIdArray = new int[]{R.drawable.item01, R.drawable.item02, R.drawable.item03, R.drawable.item04,
R.drawable.item05,R.drawable.item06, R.drawable.item07, R.drawable.item08};


//将点点加入到ViewGroup中
tips = new ImageView[imgIdArray.length];
for(int i=0; i<tips.length; i++){
ImageView imageView = new ImageView(this);

//控制点间距离
LinearLayout.LayoutParams ll= new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ll.setMargins(5, 0, 5, 0);
imageView.setLayoutParams(ll);


tips[i] = imageView;
if(i == 0){
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
}else{
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}

linearLayout.addView(imageView);
}

//设置Adapter
viewPager.setAdapter(new MyAdapter(imgIdArray));
//设置监听,主要是设置点点的背景
viewPager.setOnPageChangeListener(this);
// //设置当前项,此处是最后一项,故只能右划(默认只能左划)
// viewPager.setCurrentItem(imgIdArray.length-1);

}

public class MyAdapter extends PagerAdapter{

private int[] ids;
public MyAdapter(int[] ids) {
this.ids = ids;
}
@Override
public int getCount() {
return ids.length;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//官方推荐
return arg0 == arg1;
}

@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager)container).removeView(imageView);

}

/**
*滑动和初始加载时,会加载2次(不懂)
*/
@Override
public Object instantiateItem(View container, int position) {
ImageView imageView = new ImageView(ViewPagerDemo1Actvity.this);
imageView.setBackgroundResource(imgIdArray[position]);
((ViewPager)container).addView(imageView, 0);
return imageView;
}

}

@Override
public void onPageScrollStateChanged(int arg0) {

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageSelected(int arg0) {
setImageBackground(arg0);
}

/**
* 设置选中的tip的背景
* @param selectItems
*/
private void setImageBackground(int selectItems){
for(int i=0; i<tips.length; i++){
if(i == selectItems){
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
}else{
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}
}
}

}
③.配置文件和图片就不用了吧


心得:

和其他控件一样使用,会省很多力。开始看别人的代码,往往会出现很难懂,难以接受。理解了,发现往往有两种情况。不是大神,就是菜鸟。这里的代码,看别人写时,确实感觉很别扭。感觉很不实用,主要原因是感觉质量不高,没有考虑到手机空间和效率等原因。这些地方,我也会注意的,欢迎指正。


下载地址:http://download.csdn.net/detail/zinjin_woxin/9074133