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

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

效果

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

竖直.png

实现过程

ToolBar使用难点
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
  • 添加ToolBar右侧的菜单
    • 在res目录下创建menu目录,在menu目录下创建菜单文件
    • 在MainActivity下重写如下方法
//设置菜单
@Override
public 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.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_龙衣

赏杯快乐水喝喝

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值