标签:
逆境是成长必经的过程,能勇于接受逆境的人,生命就会日渐的茁壮。
本讲内容:GridView和ActivityGroup实现的Tab分页
一、ActivityGroup相关--getLocalActivityManager() 以及intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)用法
1、ActivityGroup中可以调用getLocalActivityManager()方法获取LocalActityManager来管理Activity。
2、LocalActivityManager通过startActivity(String id, Intent intent)方法,可以与指定的Actiivty绑定,并且返回一个Window。LocalActivityManager可以同时管理多个Activity
3、Window可以通过getDecorView()方法,返回一个View,然后通过与指定容器的addView(View)方法,实现不同的效果
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)如果在当前任务中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
示例一效果图:
下面是res/layout/activity_main.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 动态显示界面 --> <LinearLayout android:id="@+id/body" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/gridView" android:layout_alignParentTop="true" android:orientation="vertical" > </LinearLayout> <!-- 底部功能菜单栏 --> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/navigationbardown" android:cacheColorHint="#00000000" android:gravity="center" android:listSelector="@android:color/transparent" android:verticalSpacing="0dp" /> </RelativeLayout>
技巧 :制作背景色:用PS 画条黑线(navigationbardown.png) 即为黑色了
public class ImageAdapter extends BaseAdapter{ //普通图片数组 private int[] imgs={R.drawable.home, R.drawable.addrbook, R.drawable.longnormal}; //高亮图片数组 protected int[] imgsLight={R.drawable.home1, R.drawable.addrbook1, R.drawable.longhightlight};; private int selectItem; private Context context; //图片适配器,里面有一个设置高亮的方法比较重要 protected void setImageLih(int selectItem) { this.selectItem=selectItem; //界面刷新 notifyDataSetChanged(); } public ImageAdapter(Context context) { this.context = context; } @Override public int getCount() { return imgs.length; } @Override public Object getItem(int position) { return imgs[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){//产生ImageView 对象 convertView = new ImageView(context); } if(position==selectItem){ convertView.setBackgroundResource(imgsLight[position]); }else{ convertView.setBackgroundResource(imgs[position]); } return convertView; } }
public class MainActivity extends ActivityGroup { // Tab标签 private GridView gridView; // GridView 适配器 private ImageAdapter adapter; // 装载Activity的容器 private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } private void initUI() { adapter = new ImageAdapter(this); layout = (LinearLayout) findViewById(R.id.body); gridView = (GridView) findViewById(R.id.gridView); // 因为我们显示一行,列数等于数组的长度 gridView.setNumColumns(adapter.getCount()); gridView.setAdapter(adapter); startActivity(0); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> Parent, View view, int position, long id) { // 点击设置高亮显示 adapter.setImageLih(position); startActivity(position); } }); } // 根据position跳转到不同的Activity private void startActivity(int position) { // 先清除容器里面的View layout.removeAllViews(); Intent intent = null; if (position == 0) { intent = new Intent(MainActivity.this, OneView.class); } else if (position == 1) { intent = new Intent(MainActivity.this, TwoView.class); } else if (position == 2) { intent = new Intent(MainActivity.this, ThreeView.class); } intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 将Activity转换成View View view = getLocalActivityManager().startActivity("intent", intent) .getDecorView(); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); // 将Activity转换成的View添加到容器 layout.addView(view, layoutParams); } }
使用view的removeAllViews()方法清除不需要的view,使用addView(View
v)方法添加需要的view。
public class OneView extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.one); } }
下面是TwoView.java界面文件:
public class TwoView extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two); } }
下面是ThreeView.java界面文件:
public class TwoView extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two); } }
第八十五讲:GridView和ActivityGroup实现的Tab分页
标签:
原文地址:http://blog.csdn.net/liguojin1230/article/details/44276341