美文网首页
OkHttp+RecyclerView简单使用

OkHttp+RecyclerView简单使用

作者: Meimaobing | 来源:发表于2017-10-16 08:08 被阅读0次
package com.example.zhoukao2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.zhoukao2.Bean.User;
import com.example.zhoukao2.okhttp.OkHttp3Utils;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

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

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

public class Ss extends AppCompatActivity {
    User user;
    TextView tv;
    List<User.DataBean.GoodsBriefBean> list;
    RecyclerView rv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ss);
        rv= (RecyclerView) findViewById(R.id.rv);
       StaggeredGridLayoutManager mgr=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
      rv.setLayoutManager(mgr);
        rv.setLayoutManager(mgr);


        ImageLoaderConfiguration configuration= ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(configuration);
        getData();

    }

    private void getData() {
        String url="http://m.yunifang.com/yunifang/mobile/category/list?random=96333&encode=bf3386e14fe5bb0bcef234baebca2414";
        OkHttp3Utils.doGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                user=new Gson().fromJson(response.body().string(),User.class);
                list=new ArrayList<User.DataBean.GoodsBriefBean>();
                list=user.getData().getGoodsBrief();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                   Myadpter myadpter=new Myadpter();
                        rv.setAdapter(myadpter);
                    }
                });
            }
        });
    }
    class Myadpter extends RecyclerView.Adapter<Myadpter.MyViewHolder>{

        @Override
        public Myadpter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=View.inflate(Ss.this,R.layout.item,null);
            MyViewHolder holder=new MyViewHolder(view);

            return holder;
        }

        @Override
        public void onBindViewHolder(Myadpter.MyViewHolder holder, int position) {
             holder.tv.setText(list.get(position).getGoods_name());
             ImageLoader.getInstance().displayImage(list.get(position).getGoods_img(),holder.iv);
        }

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

        public class MyViewHolder extends RecyclerView.ViewHolder {
            ImageView iv;
            TextView tv;
            public MyViewHolder(View itemView) {

                super(itemView);
                iv=itemView.findViewById(R.id.iv3);
                tv=itemView.findViewById(R.id.tv5);
            }
        }
    }




}





相关文章

网友评论

      本文标题:OkHttp+RecyclerView简单使用

      本文链接:https://www.haomeiwen.com/subject/cznhuxtx.html