【Android】fragment切换的demo

1.文件准备:

①MainActivity.java activity_layout.xml;

②3-4个Fragmentxx.java   fragmentxx.xml;

③一个颜色资源文件,用于按钮切换的展示。

④style文件,同一管理按钮的样式。

2.MainActivity

public class MainActivity extends AppCompatActivity {

    private TextView marketBar;
    private TextView hatBar;
    private TextView messageBar;
    private TextView centerBar;

    Fragment1 giftFragment;
    Fragment2 hatFragment;
    Fragment3 messageFragment;
    Fragment4 centerFragment;

    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        marketBar = (TextView) findViewById(R.id.bar_market);
        hatBar = (TextView) findViewById(R.id.bar_hat);
        messageBar = (TextView) findViewById(R.id.bar_message);
        centerBar = (TextView) findViewById(R.id.bar_center);

        marketBar.setClickable(true);
        hatBar.setClickable(true);
        messageBar.setClickable(true);
        centerBar.setClickable(true);

        setAllNotSelected();
        marketBar.setSelected(true);

        giftFragment = new Fragment1();
        hatFragment = new Fragment2();
        messageFragment = new Fragment3();
        centerFragment = new Fragment4();

        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.main_fragment, giftFragment).commit();

    }


    private void setAllNotSelected() {
        marketBar.setSelected(false);
        hatBar.setSelected(false);
        messageBar.setSelected(false);
        centerBar.setSelected(false);
    }

    public void tabSelect(View v) {
        setAllNotSelected();
        switch (v.getId()) {
            case R.id.bar_market:
                marketBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, giftFragment).commit();
                break;
            case R.id.bar_hat:
                hatBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, hatFragment).commit();
                break;
            case R.id.bar_message:
                messageBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, messageFragment).commit();
                break;
            case R.id.bar_center:
                centerBar.setSelected(true);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, centerFragment).commit();
                break;
            default:
                break;
        }
    }

}

3.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee">


    <LinearLayout
        android:id="@+id/menu_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#ddd"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/bar_market"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项1" />

        <TextView
            android:id="@+id/bar_hat"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项2" />

        <TextView
            android:id="@+id/bar_message"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项3" />

        <TextView
            android:id="@+id/bar_center"
            style="@style/MainBar"
            android:onClick="tabSelect"
            android:text="选项4" />

    </LinearLayout>


    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/menu_bar"></FrameLayout>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ddd" />


</RelativeLayout>

4.其中一个Fragment举例

public class Fragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment1,container,false);
        return v;
    }
}

5.style文件

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="MainBar" >
        <item name="android:layout_weight">1</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:textSize">20dp</item>
        <item name="android:textColor">@color/tab_color_selector</item>
        <item name="android:gravity">center</item>
    </style>

</resources>
6.color文件  tab_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#118bfc" android:state_selected="true"/>
    <item android:color="#333" />
</selector>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fragment表现Activity中用户界面的一个行为或者是一部分。你可以在一个单独的activity上把多个fragment组合成为一个多区域的UI,并且可以在多个activity中再使用。你可以认为fragment是activity的一个模块零件,它有自己的生命周期,接收它自己的输入事件,并且可以在activity运行时添加或者删除。 Fragment必须总是被嵌入到一个activity之中,并且fragment的生命周期直接受其宿主activity的生命周期的影响。例如,一旦activity被暂停,它里面所有的fragment也被暂停,一旦activity被销毁,它里面所有的fragment也被销毁。然而,当activity正在运行时(处于resumed的生命周期状态),你可以单独的操控每个fragment,比如添加或者删除。当你执行这样一项事务时,可以将它添加到后台的一个栈中,这个栈由activity管理着——activity里面的每个后台栈内容实体是fragment发生过的一条事务记录。这个后台栈允许用户通过按BACK键回退一项fragment事务(往后导航)。 当你添加一个fragment作为某个activity布局的一部分时,它就存在于这个activity视图体系内部的ViewGroup之中,并且定义了它自己的视图布局。你可以通过在activity布局文件中声明fragment,用<fragment>元素把fragment插入到activity的布局中,或者是用应用程序源码将它添加到一个存在的ViewGroup中。然而,fragment并不是一个定要作为activity布局的一部分;fragment也可以为activity隐身工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值