标签:
1.引导页的4个界面布局,里面加载一张背景图片
插入到guide的界面布局中(这里不用fragment)
guide_background_fragment1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background1"/> </LinearLayout>
guide_background_fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background2"/> </LinearLayout>
guide_background_fragment3.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background3"/> </LinearLayout>
guide_background_fragment4.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide_background4"/> </LinearLayout>
guide.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <LinearLayout android:id="@+id/l1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_centerInParent="true" android:layout_alignParentBottom="true" > <ImageView android:id="@+id/imageV1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> <ImageView android:id="@+id/imageV2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> <ImageView android:id="@+id/imageV3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> <ImageView android:id="@+id/imageV4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/login_point" /> </LinearLayout> </RelativeLayout>
2. 适配器
GuidePagerAdapter.java
import android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import java.util.List; public class GuidePagerAdapter extends PagerAdapter{ private Context context; private List<View> views; public GuidePagerAdapter(List<View> views, Context context) { this.context = context; this.views = views; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(views.get(position)); return views.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(views.get(position)); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return (arg0 == arg1); } }
Guide.java
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; /** * Created by zps on 2015/9/3. */ public class Guide extends Activity implements ViewPager.OnPageChangeListener { private List<View> listView; private ViewPager viewPager; private GuidePagerAdapter vpAdapter; private int[] dotsId = {R.id.imageV1, R.id.imageV2, R.id.imageV3, R.id.imageV4}; private ImageView[] dotsView; private Button lijitiyanButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.view_pager); init(); initDots(); } private void initDots() { dotsView = new ImageView[listView.size()]; for (int i = 0; i < listView.size(); i++) { dotsView[i] = (ImageView) findViewById(dotsId[i]); } } private void init() { LayoutInflater inflater = LayoutInflater.from(this); listView = new ArrayList<View>(); //获取View对象 View view1 = inflater.inflate(R.layout.view_pager_img1, null); View view2 = inflater.inflate(R.layout.view_pager_img2, null); View view3 = inflater.inflate(R.layout.view_pager_img3, null); View view4 = inflater.inflate(R.layout.view_pager_img4, null); //view对象放在List<view>中 listView.add(view1); listView.add(view2); listView.add(view3); listView.add(view4); //List<view>放在适配器ViewPagerAdapter中 vpAdapter = new GuidePagerAdapter(listView, this); //获取ViewPage,设置适配器; viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(vpAdapter); viewPager.addOnPageChangeListener(this); lijitiyanButton =(Button) view4.findViewById(R.id.lijitiyan_button); lijitiyanButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Guide.this,MainActivity.class); startActivity(intent); finish(); } }); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i < dotsId.length; i++) { if(i == position){ dotsView[i].setImageResource(R.drawable.login_point_selected); }else { dotsView[i].setImageResource(R.drawable.login_point); } } } @Override public void onPageScrollStateChanged(int state) { } }
3.引导页的判断:是否第一次进入
public class GuideWelcome extends Activity{ private static final int TIME = 2000; private static boolean isFirstIn = false; private static final int First = 1000; private static final int NoFirst = 1001; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what){ case First: FirstIn(); break; case NoFirst: NoFirstIn(); break; default: break; } } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome); init(); } private void init() { //存储文件名为"gudiewelcome" SharedPreferences sharedPreferences = getSharedPreferences("guidewelcome",MODE_PRIVATE); isFirstIn = sharedPreferences.getBoolean("isFirstIn",true); if(!isFirstIn){ handler.sendEmptyMessageDelayed(NoFirst,TIME); }else { handler.sendEmptyMessageDelayed(First,TIME); //存储文件key为isFirstIn,值为false SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("isFirstIn",false); editor.commit(); } } private void FirstIn(){ Intent intent = new Intent(GuideWelcome.this,Guide.class); startActivity(intent); finish(); } private void NoFirstIn(){ Intent intent = new Intent(GuideWelcome.this,TabMainActivity.class); startActivity(intent); finish(); } }
ViewPager实现引导页(添加导航点,判断是否第一次进入主界面)
标签:
原文地址:http://www.cnblogs.com/520-1314/p/4795456.html