项目介绍:
券妈妈,是国内最大,最早专业网购优惠券网站,2012年底已有近300万注册用户,以第三方身份向网络消费用户提供优惠券,优惠活动,打折促销以及免费试用等购物优惠省钱的信息平台。从开通至今,目前网站日访问人数已突破15万人次,每日发放近8万张优惠券。
1.配置开发环境,使用bufferKnife注解框架:
Project的build.gradle文件中增加:
- buildscript {
repositories {
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.2.0-alpha6’
classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’ //增加这一句
}
}
app的build.gradle文件中增加classpath内容:
*apply plugin: ‘com.android.application’
apply plugin: ‘com.neenbedankt.android-apt’//增加这一句
dependencies {
compile fileTree(include: [‘*.jar’], dir: ‘libs’)
testCompile ‘junit:junit:4.12’
compile ‘com.android.support:appcompat-v7:24.1.1’
compile ‘com.google.code.gson:gson:2.4’
compile ‘com.jakewharton:butterknife:8.2.1’
apt ‘com.jakewharton:butterknife-compiler:8.2.1’//增加这一句
}
注意,需要将光标移到setContentView(R.layout.activity_main),将光标放到R.layout.activity_main,然后alt + insert 键 选择 Generate。一定要将光标放在R.layout.activity_main上面。
2.创建MyApp 继承 Application 对引入的第三方进行初始化。
1.代码如下:
*public class MyApp extends Application {
private static MyApp ourInstance = new MyApp();
public static MyApp getInstance() {
return ourInstance;
}
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
UMShareAPI.get(this);
PlatformConfig.setWeixin(“wxdc1e388c3822c80b”, “3baf1193c85774b3fd9d18447d76cab0”);
//豆瓣RENREN平台目前只能在服务器端配置
//新浪微博
PlatformConfig.setSinaWeibo(“3921700954”, “04b48b094faeb16683c32669824ebdad”);
//易信
}
List activityList=new ArrayList<>();
2.把所有打开的Activity都放入List中可以一键控制:进行退出
代码如下:
- public void AddActivity(Activity context){
activityList.add(context);
}
public void finishActivity(){
for (Activity activity : activityList) {
activity.finish();
}
}
3.创建基类,可以进行全局的修改:
1.activity的代码:Fragment的基类与此相似,不在粘贴:
*public abstract class BaseActivity extends AppCompatActivity {
{
MyApp.getInstance().AddActivity(this);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
//一定又有这句代码,不然会绑定失败:
ButterKnife.bind(this);
initView();
}
public abstract int getLayout();
public abstract void initView();
}
4.使用Fragment+TextView 搭建主界面:
1.布局代码:
2 控制Java代码:
使用onSaveInstanceState 解决Fragment重叠性问题!但crash后,页面不会重叠 ,会直接退出程序!
- public class MainActivity extends BaseActivity {
//注解:获取控件
@BindView(R.id.home_fl_content)
FrameLayout homeFlContent;
@BindView(R.id.Rg)
LinearLayout Rg;
@BindView(R.id.activity_main)
RelativeLayout activityMain;
public int getLayout() {
return R.layout.activity_main;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File photoCacheDir = Glide.getPhotoCacheDir(this);
String absolutePath = photoCacheDir.getAbsolutePath();
if (savedInstanceState!=null){
tag= savedInstanceState.getString(“TAG”);
int i = Integer.parseInt(tag);
fragmentTransaction.remove(fragments[i]);
}
}
//异常退出时保存数据
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putString(“TAG”,tag);
}
//修复数据
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public void initView() {
Rg.getChildAt(0).setSelected(true);
showFragment(0);
}
FragmentTransaction fragmentTransaction;
private Fragment[] fragments = new Fragment[5];
int currrentindex = -1;
private void showFragment(int index) {
if (index == currrentindex)
return;
fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (currrentindex != -1) {
fragmentTransaction.hide(fragments[currrentindex]);
}
if (fragments[index] == null) {
createFragment(index);
fragmentTransaction.add(R.id.home_fl_content, fragments[index], index + “”);
} else {
fragmentTransaction.show(fragments[index]);
}
fragmentTransaction.commit();
if (currrentindex != -1)
Rg.getChildAt(currrentindex).setSelected(false);
Rg.getChildAt(index).setSelected(true);
currrentindex = index;
}
String[] fragmentName = {“homefragment”, “youhuijuan”, “fuli”, “zhidemai”, “mefragment”};
//利用反射创建Fragment:
private void createFragment(int index) {
try {
fragments[index] = (Fragment) Class.forName(“fragment.” + fragmentName[index]).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
String tag;
@OnClick({R.id.radio_home, R.id.radio_youhui, R.id.radio_fuli, R.id.radio_zhidemai, R.id.radio_me})
public void onClick(View view) {
tag = (String) view.getTag();
showFragment(Integer.parseInt(tag));
}
项目地址: 链接项目地址