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

1. ActivityGroup

1.1 知识点

(1)了解ActivityGroup的作用;

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

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

1.2 具体内容

复制代码
<LinearLayout 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:orientation="vertical"
    tools:context=".ActivityGroupActivity" >

    <LinearLayout 
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        >
        <TextView android:id="@+id/cust_title"
                  android:textSize="28sp"
                  android:text="ActivityGroup实现分页导航"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
        /> 
    </LinearLayout>
    <!-- 中间动态加载的View -->
    <ScrollView 
        android:measureAllChildren="true"
        android:id="@+id/containerBody" 
        android:layout_weight="1"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        ></ScrollView>
    <LinearLayout 
        android:background="@android:color/black"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <!-- 导航按钮1 -->
        <ImageView 
            android:id="@+id/img1"
            android:src="@android:drawable/ic_dialog_dialer"
            android:layout_marginLeft="7dp" 
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
         <!-- 导航按钮2 -->
        <ImageView 
            android:id="@+id/img2"
            android:src="@android:drawable/ic_dialog_info"
            android:layout_marginLeft="7dp" 
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
         <!-- 导航按钮3 -->
        <ImageView 
            android:id="@+id/img3"
            android:src="@android:drawable/ic_dialog_alert"
            android:layout_marginLeft="7dp" 
            android:layout_marginTop="3dp"
            android:layout_marginBottom="3dp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            />
    </LinearLayout>
    
</LinearLayout>
java 复制代码
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的布局和文件:

java 复制代码
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ModuleView1" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一个Module" />

</RelativeLayout>
java 复制代码
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。

主布局:

java 复制代码
<LinearLayout 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:orientation="vertical"
    tools:context=".FragmentTabHostActivity" >
   
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    
    <FrameLayout
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <FrameLayout 
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"
            >
            
        </FrameLayout>
        
        
        
    </android.support.v4.app.FragmentTabHost>
        
</LinearLayout>

Activity:

java 复制代码
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<Fragment> list = new ArrayList<Fragment>();
	@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<count;i++){
    		//为每一个Tab按钮设置图标文字和内容
    		TabSpec tabSpec = mTabHost.newTabSpec(mTextViewArray[i]).setIndicator(getTabItemView(i));
    		mTabHost.addTab(tabSpec,fragmentArray[i],null);//将子tab添加进TabHost
    		//设置按钮的背景
    		mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(color.background_dark);
    	}
    }
    
    
    private void initPager(){
    	FragmentPage1 p1 = new FragmentPage1();
    	FragmentPage2 p2 = new FragmentPage2();
    	FragmentPage3 p3 = new FragmentPage3();
    	list.add(p1);
    	list.add(p2);
    	list.add(p3);
    	vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }
    
    private View getTabItemView(int index){
    	View view = layoutInflater.inflate(R.layout.tabspec_layout, null);
    	ImageView img = (ImageView) view.findViewById(R.id.img);
    	img.setImageResource(mImageViewArray[index]);
    	TextView tv = (TextView) view.findViewById(R.id.tv);
    	tv.setText(mTextViewArray[index]);
    	return view;
    }
    
    class ViewPagerListener implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {//根据焦点来确认切换到那个Tab
			TabWidget  widget = mTabHost.getTabWidget();
			int oldFocusability = widget.getDescendantFocusability();
			widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			mTabHost.setCurrentTab(arg0);
			widget.setDescendantFocusability(oldFocusability);
		}
    	
    }
    
    class TabHostListener implements OnTabChangeListener{

		@Override
		public void onTabChanged(String tabId) {
			int position = mTabHost.getCurrentTab();
			vp.setCurrentItem(position);
		}}
    
    class MyAdapter extends FragmentPagerAdapter{

		public MyAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}
    	
    }
}

单个标签布局:

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dp"
        />
    <TextView 
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:textColor="#FFFFFF"
        />

</LinearLayout>

单个fragment:

java 复制代码
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布局:

java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<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="@drawable/ic_launcher"
        />
    
    
</LinearLayout>

1.3 小结

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

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

相关推荐
浩浩乎@3 分钟前
【openGLES】安卓端EGL的使用
android
Kotlin上海用户组1 小时前
Koin vs. Hilt——最流行的 Android DI 框架全方位对比
android·架构·kotlin
zzq19962 小时前
Android framework 开发者模式下,如何修改动画过度模式
android
木叶丸2 小时前
Flutter 生命周期完全指南
android·flutter·ios
阿幸软件杂货间2 小时前
阿幸课堂随机点名
android·开发语言·javascript
没有了遇见2 小时前
Android 渐变色整理之功能实现<二>文字,背景,边框,进度条等
android
没有了遇见3 小时前
Android RecycleView 条目进入和滑出屏幕的渐变阴影效果
android
站在巨人肩膀上的码农4 小时前
去掉长按遥控器power键后提示关机、飞行模式的弹窗
android·安卓·rk·关机弹窗·power键·长按·飞行模式弹窗
呼啦啦--隔壁老王4 小时前
屏幕旋转流程
android
人生何处不修行4 小时前
实战:Android 15 (API 35) 适配 & 构建踩坑全记录
android