Android Fragment简单使用

一个Fragment的简单实例,代码比较粗糙,简单记录一下:

XML文件:
main.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" >

    <FrameLayout
        android:id="@+id/fl_fragment_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#f5f5f5" >

        <!-- 存放5个Fragment -->
    </FrameLayout>
    	<!-- 底部的5个选项菜单 -->
	<View
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:background="#D1D1D1" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="49dp"
          >

        <!-- 5个部分都一样:ImageView + TextView -->

        <RelativeLayout
            android:id="@+id/rl_first_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:id="@+id/iv_sy"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/homepg"/>

            <TextView
                android:id="@+id/tv_sy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iv_sy"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="2dp"
                android:textSize="11sp"
                android:text="首页" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_second_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:id="@+id/iv_kq"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/attendancepg" />

            <TextView
                android:id="@+id/tv_kq"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iv_kq"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:textSize="11sp"
                android:layout_marginTop="2dp"
                android:text="考勤" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_third_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:id="@+id/iv_gz"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/salarypg" />

            <TextView
                android:id="@+id/tv_gz"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iv_gz"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:textSize="11sp"
                android:layout_marginTop="2dp"
                android:text="工资" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_four_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:id="@+id/iv_zgz"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/findjobpg"/>

            <TextView
                android:id="@+id/tv_zgz"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iv_zgz"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:textSize="11sp"
                android:layout_marginTop="2dp"
                android:text="找工作" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rl_five_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:id="@+id/iv_wd"
                android:layout_width="22dp"
                android:layout_height="22dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/minepg" />

            <TextView
                android:id="@+id/tv_wd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iv_wd"
                android:layout_centerHorizontal="true"
                android:layout_gravity="center_horizontal"
                android:textSize="11sp"
                android:layout_marginTop="2dp"
                android:text="我的" />
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

附上一个高亮显示的代码(就是两张相同的图片但颜色不同),其他的同样:
homepg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/homepage_selected" />
    <item android:state_selected="false" android:drawable="@drawable/homepage" />
</selector>
--------------------- 

Java文件:
MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
	//底部五个按钮
	private RelativeLayout mRlFirstLayout; 
    private RelativeLayout mRlSecondLayout;
    private RelativeLayout mRlThirdLayout;
    private RelativeLayout mRlFourLayout;
    private RelativeLayout mRlFiveLayout;
    //五个fragment
    private Fragment_gz fragment_gz;
    private Fragment_kq fragment_kq;
    private Fragment_sy fragment_sy;
    private Fragment_wd fragment_wd;
    private Fragment_zgz fragment_zgz;
    //fragment事务管理
    private FragmentManager mFragmentManager;
    private FragmentTransaction mTransaction;
    private FrameLayout mFlFragmentContent;
    
    public static Loading_data loading;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if(LoginActivity.Person==null){
			Intent i = new Intent(MainActivity.this, LoginActivity.class);
			startActivity(i);
		}
        mRlFirstLayout=(RelativeLayout) findViewById(R.id.rl_first_layout);
        mRlSecondLayout=(RelativeLayout) findViewById(R.id.rl_second_layout);
        mRlThirdLayout=(RelativeLayout) findViewById(R.id.rl_third_layout);
        mRlFourLayout=(RelativeLayout) findViewById(R.id.rl_four_layout);
        mRlFiveLayout=(RelativeLayout) findViewById(R.id.rl_five_layout);
        
        mRlFirstLayout.setOnClickListener(this);
        mRlSecondLayout.setOnClickListener(this);
        mRlThirdLayout.setOnClickListener(this);
        mRlFourLayout.setOnClickListener(this);
        mRlFiveLayout.setOnClickListener(this);
        
        //默认第一个首页被选中高亮显示
        mRlFirstLayout.setSelected(true);
        mFragmentManager=getFragmentManager();
        mTransaction = mFragmentManager.beginTransaction();
        mTransaction.replace(R.id.fl_fragment_content, fragment_sy=new Fragment_sy());
        mTransaction.commit();
        loding(null);
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		mTransaction = mFragmentManager.beginTransaction(); //开启事务
        hideAllFragment(mTransaction);
        switch (v.getId()){
            //首页
            case R.id.rl_first_layout:
                seleted();
                mRlFirstLayout.setSelected(true);
                if (fragment_sy == null) {
                	fragment_sy = new Fragment_sy();
                    mTransaction.add(R.id.fl_fragment_content,fragment_sy);
                }else{
                    mTransaction.show(fragment_sy);
                }
                break;
            //考勤
            case R.id.rl_second_layout:
                seleted();
                mRlSecondLayout.setSelected(true);
                if (fragment_kq == null) {
                	fragment_kq = new Fragment_kq();
                    mTransaction.add(R.id.fl_fragment_content,fragment_kq);     
                }else{
                    mTransaction.show(fragment_kq);
                }
                break;
            //工资
            case R.id.rl_third_layout:
                seleted();
                mRlThirdLayout.setSelected(true);
                if (fragment_gz == null) {
                	fragment_gz = new Fragment_gz();
                    mTransaction.add(R.id.fl_fragment_content,fragment_gz);     
                }else{
                    mTransaction.show(fragment_gz);
                }
                break;
            //找工作
            case R.id.rl_four_layout:
                seleted();
                mRlFourLayout.setSelected(true);
                if (fragment_zgz == null) {
                	fragment_zgz = new Fragment_zgz();
                    mTransaction.add(R.id.fl_fragment_content,fragment_zgz);     
                }else{
                    mTransaction.show(fragment_zgz);
                }
                break;
            //我的
            case R.id.rl_five_layout:
                seleted();
                mRlFiveLayout.setSelected(true);
                if (fragment_wd == null) {
                	fragment_wd = new Fragment_wd();
                    mTransaction.add(R.id.fl_fragment_content,fragment_wd);     
                }else{
                    mTransaction.show(fragment_wd);
                }
                break;
        }
        mTransaction.commit();
	}
    
	//设置所有按钮都是默认都不选中
    private void seleted() {
        mRlFirstLayout.setSelected(false);
        mRlSecondLayout.setSelected(false);
        mRlThirdLayout.setSelected(false);
        mRlFourLayout.setSelected(false);
        mRlFiveLayout.setSelected(false);
    }

    //删除所有Fragment
    private void hideAllFragment(FragmentTransaction transaction) {
        if (fragment_gz != null) {
            transaction.hide(fragment_gz);
        }
        if (fragment_kq != null) {
            transaction.hide(fragment_kq);
        }
        if (fragment_sy != null) {
            transaction.hide(fragment_sy);
        }
        if (fragment_wd != null) {
            transaction.hide(fragment_wd);
        }
        if (fragment_zgz != null) {
            transaction.hide(fragment_zgz);
        }
    }
}

实现的效果:
在这里插入图片描述
单击下面的按钮就可以切换了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值