青玄 发表于 2014-11-22 20:57:22

[原创]android学习之fragmentTab

本帖最后由 青玄 于 2014-11-22 21:00 编辑

本案例主要实现的是不同页面之间的切换操作,其实就是在原有的界面上进行一个界面的替换操作而已,本来activity可以实现这个功能,但是太多的activity使得程序过于臃肿,所以用fragment进行切换,可以减少这个弊端;
一下是主要实现的步骤,当然只是一部分而已:
1. 开始出现的界面:main_activity.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"
    tools:context=".MainActivity" >

    <fragment
      android:id="@+id/tab"2. TabFragment.java文件主要实现加载标题栏的布局文件:
package com.example.tabfragment_test;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TabFragment extends Fragment {

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                View view = inflater.inflate(R.layout.fragment_tab, null);
                return view;
      }

      
}

3.加载标题栏的布局文件:fragment_tab.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="#55ff66"
    android:gravity="center"
    android:orientation="horizontal"
   
    tools:context=".MainActivity" >

    <TextView
      android:gravity="center"
      android:layout_weight="1"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:id="@+id/tv1"
      android:text="小甲鱼"
         />
   
   <TextView
      android:gravity="center"
      android:layout_weight="1"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:id="@+id/tv2"
      android:text="小白马"
         />
   
      <TextView
      android:gravity="center"
      android:layout_weight="1"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:id="@+id/tv3"
      android:text="小仙"
         />
      
       <TextView
      android:gravity="center"
      android:layout_weight="1"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:id="@+id/tv4"
      android:text="wiliam"
         />
</LinearLayout>
4.在MainActivity.java主程序中进行监听切换操作:
package com.example.tabfragment_test;

import com.cbd.tabfragment.Fragment1;
import com.cbd.tabfragment.Fragment2;
import com.cbd.tabfragment.Fragment3;
import com.cbd.tabfragment.Fragment4;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.graphics.Color;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

      private TextView tv1;
      private FragmentManager manager;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
               
                this.tv1 = (TextView) this.findViewById(R.id.tv1);

                //对这个textView 进行监听
                tv1.setOnClickListener(this);

                manager = this.getFragmentManager();    //获得fragment管理器
      }

      @Override
      public void onClick(View v) {
                // TODO Auto-generated method stub
                FragmentTransaction ft = manager.beginTransaction();   //得到这个对象调用replace方法进行替换操作
               
                switch(v.getId())
                {
                case R.id.tv1:
                        ft.replace(R.id.content, new Fragment1());
                        tv1.setBackgroundColor(Color.BLUE);
                        tv2.setBackgroundColor(34757495);
                        tv3.setBackgroundColor(57891292);
                        tv4.setBackgroundColor(46897932);
                        break;
                }
                ft.commit();   //提交
      }

}




拈花小仙 发表于 2014-11-22 21:37:32

{:7_139:}谢谢 小白马玄玄~

拈花小仙 发表于 2014-11-22 21:38:03

{:7_139:}帮我这论坛第一灌水户也出出名哈~

拈花小仙 发表于 2014-11-22 21:39:20

@machimilk @百日维新 @Angel丶L

百日维新 发表于 2014-11-22 21:41:03

强烈支持楼主ing,看来我不过用功

青玄 发表于 2014-11-23 14:14:35

拈花小仙 发表于 2014-11-22 21:37
谢谢 小白马玄玄~

呵呵! 小仙!咱两谁跟谁啊!写程序的时候,实在想不出什么来!就想到你们了!以前写的程序也是这样的!{:5_109:}

青玄 发表于 2014-11-23 14:15:16

拈花小仙 发表于 2014-11-22 21:38
帮我这论坛第一灌水户也出出名哈~

{:5_95:}

拈花小仙 发表于 2014-11-23 14:26:54

百日维新 发表于 2014-11-22 21:41
强烈支持楼主ing,看来我不过用功

新新能不能也做个实例,加上偶啦啦~
页: [1]
查看完整版本: [原创]android学习之fragmentTab