本文属于【Kotlin Jetpack 实战】系列文章。
这是我用 Java + MVC 写的一个 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 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 源码
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 的地址-->KotlinJetpackInAction
继续阅读请点击-->【Kotlin Jetpack 实战】传送门