Android中引导页

大多数手机都会有引导页,是用户更快的了解这款应用是做什么的,下面我们来简单的使用一下。

MainActivity中:

package com.example.mac.welocomedemo;import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private ViewPager vp;private List views;private MainAdapter mainAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);vp = (ViewPager) findViewById(R.id.vp);mainAdapter = new MainAdapter(this);views = new ArrayList<>();//初始化Viewviews.add(getLayoutInflater().inflate(R.layout.item_image_one, null));views.add(getLayoutInflater().inflate(R.layout.item_image_two, null));View threeView = getLayoutInflater().inflate(R.layout.item_image_three, null);TextView textView = (TextView) threeView.findViewById(R.id.tv);textView.setOnClickListener(this);//将View添加到集合中views.add(threeView);//将数据传给适配器mainAdapter.setViews(views);//绑定适配器vp.setAdapter(mainAdapter);}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.tv:Toast.makeText(this, "跳转页面", Toast.LENGTH_SHORT).show();break;}}
}

Adapter中:

package com.example.mac.welocomedemo;import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;import java.util.List;/*** Created by mac on 16-8-1.*/
public class MainAdapter extends PagerAdapter {private Context context;private List views;public MainAdapter(Context context) {this.context = context;}public void setViews(List views) {this.views = views;notifyDataSetChanged();}@Overridepublic int getCount() {return views != null ? views.size() : 0;}@Overridepublic boolean isViewFromObject(View view, Object object) {return view == object;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {View view = views.get(position);container.addView(view);return view;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {View view = views.get(position);container.removeView(view);}
}

MainActivity的布局文件:


<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/vp"android:layout_width="match_parent"android:layout_height="match_parent" />
RelativeLayout>

item_image_one的布局文件:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageView
        android:layout_width="match_parent"android:layout_height="match_parent"android:src="@mipmap/a"android:scaleType="fitXY"/>
LinearLayout>

item_image_two的布局文件:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageView
        android:layout_width="match_parent"android:layout_height="match_parent"android:src="@mipmap/b"android:scaleType="fitXY"/>
LinearLayout>

item_image_three的布局文件:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageView
        android:layout_width="match_parent"android:layout_height="match_parent"android:src="@mipmap/c"android:scaleType="fitXY"/>
LinearLayout>

当然这只是一个简单的引导页,通常我们会在引导页中初始化数据,例如获取网络资源,当用户进入到主程序中的时候,使致谢数据都初始化完成,提高用户的体验效果。这个暂时还没有时间写,等以后有时间在写吧!

本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部