Retrofit2.1.0 + Glide + ToolBar + RecyclerView 的使用

来源:互联网 发布:程序员的三大浪漫 编辑:程序博客网 时间:2024/05/17 07:39

目标是掌握网络请求框架,同时熟练使用Android MD元素
使用Retrofit2.1.0 请求网路i数据
使用Glide 加载图片
使用ToolBar显示标题
使用Recycler View显示内容

效果

( 为了节省服务器端的流量,因此图片取的有点小)
水平.png

竖直.png

实现过程

ToolBar使用难点
  • ToolBar的基础使用参看
    android:ToolBar详解(手把手教程)
  • 去掉标题栏
    在AndroidManifest.xml文件中添加一个背景,如下:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
  • 添加ToolBar右侧的菜单
    • 在res目录下创建menu目录,在menu目录下创建菜单文件
    • 在MainActivity下重写如下方法
//设置菜单@Overridepublic boolean onCreateOptionsMenu(Menu menu) {          getMenuInflater().inflate(R.menu.menu_main,menu);        return true;}
  • 改变字体颜色

    • 默认颜色
      Paste_Image.png

    • 修改后的颜色
      Paste_Image.png

    • 实现过程
      在style.xml文件中添加如下内容:

      在ToolBar中添加一个theme,如下:
      Paste_Image.png
RecyclerView的使用
  • 详细使用
     Android RecyclerView 使用完全解析 体验艺术般的控件
  • 添加依赖
    compile 'com.android.support:recyclerview-v7:24.2.1'
  • 布局中使用
<android.support.v7.widget.RecyclerView       android:id="@+id/list"        android:layout_width="match_parent"       android:layout_height="wrap_content"        android:cacheColorHint="@null"        />
  • 在Java代码中使用(网格布局)
staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);list.setLayoutManager(staggeredGridLayoutManager);
CardView的使用
  • 详细使用
    CardView的使用
  • 添加依赖
    'compile 'com.android.support:cardview-v7:24.2.1'
  • 布局使用
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView      xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"      android:id="@+id/cardView"   app:cardBackgroundColor="#80cbc4"    app:cardCornerRadius="10dp"  app:cardPreventCornerOverlap="true"      app:cardUseCompatPadding="true"    android:layout_width="match_parent"    android:layout_height="wrap_content">     <ImageView        android:id="@+id/imageView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"        android:scaleType="fitCenter"        /></android.support.v7.widget.CardView>
Retrofit2.1.0的使用
  • 导包
compile 'com.squareup.okhttp3:okhttp:3.4.1'compile 'com.squareup.retrofit2:retrofit:2.1.0'
  • 定义一个接口
public interface PhotoService {    @GET("tnfs/api/news")   Call<Tngou> getResult() ;}
  • 定义一个JavaBean
    代码太长,不贴出来了,文末看源码!
  • 代码中使用
/** * 获取服务器的数据 */private void getDataFromService() {    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.tngou.net/")            .addConverterFactory(GsonConverterFactory.create()).build();    PhotoService service = retrofit.create(PhotoService.class);    Call<Tngou> call = service.getResult();    call.enqueue(this);}@Override    public void onResponse(Call<Tngou> call, Response<Tngou> response) {        photoList = response.body().getList();        list.setAdapter(new PhotoAdater(photoList, this));//        Log.e("返回的List:", "normalGet:" + photoList.toString());//返回成功    }    @Override    public void onFailure(Call<Tngou> call, Throwable t) {    }
  • 小提示
    使用Retrofit2一定要定义好JavaBean,必须和API中的参数一模一样,否则response.body().getList()返回null

项目地址

项目地址GitHub

1 0
原创粉丝点击