Android组件通信——ActivityGroup(二十五)

1. ActivityGroup

1.1 知识点

(1)了解ActivityGroup的作用;

(2)使用ActivityGroup进行复杂标签菜单的实现;

(3)使用PopupWindow组件实现弹出菜单组件开发;

1.2 具体内容

Android组件通信——ActivityGroup(二十五)_第1张图片

Android组件通信——ActivityGroup(二十五)_第2张图片

Android组件通信——ActivityGroup(二十五)_第3张图片

Android组件通信——ActivityGroup(二十五)_第4张图片



    
         
    
    
    
    
        
        
         
        
         
        
    
    


package com.example.activitygroupproject;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ScrollView;

public class ActivityGroupActivity extends ActivityGroup {
    ScrollView container =null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
		setContentView(R.layout.activity_activity_group);
		container = (ScrollView) super.findViewById(R.id.containerBody);
		//导航1
		ImageView img1= (ImageView) super.findViewById(R.id.img1);
		img1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				container.removeAllViews();//清空子View
				container.addView(getLocalActivityManager().startActivity("Module1", 
						new Intent(ActivityGroupActivity.this,ModuleView1.class)
				.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
			}
		});
		//导航2
				ImageView img2= (ImageView) super.findViewById(R.id.img2);
				img2.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						container.removeAllViews();//清空子View
						container.addView(getLocalActivityManager().startActivity("Module2", 
								new Intent(ActivityGroupActivity.this,ModuleView2.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
					}
				});
				//导航3
				ImageView img3= (ImageView) super.findViewById(R.id.img3);
				img3.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						container.removeAllViews();//清空子View
						container.addView(getLocalActivityManager().startActivity("Module3", 
								new Intent(ActivityGroupActivity.this,ModuleView3.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
					}
				});
	}


}

下面是子Activity的布局和文件:



    



package com.example.activitygroupproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ModuleView1 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_module_view1);
	}


}

共有三个子Activity,其余两个类似,就只写一个。

以下实现目前非常流行的标签页实现形式FragmentTabHost+ViewPager。

主布局:


   
    
    
    
    
        
            
        
        
        
        
    
        


Activity:

package com.example.fragmenttabhost;

import java.util.ArrayList;
import java.util.List;

import android.R.color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

public class FragmentTabHostActivity extends FragmentActivity {
	FragmentTabHost mTabHost = null;
    LayoutInflater layoutInflater = null;
    Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class};
    int mImageViewArray[] = {android.R.drawable.ic_dialog_dialer,android.R.drawable.ic_dialog_info,android.R.drawable.ic_dialog_alert};
    String mTextViewArray[] = {"首页","消息","好友"};
    ViewPager vp;
    List list = new ArrayList();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment_tab_host);
		//实例化组件
		initView();
		initPager();
	}

    public void initView(){
    	vp = (ViewPager) super.findViewById(R.id.pager);
    	vp.setOnPageChangeListener(new ViewPagerListener());
    	layoutInflater = LayoutInflater.from(this);//实例化布局对象
    	mTabHost = (FragmentTabHost) super.findViewById(android.R.id.tabhost);
    	mTabHost.setup(this,getSupportFragmentManager(),R.id.pager);//实例化FragmentTabHost对象
    	mTabHost.setOnTabChangedListener(new TabHostListener());
    	int count = fragmentArray.length;//获取子tab的个数
    	for(int i= 0;i

单个标签布局:



    
    
    
    



单个fragment:

package com.example.fragmenttabhost;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentPage1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
		return inflater.inflate(R.layout.fragment, null);
    }
}

单个fragment布局:



    
    
    
    


1.3 小结

(1)ActivityGroup可以让多个Activity在一个屏幕上集中显示;

(2)通过PopupWindow组件可以实现弹出菜单的功能。

你可能感兴趣的:(Android,开发语言,android)