碎片fragment的使用案例

碎片fragment的使用:

/*
TabHost已经不推荐使用,而使用fragment来实现
设置碎片为全局变量,避免了每次切换显示的时候需要删除和重新创建对象
 */
public class MainActivity extends Activity{
    private FragmentManager fragmentManager;
    private FragmentOne fragmentOne;
    private FragmentTwo fragmentTwo;
    private FragmentThree fragmentThree;
    /**
     * 记录当前显示的碎片
     */
    private Fragment fragmentCurrent;
    /**
     * 记录当前点击的文本
     */
    private TextView curretTextView;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //让程序启动先显示第一个fragment
        fragmentManager = getFragmentManager();
        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
        beginTransaction.add(R.id.main_fragment,fragmentOne);
        beginTransaction.commit();
        fragmentCurrent=fragmentOne;//记录当前显示的fragment
        curretTextView=tv1;//记录当前点击的文本
        curretTextView.setBackgroundColor(Color.parseColor("#ffcccc"));
    }
    /**
     * 初始化控件
     */
    private void initView(){
        tv1 = (TextView) findViewById(R.id.main_textView1);
        tv2 = (TextView) findViewById(R.id.main_textView2);
        tv3 = (TextView) findViewById(R.id.main_textView3);
        fragmentOne = new FragmentOne();
        //创建第二个和第三个碎片
        fragmentTwo = new FragmentTwo();
           fragmentThree = new FragmentThree();
    }
    /**
     * TextView的单击事件
     */
    public void textOnClick(View v){
        switch (v.getId()) {
        case R.id.main_textView1://单击的奥运会
            changeColor(tv1);//改变颜色,tv1代表点击的
            switchFragment(fragmentOne);
            break;
        case R.id.main_textView2://点击的王宝强
            changeColor(tv2);
            switchFragment(fragmentTwo);
            break;
        case R.id.main_textView3://点击的大黑牛
            changeColor(tv3);
            switchFragment(fragmentThree);
            break;
        }
    }
    /**
     *切换fragment的显示 
     */
    public void switchFragment(Fragment newFragment){
        FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
        //判断这个要显示的fragment添没添加在管理器中
        if(newFragment.isAdded()){
            beginTransaction.hide(fragmentCurrent);//隐藏掉当前的显示
            beginTransaction.show(newFragment);//存在就直接显示
        }else{//没添加就添加
            beginTransaction.add(R.id.main_fragment, newFragment);
        }
        fragmentCurrent=newFragment;//记录现在显示的,方便下一次的隐藏
        beginTransaction.commit();
    }
    /**
     * 改变点击后显示的颜色
     */
    public void changeColor(TextView newTextView){
        curretTextView.setBackgroundColor(Color.parseColor("#cc33cc"));//变为未选中的颜色
        newTextView.setBackgroundColor(Color.parseColor("#ffcccc"));//设置为选中的颜色
        curretTextView=newTextView;//标记
    }
}

第一个碎片:FragmentOne.java:

public class FragmentOne extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1_layout, null);
    }
}

第二个碎片:FragmentTwo.java:

public class FragmentTwo extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //为fragment设置显示的布局
        return inflater.inflate(R.layout.fragment2_layout, null);
    }
}

第三个碎片:FragmentThree.java

public class FragmentThree extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //为fragment设置显示的布局
        return inflater.inflate(R.layout.fragment3_layout, null);
    }
}

主布局 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:background="#cccccc"
    android:orientation="vertical"
    tools:context=".MainActivity" >
<!-- 动态添加fragment,将碎片添加到布局中 -->
    <!-- 存放fragment -->
    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
   
 <!-- 显示activity的布局,最下面的选择条 -->
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
      <!-- 第一个选项 -->
        <TextView 
            android:id="@+id/main_textView1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="奥运会"
            android:padding="5dp"
            android:gravity="center"
            android:textSize="25sp"
            android:clickable="true"
            android:onClick="textOnClick"
            android:selectAllOnFocus="true"
               android:background="#cc33cc"/>
       <!-- 第二个选项 -->
        <TextView 
            android:id="@+id/main_textView2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="王宝强"
            android:padding="5dp"
            android:onClick="textOnClick"
            android:gravity="center"
            android:textSize="25sp"
            android:clickable="true"
            android:background="#cc33cc"/>
       <!-- 第三个选项 -->
        <TextView 
            android:id="@+id/main_textView3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="大黑牛"
            android:onClick="textOnClick"
            android:padding="5dp"
            android:gravity="center"
            android:textSize="25sp"
            android:clickable="true"
            android:background="#cc33cc"/>
        
    </LinearLayout>
   
</LinearLayout>

第一个fragment显示的布局文件: fragment1_layout.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:background="#ccffff"
    android:orientation="vertical" >
  <!-- 第一个碎片显示的布局 -->
    <TextView 
        android:id="@+id/fragment1_textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center"
        android:text="中国又夺冠了......"/>
</LinearLayout>

第二个fragment显示的布局文件: fragment2_layout.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:background="#9999ff"
    android:orientation="vertical" >
    
<!-- 第二个碎片显示的布局 -->
    <TextView 
        android:id="@+id/fragment2_textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center"
        android:text="王宝强好惨啊......"/>
</LinearLayout>

第三个fragment显示的布局文件: fragment3_layout.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:background="#ffcccc"
    android:orientation="vertical" >
    
  <!-- 第三个碎片显示的布局 -->
    <TextView 
        android:id="@+id/fragment3_textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center"
        android:text="老黑牛来了......"/>
</LinearLayout>

效果如下:

posted @ 2016-08-16 21:29  ts-android  阅读(518)  评论(0编辑  收藏  举报