欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

基于GridView和ActivityGroup实现的TAB分页(附源码)

程序员文章站 2023-12-03 14:20:34
分页大家都会用android的tabhost和tabactivity的组合,今天我这里实现的是gridview和activitygroup实现的分页,这里需要将activi...
分页大家都会用android的tabhost和tabactivity的组合,今天我这里实现的是gridview和activitygroup实现的分页,这里需要将activity转换成window,然后再换成成view添加到容器中,效果如下
基于GridView和ActivityGroup实现的TAB分页(附源码) 
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>

代码下载