尾声
最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。
当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。
进阶学习视频
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
//建立fragment管理器
fragmentManager = getSupportFragmentManager();
//管理器开启事务,将fragment实例加入管理器
fragmentManager.beginTransaction()
.add(R.id.FragmentLayout, fragmentList.get(0), “TASK”)
.add(R.id.FragmentLayout, fragmentList.get(1), “ABSOTBED”)
.add(R.id.FragmentLayout, fragmentList.get(2), “MUSIC”)
.add(R.id.FragmentLayout, fragmentList.get(3), “WEATHER”)
.commit();
//设置fragment显示初始状态
fragmentManager.beginTransaction()
.show(fragmentList.get(1))
.hide(fragmentList.get(0))
.hide(fragmentList.get(2))
.hide(fragmentList.get(3))
.commit();
//设置底部导航栏点击选择监听事件
BottomNavigationView bottomNavigationView = findViewById(R.id.BottomNavigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@SuppressLint(“NonConstantResourceId”)
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// return true : show selected style
// return false: do not show
switch (item.getItemId()) {
case R.id.menu_task:
ShowFragment(0);
return true;
case R.id.menu_accounts:
ShowFragment(1);
return true;
case R.id.menu_absorbed:
ShowFragment(2);
return true;
case R.id.menu_weather:
ShowFragment(3);
return true;
default:
Log.i(TAG, “onNavigationItemSelected: Error”);
break;
}
return false;
}
});
}
public void ShowFragment(int index) {
fragmentManager.beginTransaction()
.show(fragmentList.get(index))
.hide(fragmentList.get((index + 1) % 4))
.hide(fragmentList.get((index + 2) % 4))
.hide(fragmentList.get((index + 3) % 4))
.commit();
}
1、添加依赖(用于获取和解析天气数据)
implementation ‘com.google.code.gson:gson:2.8.6’
implementation ‘com.squareup.okhttp3:okhttp:4.9.0’
2、获取天气API接口,这里以临海市为例。使用OkHttp请求天气数据,使用Log打印测试是否能成功获取
public void RefreshWeatherData() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(weatherUrl).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
String weatherJson = response.body().string();
Weather weather = new Gson().fromJson(weatherJson, Weather.class);
Log.i(TAG, "onResponse: "+weatherJson);
}
});
}
3、Json数据获取成功后,根据Json数据的结构建立Weather类用于解析Json数据。
// class Weather
public class Weather {
private String city; //城市名
private String update_time; //更新时间
private List data; //每天的天气数据列表,data.get(0)为当天数据
/*
getter and setter
*/
}
// class DayData
public class DayData {
private String wea; //天气状况
private String tem; //当前温度
private String tem1; //最高温
private String tem2; //最低温
private String humidity; //湿度
private String air_level; //空气质量等级
private String air_tips; //空气质量小提示
/*
getter and setter
*/
}
4、由于OkHttp的请求是在子线程中进行的,需要使用Handler消息队列机制将解析出来的Weather实例发送到主线程用以显示在界面上。
//消息处理类
public class MyHandler extends Handler {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
//what == 1 天气消息
if (msg.what == 1)
ShowWeatherInfo((Weather) msg.obj);
}
}
public void ShowWeatherInfo(Weather weather) {
String city = weather.getCity();
String wea = weather.getData().get(0).getWea();
String maxTem = weather.getData().get(0).getTem1();
String minTem = weather.getData().get(0).getTem2();
String tem = weather.getData().get(0).getTem();
String humidity = "湿度 " + weather.getData().get(0).getHumidity();
String air_level = "空气指数 " + weather.getData().get(0).getAir_level();
// tem tem1 tem2 city wea rain pm image
((TextView) findViewById(R.id.cityView)).setText(city);
((TextView) findViewById(R.id.weaView)).setText(wea);
((TextView) findViewById(R.id.mmtemView)).setText(
String.format(“%s° / %s°”, minTem.substring(0, minTem.length() - 1), maxTem.substring(0, maxTem.length() - 1)));
((TextView) findViewById(R.id.temView)).setText(tem.substring(0, tem.length() - 1) + “°”);
((TextView) findViewById(R.id.humidityView)).setText(humidity);
((TextView) findViewById(R.id.levelView)).setText(air_level);
ShowWeatherImage(wea); //根据天气状况wea显示对应的天气图片,这里不详细说明,使用switch就行
}
5、别忘了在OkHttp请求完成时发送消息
public void RefreshWeatherData() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(weatherUrl).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
String weatherJson = response.body().string();
Weather weather = new Gson().fromJson(weatherJson, Weather.class);
Message message = new Message();
message.what = 1;
message.obj = weather;
myHandler.sendMessage(message);
}
});
}
6、优化xml布局
这里由于ListView是放在Fragment中的,所以直接在MainAcitivity.java中设置适配器可能会出现数据没法显示的bug。所以我直接把从数据库获取数据,Adapter的定义,ListView设置适配器的模块搬到了TaskFragment.java中。
1.在task.xml中添加ListView,先不用设置UI样式,先把数据拿到并显示在界面上
<?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=“match_parent”
android:orientation=“vertical”>
<TextView
android:id=“@+id/taskText”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“事项”/>
<ListView
android:id=“@+id/taskListView”
android:layout_width=“match_parent”
android:layout_height=“match_parent”/>
2.创建task_item.xml布局文件(这里注意线性布局的方向及宽高,以保证task_item能放在ListView中)
<?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=“wrap_content”>
<TextView
android:id=“@+id/task_content”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:textColor=“@color/black”
android:textSize=“30dp”
android:text=“TextView” />
3.新建TaskItem类,存放事项数据
package com.example.daily.tasks;
public class TaskItem {
private int id;
private String content;
private String type;
private int status;
public TaskItem(int