android ViewPager 实现导航页面和广告播放

很多应用,第一次进入app,都会有几个导航的页面,教用户怎么使用app。用户,滑动页面,可以浏览下一个页面,到最后一个页面时候,有一个按钮,用户点击按钮后,调到住页面,还有在首页顶端,经常有一个播放广告的画面,在一定时间,跳出不同的图片之类的。

这些功能可以用ViewPager来实现,比较方便。

 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
package com.hck.viewpagedemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class AdsActivity extends Activity{
private ViewPager viewPager; //ViewPager对象
private ArrayList<View> views; //保存view的数组,用来显示在页卡
private View view; //每个页面的view
private int postion;
private MyThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ads);
viewPager = (ViewPager) findViewById(R.id.vPager);
views = new ArrayList<View>();
thread=new MyThread();
initDate();
ViewPageAdpter adpter=new ViewPageAdpter();
viewPager.setAdapter(adpter);
handler.post(thread);
}
private void initDate() { //初始化3个view页面
view=getLayoutInflater().inflate(R.layout.vpage_item1,null);
views.add(view);
view=getLayoutInflater().inflate(R.layout.vpage_item2,null);
views.add(view);
view=getLayoutInflater().inflate(R.layout.vpage_view3,null);
views.add(view);
}
Handler handler=new Handler()
{
public void handleMessage(android.os.Message msg) {
viewPager.setCurrentItem(postion);
postion++;
if (postion>=3) {
postion=0;
}
};
};
class MyThread implements Runnable
{
@Override
public void run() {
handler.sendEmptyMessage(0);
handler.postDelayed(thread, 3000);
}
}
class ViewPageAdpter extends PagerAdapter //装载view的适配器
{
@Override
public void destroyItem(View arg0, int arg1, Object arg2) { //销毁view
((ViewPager) arg0).removeView(views.get(arg1));
}
@Override
public int getCount() { //view数量
return views.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) { //返回相应的view
((ViewPager) arg0).addView(views.get(arg1), 0);
return views.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0 == arg1);
}
}
}
 来自CODE的代码片
snippet_file_0.txt

 

 

第一个页面

向左滑动进入第2个页面

 

进入住界面后,顶端有个不定时跳动切换的viewPager,下面显示类容

 

 

 

 

 

 

代码:导航页面代码

 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
package com.hck.viewpagedemo;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends Activity {
private ViewPager viewPager; //ViewPager对象
private ArrayList<View> views; //保存view的数组,用来显示在页卡
private View view; //每个页面的view
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.vPager);
views = new ArrayList<View>();
initDate();
ViewPageAdpter adpter=new ViewPageAdpter();
viewPager.setAdapter(adpter);
}
private void initDate() { //初始化3个view页面
view=getLayoutInflater().inflate(R.layout.vpage_item1,null);
views.add(view);
view=getLayoutInflater().inflate(R.layout.vpage_item2,null);
views.add(view);
view=getLayoutInflater().inflate(R.layout.vpage_view3,null);
views.add(view);
}
class ViewPageAdpter extends PagerAdapter //装载view的适配器
{
@Override
public void destroyItem(View arg0, int arg1, Object arg2) { //销毁view
((ViewPager) arg0).removeView(views.get(arg1));
}
@Override
public int getCount() { //view数量
return views.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) { //返回相应的view
((ViewPager) arg0).addView(views.get(arg1), 0);
if (arg1==2) {
startButton=(Button) arg0.findViewById(R.id.start);
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,AdsActivity.class));
MainActivity.this.finish();
}
});
}
return views.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0 == arg1);
}
}
}
 来自CODE的代码片
snippet_file_0.txt

 

 

进入首页代码:

 

 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
package com.hck.viewpagedemo;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class AdsActivity extends Activity{
private ViewPager viewPager; //ViewPager对象
private ArrayList<View> views; //保存view的数组,用来显示在页卡
private View view; //每个页面的view
private int postion;
private MyThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ads);
viewPager = (ViewPager) findViewById(R.id.vPager);
views = new ArrayList<View>();
thread=new MyThread();
initDate();
ViewPageAdpter adpter=new ViewPageAdpter();
viewPager.setAdapter(adpter);
handler.post(thread);
}
private void initDate() { //初始化3个view页面
view=getLayoutInflater().inflate(R.layout.vpage_item1,null);
views.add(view);
view=getLayoutInflater().inflate(R.layout.vpage_item2,null);
views.add(view);
view=getLayoutInflater().inflate(R.layout.vpage_view3,null);
views.add(view);
}
Handler handler=new Handler()
{
public void handleMessage(android.os.Message msg) {
viewPager.setCurrentItem(postion);
postion++;
if (postion>=3) {
postion=0;
}
};
};
class MyThread implements Runnable
{
@Override
public void run() {
handler.sendEmptyMessage(0);
handler.postDelayed(thread, 3000);
}
}
class ViewPageAdpter extends PagerAdapter //装载view的适配器
{
@Override
public void destroyItem(View arg0, int arg1, Object arg2) { //销毁view
((ViewPager) arg0).removeView(views.get(arg1));
}
@Override
public int getCount() { //view数量
return views.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) { //返回相应的view
((ViewPager) arg0).addView(views.get(arg1), 0);
return views.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return (arg0 == arg1);
}
}
}
 来自CODE的代码片
snippet_file_0.txt
代码和导航页面的基本一样,多了一个handler+线程,不定时改变viewPager显示的页面

标签