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组件可以实现弹出菜单的功能。

相关推荐
姑苏风2 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
数据猎手小k6 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小106 小时前
JavaWeb项目-----博客系统
android
风和先行7 小时前
adb 命令查看设备存储占用情况
android·adb
AaVictory.7 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰8 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
大风起兮云飞扬丶8 小时前
Android——网络请求
android
干一行,爱一行8 小时前
android camera data -> surface 显示
android
断墨先生9 小时前
uniapp—android原生插件开发(3Android真机调试)
android·uni-app
无极程序员10 小时前
PHP常量
android·ide·android studio