RecyclerView多条目加载fesco图片变圆形

需要导入的依赖和权限

implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.facebook.fresco:fresco:0.12.0'
implementation 'org.greenrobot:eventbus:3.0.0'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

activity_main

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/my_srl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_rcv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        android.support.v7.widget.RecyclerView>
    android.support.v4.widget.SwipeRefreshLayout>

LinearLayout>

activity_main2

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10px">
        <ImageView
            android:id="@+id/my_img"
            android:layout_width="150px"
            android:layout_height="150px"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:id="@+id/my_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="只要你躺足70天,NASA就给你11万块钱!"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10px"/>
    LinearLayout>

LinearLayout>

rcv_item

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10px">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/rcv_img"
            android:layout_width="150px"
            android:layout_height="150px"
            fresco:placeholderImage="@drawable/a"
            fresco:roundedCornerRadius="30dp"
            fresco:roundTopLeft="true"
            fresco:roundTopRight="true"
            fresco:roundBottomLeft="true"
            fresco:roundBottomRight="true">
        com.facebook.drawee.view.SimpleDraweeView>

        <TextView
            android:id="@+id/rcv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="只要你躺足70天,NASA就给你11万块钱!"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10px"/>
    LinearLayout>

LinearLayout>

rcv_item2

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10px">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/rcv_img2"
            android:layout_width="150px"
            android:layout_height="150px"
            fresco:placeholderImage="@drawable/b"
            fresco:actualImageScaleType="centerCrop"
            fresco:placeholderImageScaleType="centerCrop"
            fresco:roundAsCircle="true">
        com.facebook.drawee.view.SimpleDraweeView>

        <TextView
            android:id="@+id/rcv_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="只要你躺足70天,NASA就给你11万块钱!"
            android:layout_marginLeft="10px"
            android:layout_gravity="center_vertical" />

    LinearLayout>LinearLayout>

MainActivity

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bwie.test.adapter.MyAdapter;
import com.bwie.test.bean.DataBean;
import com.bwie.test.evens.Evens;
import com.bwie.test.myjinjieti.inter_view.DataView;
import com.bwie.test.presenter.MyDataPersenter;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class MainActivity extends AppCompatActivity implements DataView,MyAdapter.OnClickListener{
    private RecyclerView my_rcv;
    String url = "https://api.tianapi.com/wxnew/?key=fe7aac932e1e94f10a4f8d0f2d5fe924&num=10";
    private MyAdapter myAdapter;
    private List list;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            list = (List) msg.obj;
            myAdapter = new MyAdapter(MainActivity.this, list);
            my_rcv.setAdapter(myAdapter);

            //调用适配器中的点击事件
            myAdapter.OnClick((MyAdapter.OnClickListener) MainActivity.this);
        }
    };
    private SwipeRefreshLayout my_srl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //隐藏原有标题
        getSupportActionBar().hide();
        //查找控件
        my_rcv = findViewById(R.id.my_rcv);
        my_srl = findViewById(R.id.my_srl);

        my_srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //控制使它显示刷新
                my_srl.setRefreshing(true);
            }
        });

        //设置管理器
        my_rcv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        //初始化P层,将url和本类传过去
        MyDataPersenter persenter = new MyDataPersenter(this);
        persenter.netWork(url);
    }

    @Override
    public void toBackHome(List list) {
        Message msg = new Message();
        msg.obj = list;
        handler.sendMessage(msg);

        my_srl.setRefreshing(false);
    }

    @Override
    public void OnItemClick(int position) {
        Evens evens = new Evens();
        String title = list.get(position).getTitle();
        String url = list.get(position).getUrl();
        evens.setName(title);
        evens.setUrl(R.drawable.a);
        evens.setUrl(R.drawable.b);

        EventBus.getDefault().postSticky(evens);
        //实现跳转
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);
    }
}

Main2Activity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwie.test.evens.Evens;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class Main2Activity extends AppCompatActivity {
    private TextView my_title;
    private ImageView my_img;
    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //隐藏原有标题
        getSupportActionBar().hide();
        //查找控件
        my_title = findViewById(R.id.my_title);
        my_img = findViewById(R.id.my_img);

        //注册
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvens(Evens evens) {
        //获取Evens传来的数据,进行赋值
        my_title.setText(evens.getName());
        if(index%2 == 0){
            Glide.with(this).load(evens.getUrl()).into(my_img);
        }else{
            Glide.with(this).load(evens.getUrl2()).into(my_img);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

inter_view文件夹下DataView

import com.bwie.test.bean.DataBean;
import java.util.List;

public interface DataView {
    //接收数据的方法
    void toBackHome(List list);
}

adapter文件夹下MyAdapter

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
;
import com.bwie.test.bean.DataBean;
import com.bwie.test.myjinjieti.R;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter{
    Context context;
    List list;

    public MyAdapter(Context context, List list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        //判断条目的类型 判断引入的布局文件
        int itemViewType = getItemViewType(viewType);
        if(itemViewType==0){
            view = View.inflate(context, R.layout.rcv_item, null);
        }else{
            view = View.inflate(context, R.layout.rcv_item2, null);
        }

        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    //获取条目的类型 然后进行判断
    @Override
    public int getItemViewType(int position) {
        if(position%2==0){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        //得到条目
        int itemViewType = getItemViewType(position);
        if(itemViewType==0){
            myViewHolder.rcv_title.setText(list.get(position).getTitle());
        }else{
            myViewHolder.rcv_title2.setText(list.get(position).getTitle());
        }

        //条目进行点击事件
        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.OnItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        private final TextView rcv_title,rcv_title2;
        private final SimpleDraweeView rcv_img;
        private final SimpleDraweeView rcv_img2;

        public MyViewHolder(View view) {
            super(view);
            //找到两个子布局文件的控件id
            rcv_img = view.findViewById(R.id.rcv_img);
            rcv_title = view.findViewById(R.id.rcv_title);
            rcv_img2 = view.findViewById(R.id.rcv_img2);
            rcv_title2 = view.findViewById(R.id.rcv_title2);
        }
    }

    //创建暴露点击事件的类
    OnClickListener onClickListener;
    public void OnClick(OnClickListener onClickListener){
        this.onClickListener=onClickListener;
    }

    //创建点击事件接口
    public interface OnClickListener{
        void OnItemClick(int position);
    }
}

evens文件夹下Evens

public class Evens {
    private String name;
    private int url;
    private int url2;

    public int getUrl2() {
        return url2;
    }

    public void setUrl2(int url2) {
        this.url2 = url2;
    }

    public int getUrl() {
        return url;
    }

    public void setUrl(int url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

model文件夹下DataModel

import com.bwie.test.presenter.MyDataPersenter;

public interface DataModel {
    //请求数据的方法
    void getData(String url, MyDataPersenter presenter);
}

MyDataModle

import com.bwie.test.bean.DataBean;
import com.bwie.test.presenter.MyDataPersenter;
import com.bwie.test.utils.OkhtttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MyDataModel implements DataModel{
    @Override
    public void getData(String url,final MyDataPersenter presenter) {
        OkhtttpUtils.doGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();

                Gson gson = new Gson();
                DataBean dataBean = gson.fromJson(json, DataBean.class);
                List list = dataBean.getNewslist();
                presenter.Success(list);
            }
        });
    }
}

presenter文件夹下DataPresenter

import com.bwie.test.bean.DataBean;
import java.util.List;

public interface DataPresenter {
    //成功与失败的方法
    void Success(List list);
    void Error();
}

MyDataPersenter

 
  
import com.bwie.test.bean.DataBean;
import com.bwie.test.model.MyDataModel;
import com.bwie.test.myjinjieti.inter_view.DataView;
import java.util.List;

public class MyDataPersenter implements DataPresenter{
    //创建构造器
    DataView dataView;
    private final MyDataModel model;

    public MyDataPersenter(DataView dataView) {
        this.dataView = dataView;
        model = new MyDataModel();
    }

    //成功的方法
    @Override
    public void Success(List list) {
        //调用V层接收数据的方法
        dataView.toBackHome(list);
    }

    //失败的方法
    @Override
    public void Error() {}

    //接收url
    public void netWork(String url){
        //调用M层请求数据的方法
        model.getData(url,this);
    }
}

utils文件夹下OkhttpUtils

import android.os.Environment;
import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkhtttpUtils {
    private OkhtttpUtils(){};
    static  OkHttpClient client;

    public static OkHttpClient getInstance(){

        if (client==null) {
            //更加安全
            synchronized (OkhtttpUtils.class) {
                //缓存的地方     mnt/sdcard
                File file = new File(Environment.getExternalStorageDirectory(), "cache11");
                client = new OkHttpClient().newBuilder()
                        .readTimeout(3000, TimeUnit.SECONDS)   //设置读取超时时间
                        .connectTimeout(3000, TimeUnit.SECONDS) //设置连接的超时时间
                        .cache(new Cache(file, 10 * 1024))
                        .build();
            }
        }
        return client;
    }

    /**
     * get请求
     * Callback  是一个接口
     */
    public static void doGet(String url, Callback callback){
        OkHttpClient client = getInstance();
        Request request = new Request.Builder()
                .url(url)
                .build();
        client.newCall(request).enqueue(callback);
    }

    //post请求
    /**
     * @param url  请求的地址
     * @param parms   请求的参数
     * @param callback  callback
     */
    public static void doPost(String url, Map parms, Callback callback){
        OkHttpClient client = getInstance();
        FormBody.Builder body = new FormBody.Builder();
        //key   value
        for (String key:parms.keySet()){
            //value的值
            body.add(key,parms.get(key));
        }
        Request request = new Request.Builder()
                .url(url)
                .post(body.build())
                .build();

        client.newCall(request).enqueue(callback);
    }

    //用来上传图片的
    //url  , 图片  ,参数    Callback
    public static  void upImage(String url, File file, String filenName, Map params, 
                                               Callback callback){
        OkHttpClient client = getInstance();
        //requestBody的实现类  Formbody
        MultipartBody.Builder builder = new MultipartBody.Builder();

        if (params!=null){
            for (String key :params.keySet()){
                builder.addFormDataPart(key,params.get(key));
            }
        }

        //设置类型
        builder.setType(MultipartBody.FORM);

        builder.addFormDataPart("file",filenName, RequestBody.create(MediaType.parse(
                                          "application/octet-stream"),file));

        // builder.setType(MultipartBody.FORM);
        // builder.addFormDataPart("file",filenName,RequestBody.create(MediaType.parse(
                                  "application/octet-stream"),file));
        //builder.addFormDataPart("file","aa.png",builder.build());

        Request request = new Request.Builder()
                .url(url)
                .post(builder.build())
                .build();

        client.newCall(request).enqueue(callback);
    }
}

bean文件夹下DataBean

import java.util.List;

public class DataBean {
    private int code;
    private String msg;
    private List newslist;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List getNewslist() {
        return newslist;
    }

    public void setNewslist(List newslist) {
        this.newslist = newslist;
    }

    public static class NewslistBean {

        private String ctime;
        private String title;
        private String description;
        private String picUrl;
        private String url;

        public String getCtime() {
            return ctime;
        }

        public void setCtime(String ctime) {
            this.ctime = ctime;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getPicUrl() {
            return picUrl;
        }

        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

你可能感兴趣的:(Android网络请求)