使用ViewPager+ActivityGroup实现划屏,同时切换activity

最近几天,项目用到了划屏技术,研究了好几天,终于搞出来了,现写个博客记录一下。

使用ViewPager+ActivityGroup实现划屏,同时可以切换activity,因为,我们这个项目是后期维护添加功能。现在我使用的方法,没有改动任何原来代码,只是添加了划屏代码。

上代码:

Whatsnews_viewpager.xml:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >  

    <android.support.v4.view.ViewPager
        android:id="@+id/whatsnew_viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" > 
        
        
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pagertitle"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="bottom"
            android:background="#0000"
            android:textColor="#ffff" />
    </android.support.v4.view.ViewPager>  
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        > 
        
         <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
        	android:layout_marginBottom="30dp" 
        	android:gravity="center_horizontal"     >
            
            <ImageView
            	android:id="@+id/page0"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content"        	     	
        		android:scaleType="matrix"
        		android:src="@drawable/page_now" />
            <ImageView
            	android:id="@+id/page1"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content"
        		android:layout_marginLeft="10dp"        	     	
        		android:scaleType="matrix"
        		android:src="@drawable/page" />
            <ImageView
            	android:id="@+id/page2"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content"
        		android:layout_marginLeft="10dp"           	     	
        		android:scaleType="matrix"
        		android:src="@drawable/page" />
            <ImageView
            	android:id="@+id/page3"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content" 
        		android:layout_marginLeft="10dp"          	     	
        		android:scaleType="matrix"
        		android:src="@drawable/page" />
            <ImageView
            	android:id="@+id/page4"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content" 
        		android:layout_marginLeft="10dp"          	     	
        		android:scaleType="matrix"
        		android:src="@drawable/page" />
            <ImageView
            	android:id="@+id/page5"
        		android:layout_width="wrap_content"
        		android:layout_height="wrap_content" 
        		android:layout_marginLeft="10dp"          	     	
        		android:scaleType="matrix"
        		android:src="@drawable/page" />           
            
        </LinearLayout>
        
       
    </LinearLayout>
    
	   
</FrameLayout>

act1.xml

<?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:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        
         <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="100dp"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="vertical" >

             <Button
                 android:id="@+id/textView1"
                 android:layout_width="500dp"
                 android:layout_height="300dp"
                 android:text="Signin"
                 android:textColor="#000000"
                 android:textAppearance="?android:attr/textAppearanceLarge" />

    	</LinearLayout>
        
    </RelativeLayout>

</LinearLayout>

act2.xml

<?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:orientation="vertical" >
    
<RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        
         <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="100dp"
        android:background="#ADFF2F"
        android:gravity="center"
        android:orientation="vertical" >

             <Button
                 android:id="@+id/textView2"
                 android:layout_width="500dp"
                 android:layout_height="300dp"
                 android:text="Register"
                 android:textColor="#000000"
                 android:textAppearance="?android:attr/textAppearanceLarge" />

    	</LinearLayout>
        
    </RelativeLayout>
</LinearLayout>

还有几个xml不贴了,都是很简单的布局。


现在贴出主功能代码Whatsnew.java(activity)

package com.example.android_demo6;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.test.ActivityTestCase;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class Whatsnew extends ActivityGroup {
	
	private ViewPager mViewPager;	
	private ImageView mPage0;
	private ImageView mPage1;
	private ImageView mPage2;
	private ImageView mPage3;
	private ImageView mPage4;
	private ImageView mPage5;
		
	private int currIndex = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whatsnew_viewpager);
        mViewPager = (ViewPager)findViewById(R.id.whatsnew_viewpager); 
        
        mViewPager.clearAnimation();
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
       
        
        mPage0 = (ImageView)findViewById(R.id.page0);
        mPage1 = (ImageView)findViewById(R.id.page1);
        mPage2 = (ImageView)findViewById(R.id.page2);
        mPage3 = (ImageView)findViewById(R.id.page3);
        mPage4 = (ImageView)findViewById(R.id.page4);
        mPage5 = (ImageView)findViewById(R.id.page5);
        
      //将要分页显示的View装入数组中
        LayoutInflater mLi = LayoutInflater.from(this);
        View view1 = mLi.inflate(R.layout.whats1, null);
        View view2 = mLi.inflate(R.layout.whats2, null);
        View view3 = mLi.inflate(R.layout.whats3, null);
        View view4 = mLi.inflate(R.layout.whats4, null);
        
        @SuppressWarnings("deprecation")
		View view5 = getLocalActivityManager().startActivity("activity1", new Intent(Whatsnew.this, ActivityOne.class)).getDecorView();
        
        @SuppressWarnings("deprecation")
		View view6 = getLocalActivityManager().startActivity("activity2", new Intent(Whatsnew.this, ActivityTwo.class)).getDecorView();
        		
        //      View view5 = mLi.inflate(R.layout.whats5, null);
        //        View view6 = mLi.inflate(R.layout.act1, null);
        
        
      //每个页面的view数据
        final ArrayList<View> views = new ArrayList<View>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);
        views.add(view5);
        views.add(view6);
        
        //填充ViewPager的数据适配器
        PagerAdapter mPagerAdapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}
			
			@Override
			public int getCount() {
				return views.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager)container).removeView(views.get(position));
			}
			
			
			
			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager)container).addView(views.get(position));
				return views.get(position);
			}
		};
		
		mViewPager.setAdapter(mPagerAdapter);
    }//OnCreate
    

    public class MyOnPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageSelected(int arg0) {
			switch (arg0) {
			case 0:				
				mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 1:
				mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage0.setImageDrawable(getResources().getDrawable(R.drawable.page));
				mPage2.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 2:
				mPage2.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage1.setImageDrawable(getResources().getDrawable(R.drawable.page));
				mPage3.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 3:
				mPage3.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage4.setImageDrawable(getResources().getDrawable(R.drawable.page));
				mPage2.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 4:
				mPage4.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage3.setImageDrawable(getResources().getDrawable(R.drawable.page));
				mPage5.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 5:
				mPage5.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				mPage4.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			}
			currIndex = arg0;
			//animation.setFillAfter(true);// True:图片停在动画结束位置
			//animation.setDuration(300);
			//mPageImg.startAnimation(animation);
		}
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}
/*    public void startbutton(View v) {  
      	Intent intent = new Intent();
		intent.setClass(Whatsnew.this,WhatsnewDoor.class);
		startActivity(intent);
		this.finish();
      } */   
}

ActivityOne.java

package com.example.android_demo6;



import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ActivityOne extends ActivityGroup {
	private Button button_1;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.act1, null);
		this.setContentView(v);
		
		button_1 = (Button) findViewById(R.id.textView1);
		ButtonClickListener();
		Log.d("ActGroupDemo", "Act1 onCreate()");
	}

	private void ButtonClickListener() {
		button_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(ActivityOne.this, "第一个界面", Toast.LENGTH_SHORT).show();;
			}
		});
	}
}


ActivityTwo.java

package com.example.android_demo6;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ActivityTwo extends ActivityGroup {
	private Button button_2;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.act2, null);
		this.setContentView(v);
		
		button_2 = (Button) findViewById(R.id.textView2);
		ButtonClickListener();
		Log.d("ActGroupDemo", "Act2 onCreate()");
	}

	private void ButtonClickListener() {
		button_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(ActivityTwo.this, "第二个界面", Toast.LENGTH_SHORT).show();;
			}
		});
	}
}

要完整代码找我啊。

你可能感兴趣的:(使用ViewPager+ActivityGroup实现划屏,同时切换activity)