1 package com.example.viewpager; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.support.v4.view.PagerAdapter; 7 import android.support.v4.view.ViewPager; 8 import android.support.v4.view.ViewPager.OnPageChangeListener; 9 import android.support.v7.app.ActionBarActivity; 10 import android.app.Activity; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.view.LayoutInflater; 14 import android.view.Menu; 15 import android.view.MenuItem; 16 import android.view.View; 17 import android.view.View.OnClickListener; 18 import android.view.ViewGroup; 19 import android.widget.ImageView; 20 import android.widget.LinearLayout; 21 import android.widget.LinearLayout.LayoutParams; 22 import android.widget.TextView; 23 24 public class MainActivity extends Activity implements OnPageChangeListener{ 25 26 ViewPager v;// viewpager 控件 27 List<View> img;// drawable (数据)包含的图片 28 LinearLayout liner; 29 TextView text; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 v = (ViewPager) findViewById(R.id.view); 36 text = (TextView) findViewById(R.id.guideTv); 37 liner = (LinearLayout) findViewById(R.id.li); 38 img = new ArrayList<View>(); 39 // 得到三张图片 40 LayoutInflater in = getLayoutInflater(); 41 View v1 = in.inflate(R.layout.one, null); 42 v1.setBackgroundResource(R.drawable.aa1); 43 View v2 = in.inflate(R.layout.one, null); 44 v2.setBackgroundResource(R.drawable.aa2); 45 View v3 = in.inflate(R.layout.one, null); 46 v3.setBackgroundResource(R.drawable.aa3); 47 // 将view中的图片添加到数组里 48 img.add(v3); 49 img.add(v2); 50 img.add(v1); 51 // 得到适配器 52 myAdapter adapter = new myAdapter(); 53 // 使适配器与viewpager相连 54 v.setAdapter(adapter); 55 v.setOnPageChangeListener(this); 56 addPoints(); 57 text.setOnClickListener(new OnClickListener() { 58 59 @Override 60 public void onClick(View v) { 61 62 Intent intent=new Intent(MainActivity.this,TwoActivity.class); 63 startActivity(intent); 64 65 } 66 }); 67 } 68 69 class myAdapter extends PagerAdapter { 70 71 @Override 72 public int getCount() { 73 // TODO Auto-generated method stub 74 return img.size();// 子页的数量 75 } 76 77 @Override 78 public boolean isViewFromObject(View arg0, Object arg1) { 79 // TODO Auto-generated method stub 80 return arg0 == arg1;// 这个地方有点模糊建议看源代码 令 view对象与object对象相等 81 } 82 83 @Override 84 public void destroyItem(ViewGroup container, int position, Object object) { 85 container.removeView(img.get(position));// 动态的移除view 86 } 87 88 @Override 89 public Object instantiateItem(ViewGroup container, int position) { 90 container.addView(img.get(position)); 91 return img.get(position);// 动态的添加view 92 } 93 94 } 95 96 // 添加小圆点 97 private void addPoints() { 98 // 1.根据图片多少,添加多少小圆点 99 for (int i = 0; i < img.size(); i++) { 100 LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( 101 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 102 if (i < 1) { 103 param.setMargins(0, 0, 0, 0); 104 } else { 105 param.setMargins(10, 0, 0, 0); 106 107 } 108 ImageView iv = new ImageView(this); 109 iv.setLayoutParams(param); 110 iv.setBackgroundResource(R.drawable.page_indicator_unfocused); 111 liner.addView(iv); 112 } 113 liner.getChildAt(0).setBackgroundResource( 114 R.drawable.page_indicator_focused); 115 116 } 117 118 // 判断选中小圆点 119 private void monitorPoint(int Position) { 120 for (int i = 0; i < img.size(); i++) { 121 if (i == Position) { 122 liner.getChildAt(Position).setBackgroundResource( 123 R.drawable.page_indicator_focused); 124 125 } else { 126 liner.getChildAt(i).setBackgroundResource( 127 R.drawable.page_indicator_unfocused); 128 129 } 130 131 } 132 // 当滑动到最后一个添加按钮点击进去 133 if (Position == img.size() - 1) { 134 text.setVisibility(View.VISIBLE); 135 } else { 136 text.setVisibility(View.GONE); 137 } 138 139 } 140 141 // 当滑动状态改变时调用 142 @Override 143 public void onPageScrollStateChanged(int arg0) { 144 } 145 146 // 当当前页面被滑动时调用 147 @Override 148 public void onPageScrolled(int arg0, float arg1, int arg2) { 149 } 150 151 // 当新的页面被选中时调用 152 @Override 153 public void onPageSelected(int arg0) { 154 // 设置底部小点选中状态 155 monitorPoint(arg0); 156 } 157 158 }
相应的activity文件
1 package com.example.viewpager; 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.content.SharedPreferences; 9 import android.os.Bundle; 10 import android.widget.ImageView; 11 12 public class FristActivity extends Activity { 13 ImageView img; 14 SharedPreferences share; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.frist); 20 img = (ImageView) findViewById(R.id.img);
21 share = getSharedPreferences("aa", MODE_PRIVATE); 22 int number = share.getInt("bb", 1); 23 if (number == 1) { 24 share.edit().putInt("bb", 2).commit();// 将2编辑入share并提交 25 Timer timer = new Timer(); 26 TimerTask task = new TimerTask() { 27 28 @Override 29 public void run() { 30 Intent intent = new Intent(FristActivity.this, 31 MainActivity.class); 32 startActivity(intent); 33 finish(); 34 35 } 36 }; 37 timer.schedule(task, 3000); 38 } else { 39 40 Timer timer = new Timer(); 41 TimerTask task = new TimerTask() { 42 43 @Override 44 public void run() { 45 Intent intent = new Intent(FristActivity.this, 46 TwoActivity.class); 47 startActivity(intent); 48 finish(); 49 50 } 51 }; 52 timer.schedule(task, 3000); 53 } 54 } 55 56 }
相应的xml文件
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 <android.support.v4.view.ViewPager 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 android:id="@+id/view"/> 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:id="@+id/li" 14 android:layout_alignParentBottom="true" 15 android:layout_marginBottom="20dp" 16 android:gravity="center_horizontal" 17 android:orientation="horizontal"></LinearLayout> 18 <TextView 19 android:id="@+id/guideTv" 20 android:layout_width="130dp" 21 android:layout_height="40dp" 22 android:text="立即体验" 23 android:textColor="#ffffff" 24 android:background="#88000000" 25 android:textSize="14sp" 26 android:gravity="center" 27 android:layout_above="@id/li" 28 android:layout_centerHorizontal="true" 29 android:layout_marginBottom="20dp" 30 android:visibility="gone" 31 /> 32 </RelativeLayout>
timer线程使用 sharedpreference存储
第一步 得到sharedpreference
SharedPreferences share = getSharedPreferences("aa", MODE_PRIVATE);
第二步:判断是否是第一次下载并用线程计时
1 int number = share.getInt("bb", 1); 2 if (number == 1) { 3 share.edit().putInt("bb", 2).commit();// 将2编辑入share并提交 4 Timer timer = new Timer(); 5 TimerTask task = new TimerTask() { 6 7 @Override 8 public void run() { 9 Intent intent = new Intent(FristActivity.this, 10 MainActivity.class); 11 startActivity(intent); 12 finish(); 13 14 } 15 }; 16 timer.schedule(task, 3000); 17 } else { 18 19 Timer timer = new Timer(); 20 TimerTask task = new TimerTask() { 21 22 @Override 23 public void run() { 24 Intent intent = new Intent(FristActivity.this, 25 TwoActivity.class); 26 startActivity(intent); 27 finish(); 28 29 } 30 }; 31 timer.schedule(task, 3000);//跳转时间 32 }
底部小圆点步骤
1.将小圆点添加到数组里
private void addPoints() { // 1.根据图片多少,添加多少小圆点 for (int i = 0; i < img.size(); i++) { LinearLayout.LayoutParams param = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//设置子布局的宽高 if (i < 1) { param.setMargins(0, 0, 0, 0);//设置子布局之间的距离 } else { param.setMargins(10, 0, 0, 0); }
//将imageview写入父布局里 ImageView iv = new ImageView(this); iv.setLayoutParams(param); iv.setBackgroundResource(R.drawable.page_indicator_unfocused); liner.addView(iv);//将子布局添加到父布局里
}
liner.getChildAt(0).setBackgroundResource( R.drawable.page_indicator_focused); }
2.判断选中小圆点
1 private void monitorPoint(int Position) { 2 for (int i = 0; i < img.size(); i++) { 3 if (i == Position) { 4 liner.getChildAt(Position).setBackgroundResource( 5 R.drawable.page_indicator_focused); 6 7 } else { 8 liner.getChildAt(i).setBackgroundResource( 9 R.drawable.page_indicator_unfocused); 10 11 } 12 13 }
// 当滑动到最后一个添加按钮点击进去
if (Position == img.size() - 1) {
text.setVisibility(View.VISIBLE);
} else {
text.setVisibility(View.GONE);
}
3.此处有代码解释
1 // 当滑动状态改变时调用 2 @Override 3 public void onPageScrollStateChanged(int arg0) { 4 } 5 6 // 当当前页面被滑动时调用 7 @Override 8 public void onPageScrolled(int arg0, float arg1, int arg2) { 9 } 10 11 // 当新的页面被选中时调用 12 @Override 13 public void onPageSelected(int arg0) { 14 // 设置底部小点选中状态 15 monitorPoint(arg0); 16 } 17
原文:http://www.cnblogs.com/zwn-lucky7/p/6486689.html