1,主页面

<?xml version="1.0" encoding="utf-8"?><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="com.sky.com.vegetables.MainActivity">    <FrameLayout        android:id="@+id/fragment"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1">    FrameLayout>    <RadioGroup        android:id="@+id/tabs"        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="#fff"        android:gravity="center"        android:orientation="horizontal">        <RadioButton            android:id="@+id/home_rb"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:checked="true"            android:gravity="center"            android:text="首页" />        <RadioButton            android:id="@+id/my_rb"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:gravity="center"            android:text="我的" />            RadioGroup>LinearLayout>

2,两个Fragment XML布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="我的"        android:textColor="@color/colorPrimary"        />LinearLayout>

3,Main 代码

package com.sky.com.vegetables;import android.os.Bundle;import android.support.annotation.IdRes;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v7.app.AppCompatActivity;import android.widget.FrameLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import com.sky.com.vegetables.fragment.HomeFragment;import com.sky.com.vegetables.fragment.MyFragment;import butterknife.Bind;import butterknife.ButterKnife;public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {    @Bind(R.id.fragment)    FrameLayout mFragment;    @Bind(R.id.home_rb)    RadioButton mHome;    @Bind(R.id.my_rb)    RadioButton mMy;    @Bind(R.id.tabs)    RadioGroup mTabs;    private Fragment mHomeFragment, mMyFragment//两个需要显示的页面    private  Fragment  isFragment;// 当前显示的    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        initView();        FragmentManager fragmentManager = getSupportFragmentManager();        fragmentManager.beginTransaction().add(R.id.fragment, mHomeFragment).commit();        isFragment = mHomeFragment;    }    private void initView() {        mTabs.setOnCheckedChangeListener(this);       //添加多个的话就在这new,下面点击事件,也写上。        mHomeFragment = new HomeFragment();        mMyFragment = new MyFragment();    }    //点击切换。    @Override    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {        switch (i) {            case R.id.home_rb:                switchFragment(mHomeFragment);                break;            case R.id.my_rb:                switchFragment(mMyFragment);                break;        }    }    private void switchFragment(Fragment fragment) {        //判断当前显示的Fragment是不是切换的Fragment        if (isFragment != fragment) {            //判断切换的Fragment是否已经添加过            if (!fragment.isAdded()) {                //如果没有,则先把当前的Fragment隐藏,把切换的Fragment添加上                getSupportFragmentManager().beginTransaction().hide(isFragment)                        .add(R.id.fragment, fragment).commit();            } else {                //如果已经添加过,则先把当前的Fragment隐藏,把切换的Fragment显示出来                getSupportFragmentManager().beginTransaction().hide(isFragment).show(fragment).commit();            }            isFragment = fragment;        }    }}

4,Fragment 代码

package com.sky.com.vegetables.fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.FrameLayout;import com.sky.com.vegetables.R;/** * Created by ${CWJ} on 2017/7/27. * 描述: 个人中心 */public class MyFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View v=inflater.inflate(R.layout.fragment_my,container,false);        //初始化 什么的 放这里。        return v;    }}

更多相关文章

  1. android 蓝牙打印程序源代码
  2. [Android]代码实现StateListDrawable
  3. Android仿计算器布局代码
  4. linux和windows平台下下载android sdk的源代码【Z】
  5. Android修改源代码控制不锁屏
  6. Android 入门第八讲02-WebView的高级用法(Android调用 JS 代码( lo
  7. 在eclipse中查看android SDK的源代码
  8. Android彻底组件化—代码和资源隔离
  9. 获取Android的Java源代码并在Eclipse中关联查看的最新方法

随机推荐

  1. Android对话框使用详解(一)
  2. android 中定時器的幾種用法總結
  3. Android实现选择相册图片并显示功能
  4. Android:如何从堆栈中还原ProGuard混淆后
  5. Android之照相机的使用
  6. Android中Toast的具体使用
  7. Android Framework启动流程分析(一)
  8. android view组件draw onDraw,dispatchDra
  9. android 7.0 手机调用手机相机、相册常见
  10. Android Studio详细安装教程