这个学期的短学期要求我们在移动平台上进行开发,虽然自己断断续续已经写了大半年的Android了,但是暑假的时候一直在刷PAT,开学了之后也在写HDUOJ,eclipse也好久没打开过了,现在趁这个机会重新捡起Android。
这是第二个小作业,要求我们使用Fragment,我还一直没接触过这个,所以借鉴着网上大牛们的介绍写了一个小DEMO。
Fragment的出现是因为在不同的屏幕大小下,我们的APP无法同时满足不同大小,所以出现了Fragment。(你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。)(摘自大牛的Blog)
activity_main.xml
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.whisker.fragmenttest.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="36dp"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/tab_down"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NO.1"
android:textColor="#ffffff"/>
</LinearLayout>
<ImageView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#ffffff" />
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/tab"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NO.2"
android:textColor="#ffffff"/>
</LinearLayout>
<ImageView
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#ffffff" />
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/tab"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NO.3"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
MainActivity.java
package com.whisker.fragmenttest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class MainActivity extends FragmentActivity {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>private LinearLayout tabLayout1,tabLayout2,tabLayout3;
<span style="white-space:pre"> </span>private Fragment tabFragment1,tabFragment2,tabFragment3;
<span style="white-space:pre"> </span>private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
tabLayout1 = (LinearLayout) findViewById(R.id.tab1);
tabLayout2 = (LinearLayout) findViewById(R.id.tab2);
tabLayout3 = (LinearLayout) findViewById(R.id.tab3);
clickListener clickListener = new clickListener();
<span style="white-space:pre"> </span>tabLayout1.setOnClickListener(clickListener);
tabLayout2.setOnClickListener(clickListener);
tabLayout3.setOnClickListener(clickListener);
setDefaultFragment();
}
/*初始化默认的Fragment*/
private void setDefaultFragment() {
<span style="white-space:pre"> </span>FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
<span style="white-space:pre"> </span>tabFragment1 = new TabFragment1();
<span style="white-space:pre"> </span>fragmentTransaction.replace(R.id.content_layout, tabFragment1);
<span style="white-space:pre"> </span>fragmentTransaction.commit();
}
public void replaceFragment(Fragment fragment) {
<span style="white-space:pre"> </span>FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
<span style="white-space:pre"> </span>if(!fragment.isAdded()){
<span style="white-space:pre"> </span>fragmentTransaction.replace(R.id.content_layout, fragment);
<span style="white-space:pre"> </span>fragmentTransaction.commit();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else{
<span style="white-space:pre"> </span>fragmentTransaction.show(fragment);
<span style="white-space:pre"> </span>}
}
private class clickListener implements OnClickListener{
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onClick(View v) {
<span style="white-space:pre"> </span>switch (v.getId()) {
<span style="white-space:pre"> </span>case R.id.tab1:
<span style="white-space:pre"> </span>if(tabFragment1 == null)
<span style="white-space:pre"> </span>tabFragment1 = new TabFragment1();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>replaceFragment(tabFragment1);
<span style="white-space:pre"> </span>tabLayout1.setBackgroundColor(getResources().getColor(R.color.tab_down));
<span style="white-space:pre"> </span>tabLayout2.setBackgroundColor(getResources().getColor(R.color.tab));
<span style="white-space:pre"> </span>tabLayout3.setBackgroundColor(getResources().getColor(R.color.tab));
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>case R.id.tab2:
<span style="white-space:pre"> </span>if(tabFragment2 == null)
<span style="white-space:pre"> </span>tabFragment2 = new TabFragment2();
<span style="white-space:pre"> </span>replaceFragment(tabFragment2);
<span style="white-space:pre"> </span>tabLayout2.setBackgroundColor(getResources().getColor(R.color.tab_down));
<span style="white-space:pre"> </span>tabLayout1.setBackgroundColor(getResources().getColor(R.color.tab));
<span style="white-space:pre"> </span>tabLayout3.setBackgroundColor(getResources().getColor(R.color.tab));
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>case R.id.tab3:
<span style="white-space:pre"> </span>if(tabFragment3 == null)
<span style="white-space:pre"> </span>tabFragment3 = new TabFragment3();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>replaceFragment(tabFragment3);
<span style="white-space:pre"> </span>tabLayout1.setBackgroundColor(getResources().getColor(R.color.tab));
<span style="white-space:pre"> </span>tabLayout2.setBackgroundColor(getResources().getColor(R.color.tab));
<span style="white-space:pre"> </span>tabLayout3.setBackgroundColor(getResources().getColor(R.color.tab_down));
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
}
}
TabFragment1.java
package com.whisker.fragmenttest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment1 extends Fragment{
@Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, null);
return view;
}
}
fragment_tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Tab1"
android:textColor="#000000"/>
</LinearLayout>
TabFragment2,TabFragment3同上。
效果图: