MainActivity
package com.xw.fragmentchange;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import com.xw.fragmentchange.fragment.GiftFragment;
import com.xw.fragmentchange.fragment.HomeFragment;
import com.xw.fragmentchange.fragment.OrderFragment;
import com.xw.fragmentchange.fragment.ShareFragement;
/**
* 演示点击标签切换对应的界面
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private FragmentManager manager;
private FragmentTransaction transaction;
private RadioButton rb_home,rb_share,rb_gift,rb_order;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.content_layout,new HomeFragment());
transaction.commit();
initView();
}
//初始化视图
public void initView(){
rb_home = findViewById(R.id.rb_home);
rb_share = findViewById(R.id.rb_share);
rb_gift = findViewById(R.id.rb_gift);
rb_order = findViewById(R.id.rb_order);
rb_home.setOnClickListener(this);
rb_share.setOnClickListener(this);
rb_gift.setOnClickListener(this);
rb_order.setOnClickListener(this);
}
/**
* 点击RadioButton触发的事件
*/
@Override
public void onClick(View view) {
transaction = manager.beginTransaction();
switch (view.getId()){
case R.id.rb_home:
transaction.replace(R.id.content_layout,new HomeFragment());
break;
case R.id.rb_share:
transaction.replace(R.id.content_layout,new ShareFragement());
break;
case R.id.rb_gift:
transaction.replace(R.id.content_layout,new GiftFragment());
break;
case R.id.rb_order:
transaction.replace(R.id.content_layout,new OrderFragment());
break; }
transaction.commit();
}
}
HomeFragment(其余三个fragment名字不同,其余代码相同)
package com.xw.fragmentchange.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.xw.fragmentchange.R;
/**
* Created by Administrator on 2018/4/27.
*/
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home,null);
}
}
布局文件:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xw.fragmentchange.MainActivity">
<!--展示内容界面-->
<LinearLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"></LinearLayout>
<!--展示切换标签-->
<LinearLayout
android:id="@+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ffffff"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<RadioGroup
android:id="@+id/rg_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:button="@null"
android:drawableTop="@drawable/home_item_top"
android:drawablePadding="10dp"
android:gravity="center"
android:text="首页"
android:textColor="#B3B3B3"
android:textSize="15sp"
/>
<RadioButton
android:id="@+id/rb_share"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:button="@null"
android:drawableTop="@drawable/home_item_top"
android:drawablePadding="10dp"
android:gravity="center"
android:text="手机专享"
android:textColor="#B3B3B3"
android:textSize="15sp"
/>
<RadioButton
android:id="@+id/rb_gift"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:button="@null"
android:drawableTop="@drawable/home_item_top"
android:drawablePadding="10dp"
android:gravity="center"
android:text="未来礼品"
android:textColor="#B3B3B3"
android:textSize="15sp"
/>
<RadioButton
android:id="@+id/rb_order"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:button="@null"
android:drawableTop="@drawable/home_item_top"
android:drawablePadding="10dp"
android:gravity="center"
android:text="我的订单"
android:textColor="#B3B3B3"
android:textSize="15sp"
/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
fragment_home.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">
<TextView
android:id="@+id/tv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="首页"
android:textColor="#aa0000"
android:textSize="30sp" />
</RelativeLayout>