简介
本文属于《Kotlin Jetpack 实战》系列文章。
这是我用 Java 写的一个“原始架构”的 App,名字叫:KotlinJetpackInAction,它的功能只有一个:膜拜大神!
为了方便大家理解 Kotlin,Coroutines,Jetpack,Functional Programming,MMVM 这些新知识,这个 Demo 简单到了极致
。随着文章的更新,我会一步步用 Kotlin,Jetpack 将其重构,然后再往里面加一些新功能。
截图
Android 界无人不知,无人不晓的神级存在:JakeWharton
工程结构
MainActivity:用来膜拜
大神
ImagePreviewActivity:用来瞻仰
大神的头像
MainActivity 源码
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 one rrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
stringRequest.setTag(TAG);
requestQueue.add(stringRequest);
}
private void display(@Nullable {
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 源码
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 开源,欢迎 star
,fork
,也欢迎提 Issue 说出你的建议: github.com/chaxiu/Kotl…