Android creates tabs with fragment _android

Source: Internet
Author: User

This article combines the dynamic creation fragment to carry on a practice, realizes uses the fragment to create a tab

Project layout

<linearlayout 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 "Android:o" rientation= "Vertical" tools:context= ". Mainactivity "> <linearlayout android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Androi" d:orientation= "Horizontal" > <textview android:id= "@+id/tab1" android:layout_width= "0dip" android:layout _height= "Wrap_content" android:layout_weight= "1" android:gravity= "center" android:text= "social News"/> <TextVi EW android:id= "@+id/tab2" android:layout_width= "0dip" android:layout_height= "Wrap_content" Android:layout_weig ht= "1" android:gravity= "center" android:text= "Life News"/> <textview android:id= "@+id/tab3" android:layou T_width= "0dip" android:layout_height= "Wrap_content" android:layout_weight= "1" android:gravity= "Center" Android
: text= "Military News"/>
  <textview android:id= "@+id/tab4" android:layout_width= "0dip" android:layout_height= "Wrap_content" Androi d:layout_weight= "1" android:gravity= "center" android:text= "Entertainment News"/> </LinearLayout> <linearlayout A Ndroid:id= "@+id/content" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > </
 Linearlayout> </LinearLayout>

Create a new Fragment1.java~fragment4.java, where the code in Fragment1.java is as follows:

public class Fragment1 extends Fragment {
 @Override public
 View Oncreateview (layoutinflater inflater, ViewGroup Container,
   Bundle savedinstancestate) {return
  inflater.inflate (r.layout.fragment1, null);
 }

}

The code for several other files is similar

Create a new fragment1.xml~fragment4.xml, where the code in Fragment1.xml is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" Match_parent "
 android: gravity= "center"
 android:orientation= "vertical" >

 <textview
  android:id= "@+id/textview1"
  android:layout_width= "wrap_content"
  android:layout_height= "wrap_content"
  android:text= "social News" 
  android:textappearance= "Android:attr/textappearancelarge"/>
</LinearLayout>

The code for several other files is similar

The code in Mainactivity.java is as follows:

public class Mainactivity extends activity implements Onclicklistener {private linearlayout content;
 Private TextView TV1, TV2, TV3, TV4;
 Private Fragmentmanager FM;

 Private Fragmenttransaction ft;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

  Setcontentview (R.layout.activity_main);

  Content = (linearlayout) Findviewbyid (r.id.content);
  TV1 = (TextView) Findviewbyid (R.ID.TAB1);
  TV2 = (TextView) Findviewbyid (R.ID.TAB2);
  TV3 = (TextView) Findviewbyid (R.ID.TAB3);

  TV4 = (TextView) Findviewbyid (R.ID.TAB4);
  Tv1.setonclicklistener (this);
  Tv2.setonclicklistener (this);
  Tv3.setonclicklistener (this);

  Tv4.setonclicklistener (this);
  fm = Getfragmentmanager ();
  FT = fm.begintransaction (); Ft.replace (R.id.content, New Fragment1 ());
  By default Fragment1} @Override public void OnClick (View v) {ft = Fm.begintransaction ();
Switch (V.getid ()) {case R.id.tab1:ft.replace (r.id.content, New Fragment1 ());   Break
   Case R.id.tab2:ft.replace (r.id.content, New Fragment2 ());
  Break
   Case R.id.tab3:ft.replace (r.id.content, New Fragment3 ());
  Break
   Case R.id.tab4:ft.replace (r.id.content, New Fragment4 ());

  Break
  Default:break;

 } ft.commit ();

 }

}

After you run the project, the following effects are:


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

/** * Created by Gerry.zhong on 2016/10/11. * * var Gerry = (function () {///Create a separate object, inject all the methods, including the Var tool you want to throw away and don't want to throw away = {aaaa:function () {}, Bbbb:function ()
    {Console.log ("I just want to use inside, do not want to be used by others");
 
  }
  }; * * This object hosts all objects that need to be thrown away * 1. The methods in this object can write themselves * 2. The method in this object can be injected (TempObj.tool.AA in the example) * 3. The object can also be optionally thrown to the user for the method it needs, or it can be hidden (tool. BBBB) * * * * * * * * * * */var tempobj ={//reader For some initialization needs, sometimes there will be registration events, or some of the pre-operation Reader:function () {},//Inject all the selectors, easy to choose Changes, directly modify the selector in the object, without the need to change the global selector:{myselector: "#mySelector",///original password},//Inject all the interface address, convenient interface changes can be carried out, fast change, do not need
      Global find referenced object interface:{loginurl: "",},//Inject all events in page, unified management, recommend naming specification: event _ named, example Click_login registerele:{
    Click_login:function () {//Register Click event}},//Inject all AJAX requests, page all requests, will be here unified management, recommended naming conventions: Ajax_ naming, example Ajax_login
      * * * There are 2 scenarios in this request, see demand using * 1. Unfair use of a request scheme * 2. Common one request, but callback processing is not the same * * */ajaxrequest:{//unfair use of a request scheme Ajax_login:function () {$.post ("", "", FUnction (data) {tempObj.callback.call_login (data);
      });
        //There will be multiple business common this request Ajax_login_t:function (callback) {//All interface addresses are obtained from interface, callback in Tempobj.callback
      $.post ("", "", callback);
      },},//Process all callback functions, handle a callback for a request callback:{//non-common request processing callback Call_login:function (data) {//process callback
        //Public Request processing callback Call_login_t:function () {var temp = function () {};
      TempObj.ajaxRequest.ajax_login_T (temp); 
    },//All used tool classes, if each project is a separate unit.js or common.js, etc. to store some public methods, here can not use//PS: Here is only for the page to deal with some of the tool, generally do not need to throw out, but look at the business and decided
      tool:{a:function () {Console.log ("I wrote the method myself"); }, Aa:tool. 
    AAAA,//This is something I want to throw to someone else},//temporary cache storage area, only for this page, if the cross page please store cookies or localstorage etc///main solution sometimes use page control display to cache some of the current page's data temp:{}, * * Business use area, for each particular business to string all the atoms above * because all the above methods, just do one thing, this side can be based on business for a series of services, very simple * * * * * * FIR
  m:{}}; * * * the side to which the closure is thrown* * * * * */var outputobj =function () {///First Execute reader method, initialize some operations, such as registration events what what the Tempobj.reader (); 
    * * Throw the object for others to use * what you want to show and use, you can inject tempobj objects, just like the AA in tool * don't want to show things to others, just like the BBBB method inside the tool object, you can use it internally, the outside is not referenced
  * * */return tempobj;
  //Throw out the object you want to throw away because you are in control of all, haha.
return new Outputobj ();
 })();

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.