Fragment+Framelayout 小复习 实现简单的 tab

把基础的都快忘了 汗


有时候一些需求需要很多的页面  一般的处理是

1.一些页面放到一个activitiy 缺点:业务逻辑和页面处理逻辑会在乱不堪

2.一个页面一个activitiy 缺点:增大activitiy的开销


也是在api 11以上有了fragment 


具体做法如下:


在布局中放一个盛放fragment的容器 这里这容器是framlayout

<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="a"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="b"
            android:text="Button" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/myframe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </FrameLayout>

</LinearLayout>

然后

在fragmentTransaction处理替换这个View 以达到显示多个页面的目的 而处理逻辑交个FragmentA b c d就OK


package com.example.testfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

public class MainActivity extends FragmentActivity {

	private FragmentTransaction fragmentTransaction;
	private FragmentManager fragmentManager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		fragmentManager = getSupportFragmentManager();

	}

	public void a(View view) {
		AFragment fragment = new AFragment();
		fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.myframe, fragment);
		fragmentTransaction.commit();
	}

	public void b(View view) {
		BFragment fragment = new BFragment();
		fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.myframe, fragment);
		fragmentTransaction.commit();
	}

}

package com.example.testfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class AFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		TextView textView = new TextView(getActivity());
		textView.setText("Fragment A");
		return textView;
	}
}

package com.example.testfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BFragment  extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		TextView textView = new TextView(getActivity());
		textView.setText("Fragment B");
		return textView;
	}
}

TestFragment.rar

890.62 KB, 下载次数: 0, 下载积分: e币 -2 元


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值