第五章 用户界面基础(使用Fragment实现Tab导航)

参考资料:
《Android应用程序开发》ISBN 9787302283164
参考软件:
Android Studio、Eclipse+ADT、Android SDK、JDK
使用Fragment实现Tab导航
http://blog.csdn.net/yaya_soft/article/details/9714011
http://blog.csdn.net/zjlovety/article/details/21519205


一、Tab效果

二、项目结构

三、文件代码
代码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="社会新闻" />
<TextView
android:id="@+id/tv2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="娱乐新闻" />
<TextView
android:id="@+id/tv3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="国际新闻" />
<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="社会新闻的内容" />
</FrameLayout>