Android Entry Activitygroup + GridView implement tab Paging tab

Source: Internet
Author: User
Tags transparent color

Many client software and browser software like to use tab page tags to build the interface framework. Readers may immediately think of using the combination of tabhost and tabactivity, but the most common is not them, but the combination of the GridView and Activitygroup. Whenever a user selects an item in the GridView, Activitygroup adds the activity Window for that item as a View to the container (linearlayout) specified by Activitygroup.

Next, this example runs:

Imageadapter is one of the keys to this example, it inherits from Baseadapter, and adds some custom methods. The source code of Imageadapter is as follows:

 Packagecom. Activitygroupdemo;ImportAndroid.content.Context;Importandroid.graphics.drawable.ColorDrawable;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageView;/** *  * @authorGV **/ Public classImageadapterextendsBaseadapter {PrivateContext Mcontext; Privateimageview[] Imgitems; Private intSelresid;  PublicImageadapter (Context C,int[] Picids,intWidthintHeightintSelresid) {Mcontext=C;  This. selresid=Selresid; Imgitems=NewImageview[picids.length];  for(inti=0;i<picids.length;i++) {Imgitems[i]=NewImageView (Mcontext); Imgitems[i].setlayoutparams (NewGridview.layoutparams (width, height));//setting ImageView width and heightImgitems[i].setadjustviewbounds (false); //Imgitems[i].setscaletype (ImageView.ScaleType.CENTER_CROP);Imgitems[i].setpadding (2, 2, 2, 2);         Imgitems[i].setimageresource (Picids[i]); }    }       Public intGetCount () {returnimgitems.length; }       PublicObject GetItem (intposition) {         returnposition; }       Public LongGetitemid (intposition) {         returnposition; }      /*** Set the selected effect*/       Public voidSetFocus (intindex) {           for(inti=0;i)          {              if(i!=index) {Imgitems[i].setbackgroundresource (0);//Restore unselected Styles}} imgitems[index].setbackgroundresource (Selresid);//Set the selected style    }           PublicView GetView (intposition, View Convertview, ViewGroup parent)         {ImageView ImageView; if(Convertview = =NULL) {ImageView=Imgitems[position]; } Else{ImageView=(ImageView) Convertview; }         returnImageView; } } 

SetFocus (int) This method is a key point, which is to implement the selected effect. For example, with ABCD4 item, where C is selected, item other than c is set to a style that is not selected, and C is set to the selected style.

The next step is to write the main activity, the main activity contains the GridView control, named Gvtopbar, with 2 points to note.

  • Setnumcolumns (): You must use Setnumcolumns to set the number of columns, because this GridView has only one row, that is, all the item is on the same line, and the item number is the number of columns.
  • Setselector (New colordrawable (color.transparent)): Transparent the system's default selected background color because we have added SetFocus () to the Baseadapter to change the selected style.
     Packagecom. Activitygroupdemo;Importandroid.app.Activity;ImportAndroid.app.ActivityGroup;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.graphics.Color;Importandroid.graphics.drawable.ColorDrawable;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.view.ViewGroup.LayoutParams;ImportAndroid.widget.AdapterView;ImportAndroid.widget.GridView;Importandroid.widget.LinearLayout;ImportAndroid.widget.Toast;ImportAndroid.widget.AdapterView.OnItemClickListener;/** *  * @authorGV **/ Public classActivitygroupdemoextendsActivitygroup {PrivateGridView Gvtopbar; PrivateImageadapter Topimgadapter;  PublicLinearLayout container;//Container for loading sub activity    /**picture of the top button **/    int[] Topbar_image_array ={r.drawable.topbar_home, R.drawable.topbar_user, R.drawable.topbar_shoppingcart, r.drawable.to    Pbar_note}; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Gvtopbar= (GridView) This. Findviewbyid (R.id.gvtopbar); Gvtopbar.setnumcolumns (topbar_image_array.length);//set number of rows per rowGvtopbar.setselector (NewColordrawable (color.transparent));//Transparent Color When selectedGvtopbar.setgravity (Gravity.center);//Center PositionGvtopbar.setverticalspacing (0);//Vertical Interval        intwidth = This. Getwindowmanager (). Getdefaultdisplay (). GetWidth ()/topbar_image_array.length; Topimgadapter=NewImageadapter ( This, Topbar_image_array, Width, 48, R.drawable.topbar_itemselector); Gvtopbar.setadapter (topimgadapter);//Setup Menu AdapterGvtopbar.setonitemclicklistener (NewItemclickevent ());//Project Click eventscontainer =(LinearLayout) Findviewbyid (R.id.container); Switchactivity (0);//No. 0 page opens by default    }    classItemclickeventImplementsOnitemclicklistener { Public voidOnitemclick (adapterview<?> arg0, View arg1,intArg2,LongArg3)        {switchactivity (ARG2); }    }    /*** Open specified activity based on ID *@paramID GridView The ordinal of the selected item*/    voidSwitchactivity (intID) {topimgadapter.setfocus (ID);//selected item gets highlightedContainer.removeallviews ();//all view in the container must be cleared firstIntent Intent =NULL; if(id = = 0 | | id = = 2) {Intent=NewIntent (Activitygroupdemo. This, Activitya.class); } Else if(id = = 1 | | id = = 3) {Intent=NewIntent (Activitygroupdemo. This, Activityb.class);        } intent.addflags (Intent.flag_activity_clear_top); //Activity shifts to ViewWindow subactivity =Getlocalactivitymanager (). StartActivity ("Subactivity", intent); //container Add ViewContainer.addview (Subactivity.getdecorview (), Layoutparams.fill_parent, Layoutparams.fill_paren    T); }}

    The main activity layout XML file source code is as follows:

    <?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical" android:layout_width= "fill_parent"Android:layout_height= "Fill_parent" > <relativelayout android:layout_height= "fill_parent"Android:layout_width= "Fill_parent" > <gridview android:layout_height= "wrap_content" android:id= "@+id/gvtopbar"Android:layout_alignparenttop= "true" android:layout_width= "Fill_parent"Android:fadingedgelength= "5dip" android:fadingedge= "vertical" > </GridView> <linearlayout android:id= "@+id/container"Android:layout_below= "@+id/gvtopbar" android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > </LinearLayout> </RelativeLayout></LinearLayout>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.