android引导页在最后一个页面在滑动跳转页面怎么做

 我来答
huanglenzhi
推荐于2016-10-12 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517196
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
第一个activity
package com.qualitypicture.activity;
import java.util.ArrayList;
import java.util.List;
import com.qualitypicture.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class GuideActivity extends Activity
{
private ViewPager viewPager;
private List<View> mImageViews; // 滑动的图片集合
private int[] imageResId; // 图片ID
private int currentItem = 0; // 当前图片的索引号
private GestureDetector gestureDetector; // 用户滑动
/** 记录当前分页ID */
private int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
setContentView(R.layout.guide_activity);
gestureDetector = new GestureDetector(new GuideViewTouch());
// 获取分辨率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
flaggingWidth = dm.widthPixels / 3;
imageResId = new int[]
{ R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_4 };
mImageViews = new ArrayList<View>();
// 初始化图片资源
LayoutInflater viewInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 0
View convertView0 = viewInflater.inflate(R.layout.guide_item, null);
LinearLayout linearLayout0 = (LinearLayout) convertView0.findViewById(R.id.guide_item);
linearLayout0.setBackgroundResource(imageResId[0]);
mImageViews.add(linearLayout0);
// 1
View convertView1 = viewInflater.inflate(R.layout.guide_item, null);
LinearLayout linearLayout1 = (LinearLayout) convertView1.findViewById(R.id.guide_item);
linearLayout1.setBackgroundResource(imageResId[1]);
mImageViews.add(linearLayout1);
// 2
View convertView2 = viewInflater.inflate(R.layout.guide_item, null);
LinearLayout linearLayout2 = (LinearLayout) convertView2.findViewById(R.id.guide_item);
linearLayout2.setBackgroundResource(imageResId[2]);
mImageViews.add(linearLayout2);
// 3
View convertView3 = viewInflater.inflate(R.layout.guide_item, null);
LinearLayout linearLayout3 = (LinearLayout) convertView3.findViewById(R.id.guide_item);
linearLayout3.setBackgroundResource(imageResId[3]);
mImageViews.add(linearLayout3);
// button监听
Button btn = (Button) convertView3.findViewById(R.id.start);
btn.setVisibility(View.VISIBLE);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
GoToMainActivity();
}
});
viewPager = (ViewPager) findViewById(R.id.guide_view);
viewPager.setAdapter(new MyAdapter());// 设置填充ViewPager页面的适配器
// 设置一个监听器,当ViewPager中的页面改变时调用
viewPager.setOnPageChangeListener(new MyPageChangeListener());
}
@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
if (gestureDetector.onTouchEvent(event))
{
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
private class GuideViewTouch extends SimpleOnGestureListener
{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
if (currentItem == 3)
{
if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY() - e2.getY()) && (e1.getX() - e2.getX() <= (-flaggingWidth) || e1.getX() - e2.getX() >= flaggingWidth))
{
if (e1.getX() - e2.getX() >= flaggingWidth)
{
GoToMainActivity();
return true;
}
}
}
return false;
}
}
/**
* 进入主界面
*/
void GoToMainActivity()
{
Intent intent = new Intent(GuideActivity.this, TabHostActivity.class);
startActivity(intent);
finish();
}
/**
* 当ViewPager中页面的状态发生改变时调用
*
* @author Administrator
*
*/
private class MyPageChangeListener implements OnPageChangeListener
{
public void onPageSelected(int position)
{
currentItem = position;
}
public void onPageScrollStateChanged(int arg0)
{
}
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
}
/**
* 填充ViewPager页面的适配器
*
* @author Administrator
*
*/
private class MyAdapter extends PagerAdapter
{
@Override
public int getCount()
{
return imageResId.length;
}
@Override
public Object instantiateItem(View arg0, int arg1)
{
((ViewPager) arg0).addView(mImageViews.get(arg1));
return mImageViews.get(arg1);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2)
{
((ViewPager) arg0).removeView((View) arg2);
}
@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)
{
}
@Override
public void finishUpdate(View arg0)
{
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
{
GoToMainActivity();
return false;
}
return super.onKeyDown(keyCode, event);
}
}

布局文件
第一个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/guide_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
第二个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
android:id="@+id/guide_item"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="5" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="@string/butTxt"
android:textColor="#00000000"
android:visibility="gone" >
</Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
大家用的时候文件名要写对,好友图片也要相应的放到文件中去!
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式