<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.zjly.androidkuangjia.MainActivity"> <include layout="@layout/title_layout" /> <FrameLayout android:id="@+id/frame_main" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <RadioGroup android:id="@+id/rg_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ccc" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_home" style="@style/RadioButton_Style" android:drawableTop="@drawable/home_drawabel_rb" android:text="首页" /> <RadioButton android:id="@+id/rb_recycle" style="@style/RadioButton_Style" android:drawableTop="@drawable/recycle_drawabel_rb" android:text="回收" /> <RadioButton android:id="@+id/rb_huanwu" style="@style/RadioButton_Style" android:drawableTop="@drawable/huanwu_drawabel_rb" android:text="换物" /> <RadioButton android:id="@+id/rb_mine" style="@style/RadioButton_Style" android:drawableTop="@drawable/mine_drawabel_rb" android:text="我的" /> </RadioGroup> </LinearLayout>
android官方文档,Fragment包在FrameLayout中,建议用FrameLayout;
<?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="50dp" android:background="@color/colorGreen" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android框架基本搭建" android:textColor="#fff" android:textSize="18sp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_home" android:state_checked="false" /> <item android:drawable="@drawable/icon_home_checked" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_huanwu" android:state_checked="false" /> <item android:drawable="@drawable/icon_huanwu_checked" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_mine" android:state_checked="false" /> <item android:drawable="@drawable/icon_mine_cheched" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#000" android:state_checked="false" /> <item android:color="@color/colorGreen" android:state_checked="true" /> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_recycle" android:state_checked="false" /> <item android:drawable="@drawable/icon_recycle_checked" android:state_checked="true" /> </selector>
package com.zjly.androidkuangjia; import android.os.Bundle; import android.support.annotation.IdRes; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.widget.FrameLayout; import android.widget.RadioGroup; import java.util.ArrayList; import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; public class MainActivity extends FragmentActivity { @Bind(R.id.frame_main) FrameLayout frameMain; @Bind(R.id.rg_main) RadioGroup rgMain; private List<Fragment> fragments; private int position; private Fragment mContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initView(); initEvent(); rgMain.check(R.id.rb_home); } private void initEvent() { rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { switch (i) { case R.id.rb_home: position = 0; break; case R.id.rb_recycle: position = 1; break; case R.id.rb_huanwu: position = 2; break; case R.id.rb_mine: position = 3; break; default: position = 0; break; } Fragment toFragment = getFragment(); switchFragment(mContent,toFragment); } }); } private void switchFragment(Fragment fromFragment,Fragment toFragment) { if (fromFragment != toFragment) { mContent=toFragment; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //判断有没有被添加 if (!toFragment.isAdded()) { if (fromFragment != null) { transaction.hide(fromFragment); } if (toFragment != null) { transaction.add(R.id.frame_main, toFragment).commit(); } } else {//已经被添加过 //隐藏以前的 if (fromFragment != null) { transaction.hide(fromFragment); } //显示新的 if (toFragment != null) { transaction.show(toFragment).commit(); } } } } private Fragment getFragment() { return fragments.get(position); } private void initView() { fragments = new ArrayList<>(); fragments.add(new HomeFragment()); fragments.add(new RecycleFragment()); fragments.add(new HuanWuFragment()); fragments.add(new MineFragment()); } }
package com.zjly.androidkuangjia; import android.content.Context; 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; /** * Created by Administrator on 2017/5/27. */ public abstract class BaseFragment extends Fragment { public Context mContext; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return initView(); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initData(); } protected void initData() { } protected abstract View initView(); }
package com.zjly.androidkuangjia; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; import static android.content.ContentValues.TAG; /** * Created by Administrator on 2017/5/27. */ public class HomeFragment extends BaseFragment { @Override protected View initView() { Log.e(TAG, "HomeFragment初始化 "); TextView tv = new TextView(mContext); tv.setText("HomeFragment"); tv.setTextSize(18); tv.setTextColor(Color.RED); tv.setGravity(Gravity.CENTER); return tv; } @Override protected void initData() { Log.e(TAG, "HomeFragment加载数据 "); super.initData(); } }
package com.zjly.androidkuangjia; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; import static android.content.ContentValues.TAG; /** * Created by Administrator on 2017/5/27. */ public class HuanWuFragment extends BaseFragment { @Override protected View initView() { Log.e(TAG, "HuanWuFragment "); TextView tv = new TextView(mContext); tv.setText("HuanWuFragment"); tv.setTextSize(18); tv.setTextColor(Color.RED); tv.setGravity(Gravity.CENTER); return tv; } @Override protected void initData() { Log.e(TAG, "HuanWuFragment "); super.initData(); } }
package com.zjly.androidkuangjia; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; import static android.content.ContentValues.TAG; /** * Created by Administrator on 2017/5/27. */ public class RecycleFragment extends BaseFragment { @Override protected View initView() { Log.e(TAG, "RecycleFragment "); TextView tv = new TextView(mContext); tv.setText("RecycleFragment"); tv.setTextSize(18); tv.setTextColor(Color.RED); tv.setGravity(Gravity.CENTER); return tv; } @Override protected void initData() { Log.e(TAG, "RecycleFragment "); super.initData(); } }
package com.zjly.androidkuangjia; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.TextView; import static android.content.ContentValues.TAG; /** * Created by Administrator on 2017/5/27. */ public class MineFragment extends BaseFragment { @Override protected View initView() { Log.e(TAG, "MineFragment "); TextView tv = new TextView(mContext); tv.setText("MineFragment"); tv.setTextSize(18); tv.setTextColor(Color.RED); tv.setGravity(Gravity.CENTER); return tv; } @Override protected void initData() { Log.e(TAG, "MineFragment "); super.initData(); } }