[Android] Create a tab with fragment, androidfragment

Source: Internet
Author: User

[Android] Create a tab with fragment, androidfragment

Interface layout, linear layout, horizontal arrangement, and average center Distribution

Four textviews, 0 dip in width, 1 in weight, and center content

Use the LinearLayout control to set the id of the subject content.

 

Creates four Fragment Option Classes that inherit the Fragment class of the system.

Override onCreateView () to return a View object

 

Create four layout files to display the content of the tab

 

In activity

Obtain the LinearLayout object by using the findViewById () method.

First, find the four TextView control objects.

Call the setOnClickListener () method of the four TextView objects and set the click event. Parameter: this

The current activity implements the OnClickLisntenr interface, implements the onClick () method, and passes it to the View object.

 

Set the default Interface

Get the FragmentManager object by using the getFragmentManager () method

Call the beginTransaction () method of the FragmentManager object to start the transaction and obtain the FragmentTransaction object.

Call the replace () method of the FragmentTransaction object and replace the current interface with Fragment. Parameter: containerViewId, the first Fragment object

ContainerViewId is the container id, which is the id of the content body we define

Call the commit () method of the FragmentTransaction object to submit the transaction.

 

In The onClick () method

Call the getId () method of the View object to obtain the id of the currently clicked control.

Switch determines the id and displays different fragment based on different IDs.

 

In the replace () method of the FragmentTransaction object, parameter 1 must be a container object. I use TextView to report an exception in type conversion. The LinearLayout control must have a subcontrol.

 

No animation effect currently

 

Package com. tsh. mytabfrag; import android. app. activity; import android. app. fragment; import android. app. fragmentManager; import android. app. fragmentTransaction; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. textView; public class MainActivity extends Activity implements OnClickListener {private TextView tv1, tv2, tv3; private FragmentManager fm; private FragmentTransaction ft; private Fragment fm1, fm2, fm3; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the control tv1 = (TextView) findViewById (R. id. tv1); tv2 = (TextView) findViewById (R. id. tv2); tv3 = (TextView) findViewById (R. id. tv3); fm1 = new Fragment1 (); fm2 = new Fragment2 (); fm3 = new Fragment3 (); tv1.setOnClickListener (this); tv2.setOnClickListener (this); tv3.setOnClickListener (this ); fm = getFragmentManager (); ft = fm. beginTransaction (); ft. replace (R. id. content, fm1); ft. commit () ;}@ Override public void onClick (View v) {ft = fm. beginTransaction (); switch (v. getId () {case R. id. tv1: ft. replace (R. id. content, fm1); break; case R. id. tv2: ft. replace (R. id. content, fm2); break; case R. id. tv3: ft. replace (R. id. content, fm3); break; default: break;} ft. commit ();}}

 

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.