Android Fragment: An Activity composed of static Fragment and androidfragment
I have always been used to writing all event processing code in the Activity. I did not find that the Activity can be composed of multiple Fragment items until I carefully studied Fragment.
I do not have a deep understanding of Fragment. I will first start with static Fragment and use Fragment as a simple Activity control.
Let's write the most commonly used "title + body" layout!
Activity_main.xml:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity"> 6 7 <fragment 8 android:id="@+id/fragment_title" 9 android:name="com.example.hopecapital.myapplication.TitleFragment"10 android:layout_width="fill_parent"11 android:layout_height="45dp" />12 13 <fragment14 android:layout_below="@id/fragment_title"15 android:id="@+id/id_fragment_content"16 android:name="com.example.hopecapital.myapplication.ContentFragment"17 android:layout_width="fill_parent"18 android:layout_height="fill_parent" />19 20 </RelativeLayout>
For the layout of the main interface, you need to write the fragment used and specify their location.
MainActivity. java:
1 package com.example.hopecapital.myapplication; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 7 public class MainActivity extends Activity { 8 9 @Override10 protected void onCreate(Bundle savedInstanceState) {11 super.onCreate(savedInstanceState);12 setContentView(R.layout.activity_main);13 }14 15 }
You only need to load the layout here.
The following is the layout of the two fragment and the java code.
Fragment_title.xml:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "45dp"> 5 6 <ImageButton 7 android: id = "@ + id/btn_menu" 8 android: layout_width = "wrap_content" 9 android: layout_height = "wrap_content" 10 android: layout_centerVertical = "true" 11 android: layout_marginLeft = "3dp" 12 android: background = "@ mipmap/ic_launcher"/> 13 14 <TextView15 android: layout_width = "fill_parent" 16 android: layout_height = "fill_parent" 17 android: gravity = "center" 18 android: text = "title" 19 android: textSize = "20sp" 20 android: textStyle = "bold"/> 21 22 </RelativeLayout>
TitleFragment. java:
1 package com.example.hopecapital.myapplication; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.ImageButton; 9 import android.widget.Toast;10 11 public class TitleFragment extends Fragment {12 private ImageButton Menu;13 14 @Override15 public View onCreateView(LayoutInflater inflater, ViewGroup container,16 Bundle savedInstanceState){17 View view = inflater.inflate(R.layout.fragment_title,container,false);18 Menu = (ImageButton)view.findViewById(R.id.btn_menu);19 20 Menu.setOnClickListener(new View.OnClickListener() {21 @Override22 public void onClick(View v) {23 24 Toast.makeText(getActivity(),"titleFragment!",Toast.LENGTH_SHORT).show();25 }26 });27 return view;28 }29 }
Fragment_content.xml:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 6 <TextView 7 android: layout_width = "match_parent" 8 android: layout_height = "match_parent" 9 android: gravity = "center" 10 android: text = "body area" 11 android: textSize = "20sp"/> 12 13 </RelativeLayout>
ContentFragment. java:
1 package com.example.hopecapital.myapplication; 2 3 4 import android.app.Fragment; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 11 /**12 * A simple {@link Fragment} subclass.13 */14 public class ContentFragment extends Fragment {15 16 17 public ContentFragment() {18 // Required empty public constructor19 }20 21 22 @Override23 public View onCreateView(LayoutInflater inflater, ViewGroup container,24 Bundle savedInstanceState) {25 return inflater.inflate(R.layout.fragment_content,container,false);26 }27 28 29 }
At this point, the Activity is controlled by the Fragment of the two parts, and you can directly find the corresponding Fragment where you need to change it.