分页大家都会用Android的TabHost和TabActivity的组合,今天我这里实现的是GridView和ActivityGroup实现的分页,这里需要将Activity转换成Window,然后再换成成View添加到容器中,效果如下
1.布局文件底部放一个GridView,然后一个LinearLayout容器在GridView之上,LinearLayout用于装载Activity的,这里布局位置错误,运行程序会抛异常,修改在下面
<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" > <LinearLayout android:id="@+id/activity_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/gridView1" android:layout_alignParentTop="true" android:orientation="vertical" > </LinearLayout> <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/navigationbardown" android:cacheColorHint="@android:color/transparent" android:gravity="center" android:listSelector="@android:color/transparent" android:verticalSpacing="0dip" > </GridView> </RelativeLayout>2.接下来就是代码啦,我们需要设置GridView的列数mGridView.setNumColumns(mImageIds.length),因为他只能显示一行,然后就是设置GridView点击高亮,每张图片的高亮图片都不一样的,然后就是点击不同的item进入不同的Activity,我们先用到ActivityGroup里面的public Window startActivity(String id, Intent intent) ,将Activity转换成Window,然后通过public abstract View getDecorView()这个抽象方法将Window转换成View,在添加到LinearLayout容器中
package com.example.tabactivity; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; public class TestActivity extends ActivityGroup implements OnItemClickListener { /** * Tab标签 */ private GridView mGridView; /** * 普通图片id数组 */ private int [] mImageIds; /** * 高亮图片id数组 */ private int [] mImageLightIds; /** * GridView 适配器 */ private ImageAdapter mImageAdapter; /** * 装载Activity的容器 */ private LinearLayout mLinearLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); mImageIds = new int[]{R.drawable.home, R.drawable.task, R.drawable.addrbook, R.drawable.longnormal}; mImageLightIds = new int[]{R.drawable.home1, R.drawable.taskhl, R.drawable.addrbook1, R.drawable.longhightlight}; mLinearLayout = (LinearLayout) findViewById(R.id.activity_group); mGridView = (GridView) findViewById(R.id.gridView1); //因为我们显示一行,列数等于数组的长度 mGridView.setNumColumns(mImageIds.length); mImageAdapter = new ImageAdapter(); mGridView.setAdapter(mImageAdapter); mGridView.setOnItemClickListener(this); startActivity(0); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //点击设置高亮显示 mImageAdapter.setImageLight(position); startActivity(position); } /** * 根据position跳转到不同的Activity * @param id */ private void startActivity(int position){ //先清除容器里面的View mLinearLayout.removeAllViews(); Intent intent = null; if(position == 0){ intent = new Intent(TestActivity.this, Activity_01.class); }else if(position == 1){ intent = new Intent(TestActivity.this, Activity_02.class); }else if(position == 2){ intent = new Intent(TestActivity.this, Activity_03.class); }else if(position == 3){ intent = new Intent(TestActivity.this, Activity_04.class); } intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //将Activity转换成View View view = getLocalActivityManager().startActivity("intent", intent).getDecorView(); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); //将Activity转换成的View添加到容器 mLinearLayout.addView(view, layoutParams); } /** * 图片适配器,没什么特别的,里面有一个设置高亮的方法比较重要 * @author mining * */ private class ImageAdapter extends BaseAdapter{ private int currentItem; /** * 设置高亮显示 * @param currentItem */ public void setImageLight(int selectItem){ this.currentItem = selectItem; notifyDataSetChanged(); } @Override public int getCount() { return mImageIds.length; } @Override public Object getItem(int position) { return mImageIds[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null){ convertView = new ImageView(TestActivity.this); } if(position == currentItem){ convertView.setBackgroundResource(mImageLightIds[position]); }else{ convertView.setBackgroundResource(mImageIds[position]); } return convertView; } } }
代码都上完了,是不是很简单的赶脚,短短的100多行的代码就实现了Tab分页,上面有设置GridView高亮的方法,还不知道设置高亮的同学可以借鉴一下咯,呵呵, 写的不好,希望各位大牛指点指点!万分感谢
不好意思,上面的布局需要纠正下,位置放错了
<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" > <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/navigationbardown" android:cacheColorHint="@android:color/transparent" android:gravity="center" android:listSelector="@android:color/transparent" android:verticalSpacing="0dip" > </GridView> <LinearLayout android:id="@+id/activity_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/gridView1" android:layout_alignParentTop="true" android:orientation="vertical" > </LinearLayout> </RelativeLayout>