分頁大家都會用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>
代碼下載