码迷,mamicode.com
首页 > 移动开发 > 详细

android用ViewPager实现欢迎界面

时间:2015-07-25 16:38:46      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

一、页面布局文件

welcome.xml

技术分享
<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" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_welcome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>
View Code

welcome_one.xml

技术分享
<LinearLayout 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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/bg_icon"
        android:gravity="center"
        android:text="one"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
View Code

welcome_two.xml

技术分享
<LinearLayout 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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/bg_icon"
        android:gravity="center"
        android:text="two"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
View Code

welcom_three.xml

技术分享
<LinearLayout 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" >

    <TextView
        android:id="@+id/tv_welcomethree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/bg_icon"
        android:gravity="center"
        android:text="three"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <Button
        android:id="@+id/btn_justtogo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"     
        android:text="登录"/>    

</LinearLayout>
View Code

WelcomeActivity.java

技术分享
package com.mytest.huilife;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class WelcomeActivity extends Activity {
    
    private List<View> welPageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
    
        
        View welOne = getLayoutInflater().inflate(R.layout.welcome_one, null);
        View welTwo = getLayoutInflater().inflate(R.layout.welcome_two, null);
        View welThree = getLayoutInflater().inflate(R.layout.welcome_three, null);
        List<View> pageList = new ArrayList<View>();
        pageList.add(welOne);
        pageList.add(welTwo);
        pageList.add(welThree);
        
        ViewPager viewPager = (ViewPager) this.findViewById(R.id.viewpager_welcome);
        viewPager.setAdapter(new WelcomePagerAdapter(pageList));

        Button btnJusttogo = (Button)welThree.findViewById(R.id.btn_justtogo);
        btnJusttogo.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Toast.makeText(WelcomeActivity.this, "just to go ", Toast.LENGTH_SHORT).show();
            }
        });        
        
    }
}
View Code

WelcomePagerAdapter.java

技术分享
package com.mytest.huilife;

import java.util.List;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

public class WelcomePagerAdapter extends PagerAdapter {

    private List<View> pageList;

    public WelcomePagerAdapter(List<View> pageList) {
        this.pageList = pageList;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ViewGroup vg = (ViewGroup) arg0;
        vg.removeView(pageList.get(arg1));

    }

    @Override
    public void finishUpdate(View arg0) {

    }

    @Override
    public int getCount() {
        return pageList.size();
    }

    @Override
    public Object instantiateItem(View arg0, int arg1) {
        ViewGroup vg = (ViewGroup) arg0;
        vg.addView(pageList.get(arg1));
        return pageList.get(arg1);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {

        return null;
    }

    @Override
    public void startUpdate(View arg0) {

    }

}
View Code

 

android用ViewPager实现欢迎界面

标签:

原文地址:http://www.cnblogs.com/2015android/p/4676036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!