布局========================================================================
mian_actitvity================================================================
xml version="1.0" encoding="utf-8"?>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.daydayup.myjddemo.view.activities.MainActivity"> android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> layout="@layout/divider1" /> android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> android:id="@+id/main_sy" android:layout_width="0dp" android:layout_height="@dimen/nav_width" android:layout_weight="1" android:background="@drawable/main_nav_sy_selector" /> android:id="@+id/main_fl" android:layout_width="0dp" android:layout_height="@dimen/nav_width" android:layout_weight="1" android:background="@drawable/main_nav_fl_selector" /> android:id="@+id/main_fx" android:layout_width="0dp" android:layout_height="@dimen/nav_width" android:layout_weight="1" android:background="@drawable/main_nav_fx_selector" /> android:id="@+id/main_gwc" android:layout_width="0dp" android:layout_height="@dimen/nav_width" android:layout_weight="1" android:background="@drawable/main_nav_gwc_selector" /> android:id="@+id/main_wd" android:layout_width="0dp" android:layout_height="@dimen/nav_width" android:layout_weight="1" android:background="@drawable/main_nav_wd_selector" />
divider=======================================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/divider_height" android:background="#999999" android:orientation="vertical">
layout_fl===================================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="分类" android:textSize="30sp" />
layout_fx=====================================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="发现" android:textSize="30sp" />
layout_gwc================================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="首页" android:textSize="30sp" />
layout_spalsh=================================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> android:id="@+id/splash_pic" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/b1i" /> android:layout_margin="@dimen/margin5" android:gravity="center" android:textSize="@dimen/textSize20" android:layout_alignParentRight="true" android:background="@drawable/spash_time_shape" android:id="@+id/spalsh_time" android:layout_width="wrap_content" android:layout_height="wrap_content" />
layout_sy=====================================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="首页" android:textSize="30sp" />
layout_wd=================================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="我的" android:textSize="30sp" />
按钮切换====================================================================
main_nav_fl_selector==========================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"> - android:drawable="@drawable/abx" android:state_selected="true" />
- android:drawable="@drawable/abw" android:state_selected="false" />
main_nav_fx_selector==========================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"> - android:drawable="@drawable/abz" android:state_selected="true" />
- android:drawable="@drawable/aby" android:state_selected="false" />
main_nav_gwc_selector==========================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"> - android:drawable="@drawable/abv" android:state_selected="true" />
- android:drawable="@drawable/abu" android:state_selected="false" />
main_nav_sy_selector==========================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"> - android:drawable="@drawable/ac1" android:state_selected="true" />
- android:drawable="@drawable/ac0" android:state_selected="false" />
main_nav_wd_selector========================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"> - android:drawable="@drawable/ac3" android:state_selected="true" />
- android:drawable="@drawable/ac2" android:state_selected="false" />
spash_tiem_shape==============================================================
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> android:width="50dp" android:height="30dp" /> android:radius="15dp" /> android:color="#66999999" />
功能=======================================================================
MainActivity================================================================
package com.daydayup.myjddemo.view.activities; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.TextView; import com.daydayup.myjddemo.R; import com.daydayup.myjddemo.view.fragments.BaseFragment; import com.daydayup.myjddemo.view.fragments.FragmentFactory; import java.util.ArrayList; import java.util.List; /** * Fragment 容器 FrameLayout */ public class MainActivity extends BaseActivity implements View.OnClickListener { private final int SY = 0, FL = 1, FX = 2, GWC = 3, WD = 4; private View view; private TextView main_sy, main_fl, main_fx, main_gwc, main_wd; private Listnav_list = null; private List fg_list = null; private FragmentManager fm; @Override protected void initDatas() { fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); fg_list = new ArrayList<>(); for (int i = 0; i < 5; i++) { BaseFragment fragement = FragmentFactory.createFragement(i); fg_list.add(fragement); ft.add(R.id.main_content, fragement); if (i == 0) { ft.show(fragement); } else { ft.hide(fragement); } } ft.commit(); //底部导航栏 } @Override protected void initViews() { main_sy = findViewById(R.id.main_sy); main_fl = findViewById(R.id.main_fl); main_fx = findViewById(R.id.main_fx); main_gwc = findViewById(R.id.main_gwc); main_wd = findViewById(R.id.main_wd); nav_list = new ArrayList<>(); nav_list.add(main_sy); nav_list.add(main_fl); nav_list.add(main_fx); nav_list.add(main_gwc); nav_list.add(main_wd); //默认第一个选中 nav_list.get(0).setSelected(true); main_sy.setOnClickListener(this); main_fl.setOnClickListener(this); main_fx.setOnClickListener(this); main_gwc.setOnClickListener(this); main_wd.setOnClickListener(this); } @Override View createView() { view = View.inflate(this, R.layout.activity_main, null); return view; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.main_sy: changeNav(nav_list, SY); changeFragment(fg_list, SY); break; case R.id.main_fl: changeNav(nav_list, FL); changeFragment(fg_list, FL); break; case R.id.main_fx: changeNav(nav_list, FX); changeFragment(fg_list, FX); break; case R.id.main_gwc: changeNav(nav_list, GWC); changeFragment(fg_list, GWC); break; case R.id.main_wd: changeNav(nav_list, WD); changeFragment(fg_list, WD); break; } } //切换状态 public void changeNav(List nav_list, int postion) { for (int i = 0; i < nav_list.size(); i++) { TextView textView = nav_list.get(i); if (postion == i) { textView.setSelected(true); } else { textView.setSelected(false); } } } //切换页面 public void changeFragment(List fg_list, int postion) { FragmentTransaction ft = fm.beginTransaction(); for (int i = 0; i < fg_list.size(); i++) { BaseFragment baseFragment = fg_list.get(i); if (postion == i) { ft.show(baseFragment); } else { ft.hide(baseFragment); } } ft.commit(); } }
BeanActivity================================================================
package com.daydayup.myjddemo.view.activities; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; import android.view.View; import android.view.Window; /** * Created by gjl on 2018/4/10. * * 基类: * 继承:增加代码的复用性 * 多态:同一事物在不同状态下的不同形态。可扩展性。 * * * */ public abstract class BaseActivity extends FragmentActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { //去掉标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(createView()); //初始化界面 initViews(); //初始化数据 initDatas(); } protected abstract void initDatas(); protected abstract void initViews(); abstract View createView(); }
SplashActivity==================================================================
package com.daydayup.myjddemo.view.activities; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.daydayup.myjddemo.R; /** * Created by gjl on 2018/4/10. ** Handler 的面试题 */ public class SplashActivity extends BaseActivity { private View view; private ImageView splash_pic; private TextView spalsh_time; private MyHandler myHandler = new MyHandler(); private int time = 3; @Override protected void initDatas() { //默认值 spalsh_time.setText(time + "s"); myHandler.sendEmptyMessageDelayed(0, 1000); } class MyHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: time--; spalsh_time.setText(time + "s"); if (time == 0) { myHandler.removeCallbacksAndMessages(null); //跳转 startActivity(new Intent(SplashActivity.this, MainActivity.class)); } else { myHandler.sendEmptyMessageDelayed(0, 1000); } break; } } } //初始化控件 @Override protected void initViews() { splash_pic = findViewById(R.id.splash_pic); spalsh_time = findViewById(R.id.spalsh_time); } //创建视图 @Override View createView() { view = View.inflate(this, R.layout.layout_spalsh, null); return view; } }
BeanFragment=============================================
package com.daydayup.myjddemo.view.fragments; 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 gjl on 2018/4/10. ** Fragment? * Activity *
* 生命周期 */ public abstract class BaseFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return createView(inflater); } abstract View createView(LayoutInflater inflater); }
FaXianFragment==============================================================
package com.daydayup.myjddemo.view.fragments; import android.view.LayoutInflater; import android.view.View; import com.daydayup.myjddemo.R; /** * Created by gjl on 2018/4/10. */ public class FaXianFragment extends BaseFragment { private View view; //页面 @Override View createView(LayoutInflater inflater) { view = inflater.inflate(R.layout.layout_fx,null); return view; } }
FenLeiFragment================================================================
package com.daydayup.myjddemo.view.fragments; import android.view.LayoutInflater; import android.view.View; import com.daydayup.myjddemo.R; /** * Created by gjl on 2018/4/10. */ public class FenLeiFragment extends BaseFragment { private View view; //页面 @Override View createView(LayoutInflater inflater) { view = inflater.inflate(R.layout.layout_fl,null); return view; } }
FragmentFactory==============================================================
package com.daydayup.myjddemo.view.fragments; /** * Created by gjl on 2018/4/10. ** 根据不同的需求创建不同的Fragment */ public class FragmentFactory { public static BaseFragment createFragement(int type) { BaseFragment baseFragment = null; switch (type) { case 0: baseFragment = new ShouYeFragment(); break; case 1: baseFragment = new FenLeiFragment(); break; case 2: baseFragment = new FaXianFragment(); break; case 3: baseFragment = new GouWuCheFragment(); break; case 4: baseFragment = new WoDeFragment(); break; } return baseFragment; } }
GouWuCheFragmnet=============================================================
package com.daydayup.myjddemo.view.fragments; import android.view.LayoutInflater; import android.view.View; import com.daydayup.myjddemo.R; /** * Created by gjl on 2018/4/10. */ public class GouWuCheFragment extends BaseFragment { private View view; //页面 @Override View createView(LayoutInflater inflater) { view = inflater.inflate(R.layout.layout_gwc,null); return view; } }
ShouYeFragment=======================================================
package com.daydayup.myjddemo.view.fragments; import android.view.LayoutInflater; import android.view.View; import com.daydayup.myjddemo.R; /** * Created by gjl on 2018/4/10. */ public class ShouYeFragment extends BaseFragment { private View view; //页面 @Override View createView(LayoutInflater inflater) { view = inflater.inflate(R.layout.layout_sy,null); return view; } }
WoDeFragment========================================================
package com.daydayup.myjddemo.view.fragments; import android.view.LayoutInflater; import android.view.View; import com.daydayup.myjddemo.R; /** * Created by gjl on 2018/4/10. */ public class WoDeFragment extends BaseFragment { private View view; //页面 @Override View createView(LayoutInflater inflater) { view = inflater.inflate(R.layout.layout_wd,null); return view; } }
清单文件=====================================================================
android:name=".view.activities.SplashActivity">