您的位置:首页 > 移动开发 > Android开发

Android学习第七篇——ViewPager引导页

2016-04-17 18:04 405 查看
首先我们来捋一捋ViewPager的使用流程

我们要使用一个ViewPager(默认在已经创建好布局之后),那么我们需要为ViewPager设置数据,那么数据哪里来呢??

因此我们就需要一个适配器,而对于ViewPager,我们有一个专门的适配器——PageAdapter,所以我们会在这里先创建一个class,

然后让这个类继承自PageAdapter,当我们创建了这个类以后,我们需要对这个类中的以下方法进行重写

instantiateItem(ViewGroup,
int)

destroyItem(ViewGroup,
int, Object)

getCount()

isViewFromObject(View,
Object)


对于这些方法的详细解释可以参考API文档中的解释

与此同时我们还需要为这个类定义以下参数和构造方法
private List<View> views;
private Context context;

public ViewPagerAdapter(List<View> views,Context context) {
// TODO Auto-generated constructor stub
this.views=views;
this.context=context;
}
当我们已经完成这些以后,我们可以返回到ViewPager的引导页去。
在该页面中我们同样需要定义一些变量
private ViewPager vp;
private ViewPagerAdapter vpAdapter;//自定义个PageAdapter类
private List<View> views;
private Button start_btn;//之后会用到的,当点击这个按钮,跳转到MainActivity


既然已经定义了这些个变量以后,我们就需要对这些变量进行初始化
private void initView(){
LayoutInflater inflater= LayoutInflater.from(this);

views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.one, null));
views.add(inflater.inflate(R.layout.two, null));
views.add(inflater.inflate(R.layout.three, null));

vpAdapter = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);

start_btn = (Button) views.get(2).findViewById(R.id.start);

start_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(Guid.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
这里我来一步步讲解一下这些代码的意思
首先,第一行的代码

LayoutInflater inflater= LayoutInflater.from(this);
这句代码的意思和起到的作用类似于findViewById和Inflate
下面是我查找别人的一些解释

LayoutInflater 和 inflate

在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

具体作用:

1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

/**

* LayoutInflater这个类的作用类似于findViewById(),

* 不同点:

* LayoutInflater是用来找layout下xml布局文件的,而且它会实例化

* findViewById()是找具体xml布局文件下的具体widget控件,比如:Button按钮

*

*

*

* inflate就相当于将一个xml中定义的布局找出来.   

* 因为如果在一个Activity文件里直接用findViewById()这个方法的话,

* 那么它所对应的是setConentView()中调用的那个layout里的组件.   

* 因此如果在同样的Activity里用到别的layout的话,

* 而且你还要设置这个layout里的组件(比如:ImageView,TextView)上的内容,

* 那么你就必须用inflate()先将这个layout找出来, 然后再用这个layout对象去找到它上面的组件

* 然后进行一系列的操作

*

* inflate()方法中参数:

* 1.想要用的布局文件的id

* 2.持有选项卡的内容,获取FrameLayout

* 3.true:将此处解析的xml文件做为根视图View

*/

LayoutInflater.from(this).inflate(R.layout.tabhost_layout,

tabhost.getTabContentView(), true);

看了这些解释以后,我相信应该可以明白了这个的意思。

我们来解释接下来的四行代码
views = new ArrayList<View>();
views.add(inflater.inflate(R.layout.one, null));
views.add(inflater.inflate(R.layout.two, null));
views.add(inflater.inflate(R.layout.three, null));
我们所看到的引导页其实是多个布局文件的切换,这里我们需要在布局文件夹下创建三个布局文件,分别为one,two ,three,而在这些页面上只有一个ImageView,不过在three中还有一个Button,这个Button是用于跳转到MainActivity中的。我们把这些布局全部添加到views中,这样我们就可以得到我们的数据源了。

下面三行代码的作用就是把数据源绑定到ViewPager中去
vpAdapter = new ViewPagerAdapter(views, this);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(vpAdapter);
然后我们还需要给three布局中的按钮绑定监听事件
start_btn = (Button) views.get(2).findViewById(R.id.start);

start_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(Guid.this,MainActivity.class);
startActivity(intent);
finish();
}
});
关于Intent的知识,以下是我之前做的笔记

Intent分为显式Intent和隐式Intent

显式Intent的调用方法为

StartActivity(new Intent(Content , 目标Acitivity))

隐式Intent的调用方法

需要在AndroidManifest中做如下设置
在Activity标签中添加一个intent-filter标签
同时设置属
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="自己定义名字"/>

在调用的时候需做这样的改变
StartActivity(new Intent("自己定义的名字"));
但是在这里我们有 个约定俗成的规定,那就是在取名的时候
包名.intent.action.类名
由于上述的名字复杂难记,所以我们会定义一个静态常量保存名字
然后我们在其他的Activity中通过   类名.变量名  来启动

通过上述的隐式方法可以在一个的project中调用另一个project中的Activity,当然我们也可以设置Androidmanifest中德Activity标签来改变访问性
<activity android:name="..." android:exported="false"/>
这样就无法在一个的project中调用另一个project中的Activity,exported默认是true的。


做好了上面的以后我们来想一想,这样好了吗??

这样做以后我们每次进入这个程序或者APP的时候多会经过引导页,这样给用户的体验就不是很好了。所以我们需要判断用户是否是第一次进入

因此,我们需要在创建一个类,用于判断进入引导页还是直接进入主页

先贴代码,然后解释

private boolean isFirstIn = false;
private static final int TIME= 2000;
private static final int Go_Home= 1000;
private static final int Go_Guide= 1001;

//接受数据
private Handler mHangle=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case Go_Home:
goHome();
break;

case Go_Guide:
goGuide();
break;
}

};
};

private void goHome(){
Intent intent =new Intent(WelcomeAct.this,MainActivity.class);
startActivity(intent);
finish();
}
private void goGuide(){
Intent intent =new Intent(WelcomeAct.this,Guid.class);
startActivity(intent);
finish();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
init();
}

private void init(){
SharedPreferences pre = getSharedPreferences("abc", MODE_PRIVATE);
isFirstIn = pre.getBoolean("isFirstIn", true);
if(!isFirstIn){
mHangle.sendEmptyMessageDelayed(Go_Home, TIME);
}else{
mHangle.sendEmptyMessageDelayed(Go_Guide, TIME);
Editor editor = pre.edit();
editor.putBoolean("isFirstIn", false);
editor.commit();
}
}
这里我们重点要讲的是Handler和SharedPreferences这两个。

handler在这里我的理解就是接受信息,然后进行判断是进入哪一个页,处理子进程中的事件

对于SharedPreferences这个,以下是我的笔记
Android应用开发SharedPreferences存储数据的使用方法
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来存储一些轻量级的数据。

1、使用SharedPreferences保存数据方法如下:

//实例化SharedPreferences对象(第一步)
SharedPreferences mySharedPreferences= getSharedPreferences("test",
Activity.MODE_PRIVATE);
//实例化SharedPreferences.Editor对象(第二步)
SharedPreferences.Editor editor = mySharedPreferences.edit();
//用putString的方法保存数据
editor.putString("name", "Karl");
editor.putString("habit", "sleep");
//提交当前数据
editor.commit();
//使用toast信息提示框提示成功写入数据
Toast.makeText(this, "数据成功写入SharedPreferences!" , Toast.LENGTH_LONG).show();

执行以上代码,SharedPreferences将会把这些数据保存在test.xml文件中,可以在File Explorer的data/data/相应的包名/test.xml 下导出该文件,并查看。

2、使用SharedPreferences读取数据方法如下:

//同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
SharedPreferencessharedPreferences= getSharedPreferences("test",
Activity.MODE_PRIVATE);
// 使用getString方法获得value,注意第2个参数是value的默认值
String name =sharedPreferences.getString("name", "");
String habit =sharedPreferences.getString("habit", "");
//使用toast信息提示框显示信息
Toast.makeText(this, "读取数据如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit,
Toast.LENGTH_LONG).show();


到此为止,我对于ViewPager的基础学习结束了,下面附上我自己的源代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: