Android learning-Fragment usage,

Source: Internet
Author: User

Android learning-Fragment usage,

1. simple use of fragments (add two fragments to an activity and divide the fragments equally among the active space)

1. Create a New FragmentTest project;

Create a left-side fragment layout left_fragment.xml. The Code is as follows: (only one button is placed and displayed horizontally in the center)

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent"> 6     <Button 7         android:id="@+id/button" 8         android:layout_width="wrap_content" 9         android:layout_height="wrap_content"10         android:layout_gravity="center_horizontal"11         android:text="Button"12         />13 </LinearLayout>

Create the right-side fragment layout right_fragment.xml. The Code is as follows: (set the layout background to blue and place a TextView to display text)

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:background="@color/colorPrimaryDark"> 7  8     <TextView 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:layout_gravity="center_horizontal"12         android:textSize="20sp"13         android:text="This is right fragment"14         />15 </LinearLayout>

2. Create a New LeftFragment class and let it inherit from Fragment (note that Fragment here uses the support-v4), the Code is as follows:

1 public class LeftFragment extends Fragment{2     @Override3     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){4         View view=inflater.inflate(R.layout.left_fragment,container,false);5         return view;6     }7 }

Override the onCreateView () method of Fragment, and then dynamically load the defined left_fragment layout through the inflate () method of LayoutInflater in this method.

The new RightFragment class also inherits Fragment. The Code is as follows:

1 public class RightFragment extends Fragment {2     @Override3     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){4         View view=inflater.inflate(R.layout.right_fragment,container,false);5         return view;6     }7 }

3. modify the code in activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     > 6    <fragment 7        android:id="@+id/left_fragment" 8        android:name="com.example.administrator.fragmenttest.LeftFragment" 9        android:layout_width="0dp"10        android:layout_height="match_parent"11        android:layout_weight="1"12        />13     <FrameLayout14         android:id="@+id/right_layout"15         android:layout_width="0dp"16         android:layout_height="match_parent"17         android:layout_weight="1"18         >19     </FrameLayout>20 </LinearLayout>

Run the program as follows:

Ii. dynamically add fragments

1. Create another_right_fragment.xml based on the preceding Code. The Code is as follows: (the layout file is basically the same as right_fragment.xml, and only the background color and display text are modified)

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="match_parent" 5     android:layout_height="match_parent" 6     android:background="#ffff00" 7     > 8     <TextView 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:layout_gravity="center_horizontal"12         android:textSize="20sp"13         android:text="This is another right fragment"14         />15 </LinearLayout>

2. Create the AnotherRightFragment class as another right part. The Code is as follows:

1 public class AnotherRightFragment extends Fragment{2         @Override3         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){4             View view=inflater.inflate(R.layout.another_right_fragment,container,false);5             return view;6         }7 }

3. Modify activity_main.xml with the following code:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     > 6    <fragment 7        android:id="@+id/left_fragment" 8        android:name="com.example.administrator.fragmenttest.LeftFragment" 9        android:layout_width="0dp"10        android:layout_height="match_parent"11        android:layout_weight="1"12        />13     <FrameLayout14         android:id="@+id/right_layout"15         android:layout_width="0dp"16         android:layout_height="match_parent"17         android:layout_weight="1"18         >19     </FrameLayout>20 </LinearLayout>

4. modify the code in MainActivity: (add content to FrameLayout in the Code)

 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 2  3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_main); 7         Button button=(Button)findViewById(R.id.button); 8         button.setOnClickListener(this); 9         replaceFragment(new RightFragment());10     }11 12     @Override13     public void onClick(View v){14         switch(v.getId()){15             case R.id.button:16                 replaceFragment(new AnotherRightFragment());17                 break;18             default:19                 break;20         }21     }22 23     private void replaceFragment(Fragment fragment){24         FragmentManager fragmentManager=getSupportFragmentManager();25         FragmentTransaction transaction=fragmentManager.beginTransaction();26         transaction.replace(R.id.right_layout,fragment);27         transaction.commit();28     }29 }

First, register a click event for the button in the left fragment, and then call the replaceFrameLayout () method to dynamically Add the RightFragment. When you click the button in the left fragment, replaceFragment () is called () method to replace the right part with AnotherRightFragment.

Run the program again to see the same interface as before, and then click the button, the effect is as follows:

 

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.