講完了網(wǎng)絡(luò)通信模塊和MVP結(jié)構(gòu)。回過頭來再說說基礎(chǔ)頁面的封裝
BaseFragment
BaseFragment和BaseActivity基本是一樣的
在BaseActivity同級目錄下新建
BaseFragment吃靠,直接貼出內(nèi)容
package com.example.burro.demo.appframework.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.burro.demo.appframework.mvp.presenter.BasePresenter;
import com.example.burro.demo.appframework.mvp.view.BaseView;
import com.example.burro.demo.appframework.util.StringUtils;
import com.example.burro.demo.appframework.util.ToastUtils;
import com.example.burro.demo.dataframework.model.BaseResultBean;
import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
* Created by ex.zhong on 2017/9/26.
* BaseFragment
* 一般使用mvp模式的話會(huì)在BaseFragment中進(jìn)行P和V的初始化綁定 butterKnife的綁定 初始方法的設(shè)定
*/
public abstract class BaseFragment<T extends BasePresenter> extends Fragment implements BaseView {
protected T mPresenter;
protected FragmentActivity mContext;
private View mRootView;
private Unbinder mUnbinder;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView = inflater.inflate(getLayout(), container, false);//和 BaseActivity 一樣,layout() 由子類實(shí)現(xiàn)足淆,提供布局巢块。
mUnbinder = ButterKnife.bind(this, mRootView);
ButterKnife.bind(this,mRootView);
mContext = getActivity();
createPresenter();
if (mPresenter != null) mPresenter.attachView(this);
initParams();
initViews();
return mRootView;
}
protected abstract void initViews();
protected abstract void initParams();
protected abstract void createPresenter();
protected abstract int getLayout();
//統(tǒng)一處理錯(cuò)誤信息
public void handleError(BaseResultBean errResult) {
if (errResult == null) return;
if (this == null) return;
//可以分門別類的處理 錯(cuò)誤消息,如session過期巧号,跳轉(zhuǎn)到登錄頁面族奢。其他情況提示即可
ToastUtils.showToast(mContext, errResult.getMsg());
}
@Override
public void onDestroyView() {
if (mPresenter != null) mPresenter.detachView();
if (mUnbinder != null) mUnbinder.unbind();
super.onDestroyView();
}
}
MainActivity,下圖是大致的效果
把MainActivity貼出來的原因丹鸿,是我見過好幾個(gè)項(xiàng)目越走,切換做的太復(fù)雜。各種TextView的顏色設(shè)置靠欢。搞的主頁面亂七八糟廊敌。這種RadioGroup+RadioButton+FrameLayout模式南片,先前老師也這么講過。還有RadioGroup+RadioButton+ViewPager模式庭敦。都差不多疼进。
主要給出MainActivity和activity_main的代碼。其余部分秧廉,請到源碼中查看伞广。
MainActivity
package com.example.burro.demo.appbiz.main.view;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.burro.demo.appbiz.R;
import com.example.burro.demo.appbiz.R2;
import com.example.burro.demo.appbiz.main.MainContract;
import com.example.burro.demo.appbiz.main.MainPresenterImpl;
import com.example.burro.demo.appframework.ui.BaseActivity;
import com.example.burro.demo.appframework.util.ToastUtils;
import com.example.burro.demo.appframework.util.typeface.TypefaceUtils;
import com.example.burro.demo.dataframework.model.BaseResultBean;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnCheckedChanged;
/**
* Created by ex.zhong on 2017/9/27.
*/
public class MainActivity extends BaseActivity<MainPresenterImpl> implements MainContract.View {
@BindView(R2.id.toolbar)
Toolbar mToolbar;
@BindView(R2.id.rbMain0)
RadioButton rbMain0;
@BindView(R2.id.rbMain1)
RadioButton rbMain1;
@BindView(R2.id.rbMain2)
RadioButton rbMain2;
@BindView(R2.id.rbMain3)
RadioButton rbMain3;
private List<Fragment> mFragments;
private FragmentManager mFragmentManager;
private int currentItem = -1;
@Override
protected int initLayoutInflater() {
return R.layout.activity_main;
}
@Override
protected void initViews() {
// 繼續(xù)設(shè)置字體大小
TypefaceUtils.setTextSpanAndTypeface(rbMain0, getString(R.string.icon_home) + "\n" + getString(R.string.home), 25);
TypefaceUtils.setTextSpanAndTypeface(rbMain1, getString(R.string.icon_music) + "\n" + getString(R.string.music), 25);
TypefaceUtils.setTextSpanAndTypeface(rbMain2, getString(R.string.icon_vidio) + "\n" + getString(R.string.vidio), 25);
TypefaceUtils.setTextSpanAndTypeface(rbMain3, getString(R.string.icon_mine) + "\n" + getString(R.string.mine), 25);
//設(shè)置默認(rèn)選中
rbMain0.setChecked(true);
}
@Override
protected void initParams() {
//注意fragments數(shù)量和radioBtn數(shù)量要對應(yīng)
mFragments = new ArrayList<>();
mFragments.add(new Fragment());
mFragments.add(new Fragment());
mFragments.add(new Fragment());
mFragments.add(new Fragment());
mFragmentManager = getSupportFragmentManager();
}
@OnCheckedChanged({R2.id.rbMain0, R2.id.rbMain1, R2.id.rbMain2, R2.id.rbMain3})
public void checkRadioBtn(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
int viewId = buttonView.getId();
if (viewId == R.id.rbMain0) {
setToolBar(mToolbar, "首頁");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
changeFragment(0);
} else if (viewId == R.id.rbMain1) {
setToolBar(mToolbar, "音樂");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
changeFragment(1);
} else if (viewId == R.id.rbMain2) {
setToolBar(mToolbar, "視頻");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
changeFragment(2);
} else if (viewId == R.id.rbMain3) {
setToolBar(mToolbar, "我的");
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
changeFragment(3);
}
}
}
/**
* 切換fragment
*
* @param i
*/
private void changeFragment(int i) {
Log.i("TAG", "i:" + i + " currentItem:" + currentItem);
if (currentItem == i) return; //若選中的item為當(dāng)前,返回
//這里不推薦使用replace()
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
if (mFragments.get(i).isAdded()) {
fragmentTransaction.hide(mFragments.get(currentItem)).show(mFragments.get(i));
} else {
if (currentItem == -1) {//初始狀態(tài)新建
fragmentTransaction.add(R.id.flMain, mFragments.get(i));
} else {//非初始狀態(tài)切換
fragmentTransaction.hide(mFragments.get(currentItem)).add(R.id.flMain, mFragments.get(i));
}
}
fragmentTransaction.commit();
currentItem = i;
}
@Override
protected void createPresenter() {
mPresenter = new MainPresenterImpl(this);
}
@Override
public void showError(BaseResultBean showError) {
handleError(showError);
}
long exitTime;
@Override
public void onBackPressed() {
if (rbMain0.isChecked()) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
ToastUtils.showToast(this, "再按一次將退出應(yīng)用");
exitTime = System.currentTimeMillis();
} else {
super.onBackPressed();
}
} else {
rbMain0.setChecked(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Toast.makeText(MainActivity.this, "setNavigationlcon", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
String msg = "";
msg = item.getTitle().toString();
ToastUtils.showToast(this, "您點(diǎn)擊了" + msg);
return true;
}
}
activity_main
<?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.base.basebiz.main.view.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/appBarLayout">
<RadioGroup
android:id="@+id/rgMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="@color/homeRadioBack"
>
<RadioButton
android:id="@+id/rbMain0"
android:layout_width="@dimen/dp0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:button="@null"
android:gravity="center"
android:lineSpacingExtra="@dimen/dp3"
android:minHeight="@dimen/dp30"
android:paddingBottom="@dimen/dp5"
android:paddingTop="@dimen/dp5"
android:textColor="@drawable/selector_radiobutton_textcolor"
android:textSize="@dimen/sp12" />
<RadioButton
android:id="@+id/rbMain1"
android:layout_width="@dimen/dp0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:button="@null"
android:gravity="center"
android:lineSpacingExtra="@dimen/dp3"
android:minHeight="@dimen/dp30"
android:paddingBottom="@dimen/dp5"
android:paddingTop="@dimen/dp5"
android:textColor="@drawable/selector_radiobutton_textcolor"
android:textSize="@dimen/sp12" />
<RadioButton
android:id="@+id/rbMain2"
android:layout_width="@dimen/dp0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:button="@null"
android:gravity="center"
android:lineSpacingExtra="@dimen/dp3"
android:minHeight="@dimen/dp30"
android:paddingBottom="@dimen/dp5"
android:paddingTop="@dimen/dp5"
android:textColor="@drawable/selector_radiobutton_textcolor"
android:textSize="@dimen/sp12" />
<RadioButton
android:id="@+id/rbMain3"
android:layout_width="@dimen/dp0"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:button="@null"
android:gravity="center"
android:lineSpacingExtra="@dimen/dp3"
android:minHeight="@dimen/dp30"
android:paddingBottom="@dimen/dp5"
android:paddingTop="@dimen/dp5"
android:textColor="@drawable/selector_radiobutton_textcolor"
android:textSize="@dimen/sp12" />
</RadioGroup>
<FrameLayout
android:id="@+id/flMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/rgMain"
android:background="@color/background"></FrameLayout>
</RelativeLayout>
</RelativeLayout>
有兩點(diǎn)需要強(qiáng)調(diào)
1 RadioButton使用字體文件
TextView使用字體文件不用多說疼电,RadioButton使用字體文件卻不多見嚼锄。我們可以看一下RadioButton的繼承關(guān)系:
RadioButton>CompoundButton>Button>TextView
一目了然,RadioButton是個(gè)特殊的TextView,適用于TextView的字體文件蔽豺,當(dāng)然也適用于RadioButton
2 RadioButton改變部分字體大小
這里只改變第一個(gè)字符大小
Spannable spannable = new SpannableString(text);
spannable.setSpan(new AbsoluteSizeSpan(textSize,true), 0, 1,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
view.setText(spannable);
ToolBar細(xì)化
為什么要把ToorBar拿出來細(xì)說区丑。細(xì)細(xì)研究了一番。不得不說安卓原生的控件的確做得很精致修陡,而且微信也是用的原生的沧侥。
此外右側(cè)的menu長按時(shí)還會(huì)有提示。很值得擁有魄鸦,我看過好多款A(yù)PP宴杀,Toolbar基本都是隨意搭建的,長按沒有提示不說拾因,連點(diǎn)擊效果沒有旺罢。真是太粗糙!另外绢记,可能大家覺得扁达,Toolbar左側(cè)回退和右側(cè)menu 都是圖片 這個(gè)素材有些困難。殊不知iconfont網(wǎng)站的任意一個(gè)字體文件都可以以png格式下載下來蠢熄。
是不是頓時(shí)感嘆阿里的強(qiáng)大跪解!