Kotlin Jetpack 实战 01 从一个膜拜大

简介

本文属于《Kotlin Jetpack 实战》系列文章。

这是我用 Java 写的一个“原始架构”的 App,名字叫:KotlinJetpackInAction,它的功能只有一个:膜拜大神!

为了方便大家理解 Kotlin,Coroutines,Jetpack,Functional Programming,MMVM 这些新知识,这个 Demo 简单到了极致。随着文章的更新,我会一步步用 Kotlin,Jetpack 将其重构,然后再往里面加一些新功能。

截图

Android 界无人不知,无人不晓的神级存在:JakeWharton

工程结构

MainActivity:用来膜拜大神

ImagePreviewActivity:用来瞻仰大神的头像

MainActivity 源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
java复制代码public class MainActivity extends AppCompatActivity {
public static final String TAG = "Main";
public static final String EXTRA_PHOTO = "photo";

StringRequest stringRequest;
RequestQueue requestQueue;

private ImageView image;
private ImageView gif;
private TextView username;
private TextView company;
private TextView website;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

private void init() {
image = findViewById(R.id.image);
gif = findViewById(R.id.gif);
username = findViewById(R.id.username);
company = findViewById(R.id.company);
website = findViewById(R.id.website);

display(User.CACHE_RESPONSE);
requestOnlineInfo();
}

private void requestOnlineInfo() {
requestQueue = Volley.newRequestQueue(this);
String url ="https://api.github.com/users/JakeWharton";
stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
display(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
stringRequest.setTag(TAG);
requestQueue.add(stringRequest);
}

private void display(@Nullable String response) {
if (TextUtils.isEmpty(response)) { return; }

Gson gson = new Gson();
final User user = gson.fromJson(response, User.class);
if (user != null){
Glide.with(this).load("file:///android_asset/bless.gif").into(gif);
Glide.with(this).load(user.getAvatar_url()).apply(RequestOptions.circleCropTransform()).into(image);
this.username.setText(user.getName());
this.company.setText(user.getCompany());
this.website.setText(user.getBlog());

image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoImagePreviewActivity(user);
}
});
}
}

private void gotoImagePreviewActivity(User user) {
Intent intent = new Intent(this, ImagePreviewActivity.class);
intent.putExtra(EXTRA_PHOTO, user.getAvatar_url());
startActivity(intent);
}

@Override
protected void onStop () {
super.onStop();
if (requestQueue != null) {
requestQueue.cancelAll(TAG);
}
}
}

ImagePreviewActivity 源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
java复制代码public class ImagePreviewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_preview);

Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_PHOTO);
if (!TextUtils.isEmpty(url)) {
ImageView imageView = findViewById(R.id.imagePreview);
Glide.with(this).load(url).into(imageView);
}
}
}

结尾

我相信,即使是刚入门的小伙伴也能轻松看懂这个 Demo。

这也是本系列文章的不同之处,我会带着各位从这个简单 Demo 开始,一步步学习那些"高端"技术,最终把这个"简单"App变成"高端"App。

这个 Demo 已在 GitHub 开源,欢迎 starfork,也欢迎提 Issue 说出你的建议: github.com/chaxiu/Kotl…

回目录–>《Kotlin Jetpack 实战》

都看到这了,点个赞呗!

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%