获取网络数据实现RecyclerView多条目

先上效果图:

接口地址:http://365jia.cn/news/api3/365jia/news/headline?page=2


1、MainActivity代码 


public class MainActivity extends AppCompatActivity {

    private static final int SUCCESS = 1;
    private static final int FALL = 2;
    private String path = "http://365jia.cn/news/api3/365jia/news/headline?page=2";
    private RecyclerView recyclerView;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //判断如果成功就进行解析并展示数据
            if(msg.what==SUCCESS){
            String s = (String) msg.obj;
            Gson gson = new Gson();
            List<UserBean.DataBeanX.DataBean> list = gson.fromJson(s, UserBean.class).getData().getData();
            recyclerView.setAdapter(new MyBase(MainActivity.this,list));
            } else if(msg.what == FALL){
                Toast.makeText(MainActivity.this, "获取网络失败", Toast.LENGTH_SHORT).show();
            } else{}
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recy);
        //一定得设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
        getdata();
    }

    //获取数据
    private void getdata() {
        OkHttpClient client = new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(path).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            handler.sendEmptyMessage(FALL);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String s = response.body().string();
                Message msg = Message.obtain();
                msg.obj = s;
                msg.what = SUCCESS;
                handler.sendMessage(msg);
            }
        });
    }
}
 

2、ImageLoader

3、RecyclerView适配器

                                              //多条目时此处不用类名调用ViewHodler  
public class MyBase extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int FLAG = 0;
    private static final int FLOW = 1;
    private Context context;
    private List<UserBean.DataBeanX.DataBean> list;

    public MyBase(Context context, List<UserBean.DataBeanX.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)//使用内存缓存
            .cacheOnDisk(true)//使用磁盘缓存
            .showImageOnFail(R.mipmap.ic_launcher)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值