第五章 用戶(hù)界面基礎(chǔ)(使用Fragment實(shí)現(xiàn)Tab導(dǎo)航)

參考資料:
《Android應(yīng)用程序開(kāi)發(fā)》ISBN 9787302283164
參考軟件:
Android Studio、Eclipse+ADT、Android SDK、JDK
使用Fragment實(shí)現(xiàn)Tab導(dǎo)航
http://blog.csdn.net/yaya_soft/article/details/9714011
http://blog.csdn.net/zjlovety/article/details/21519205


?
一、Tab效果

二、項(xiàng)目結(jié)構(gòu)

三、文件代碼
代碼1 activity_main.xml
<?xml version="1.0"?encoding="utf-8"?>
?<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"
?????xmlns:app="http://schemas.android.com/apk/res-auto"
?????xmlns:tools="http://schemas.android.com/tools"
?????android:layout_width="match_parent"
?????android:layout_height="match_parent"
?????android:orientation="vertical"
?????tools:context="com.example.ch04.MainActivity">
???? <LinearLayout
?????????android:layout_width="match_parent"
?????????android:layout_height="wrap_content"
?????????android:orientation="horizontal">
???????? <TextView
?????????????android:id="@+id/tv1"
?????????????android:layout_width="0dip"
?????????????android:layout_height="wrap_content"
?????????????android:layout_weight="1"
?????????????android:text="社會(huì)新聞"?/>
???????? <TextView
?????????????android:id="@+id/tv2"
?????????????android:layout_width="0dip"
?????????????android:layout_height="wrap_content"
?????????????android:layout_weight="1"
?????????????android:text="娛樂(lè)新聞"?/>
???????? <TextView
?????????????android:id="@+id/tv3"
?????????????android:layout_width="0dip"
?????????????android:layout_height="wrap_content"
?????????????android:layout_weight="1"
?????????????android:text="國(guó)際新聞"?/>
???????? <TextView
?????????????android:id="@+id/tv4"
??????? ?????android:layout_width="0dip"
?????????????android:layout_height="wrap_content"
?????????????android:layout_weight="1"
?????????????android:text="體育新聞"?/>
???? </LinearLayout>
???? <LinearLayout
?????????android:id="@+id/content"
?????????android:layout_width="fill_parent"
?????????android:layout_height="fill_parent"
?????????android:orientation="horizontal">
???? </LinearLayout>
?</LinearLayout>
代碼2 MainActivy
package?com.example.ch04;
?
?import?android.app.FragmentManager;
?import?android.app.FragmentTransaction;
?import?android.support.v7.app.AppCompatActivity;
?import?android.os.Bundle;
?import?android.view.View;
?import?android.widget.LinearLayout;
?import?android.widget.TextView;
?import?android.widget.Toast;
?
?public class?MainActivity?extends?AppCompatActivity?implements ?View.OnClickListener{
?????private?LinearLayout?linearLayout;
?????private?TextView?tv1,tv2,tv3,tv4;
?????private?FragmentManager?fragmentManager;
?????private?FragmentTransaction?fragmentTransaction;
?????@Override
?????protected void?onCreate(Bundle savedInstanceState) {
?????????super.onCreate(savedInstanceState);
???????? setContentView(R.layout.activity_main);
?????????linearLayout?= (LinearLayout)?this.findViewById(R.id.content);
?????????tv1?= ?(TextView)?this.findViewById(R.id.tv1);
???????? ?tv2?= (TextView)?this.findViewById(R.id.tv2);
?????????tv3?= ?(TextView)?this.findViewById(R.id.tv3);
?????????tv4?= ?(TextView)?this.findViewById(R.id.tv4);
?????????tv1.setOnClickListener(this);
?????????tv2.setOnClickListener(this);
?????????tv3.setOnClickListener(this);
?????????tv4.setOnClickListener(this);
?????????fragmentManager?= getFragmentManager();
?????????fragmentTransaction?=?fragmentManager.beginTransaction();
?????????//第一次顯示的新聞
?????????fragmentTransaction.add(R.id.content,new?Fragment1());
?????????fragmentTransaction.commit();
???? }
?
?????@Override
?????public void?onClick(View v) {
?????????fragmentTransaction?=?fragmentManager.beginTransaction();
?????????switch?(v.getId()){
?????????????case?R.id.tv1:
???????????????? Toast.makeText(this,?"tv1", Toast.LENGTH_SHORT).show();
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment1());
?????????????????break;
?????????????case?R.id.tv2:
???????????????? Toast.makeText(this,?"tv2", Toast.LENGTH_SHORT).show();
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment2());
?????????????????break;
?????????????case?R.id.tv3:
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment3());
?????????????????break;
?????????????case?R.id.tv4:
?????????????????fragmentTransaction.replace(R.id.content,new?Fragment4());
?????????????????break;
???????? }
?????????fragmentTransaction.commit();
???? }
?}?
代碼3 Fragment1??? Fragment2? Fragment3?Fragment4
package?com.example.ch04;
?import?android.os.Bundle;
?import?android.app.Fragment;
?import?android.view.LayoutInflater;
?import?android.view.View;
?import?android.view.ViewGroup;
?/**
??* A simple {@link?Fragment} ?subclass.
??*/
?public class?Fragment1?extends?Fragment {
?????public?Fragment1() {
?????????// Required empty public constructor
?????}
?????@Override
?????public?View onCreateView(LayoutInflater inflater, ViewGroup container,
????????????????????????????? Bundle ?savedInstanceState) {
?????????// Inflate the layout for this fragment
?????????return?inflater.inflate(R.layout.fragment_fragment1, container,?false);
???? }
?}
代碼4 layout/fragment_fragment1.xml ??
<FrameLayout ?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"
?????tools:context="com.example.ch04.Fragment1">
?????<!--?TODO: Update blank fragment layout?-->
?????<TextView
?????????android:layout_width="match_parent"
?????????android:layout_height="match_parent"
?????????android:text="社會(huì)新聞的內(nèi)容"?/>
?</FrameLayout>