OkHttp请求数据到RecyclerView多条目展示

移动开发 waitig 664℃ 百度已收录 0评论
package test.bwei.com.okhttpshujuzhanshi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.IOException;

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

public class MainActivity extends AppCompatActivity {
    //找到控件
    private RecyclerView rv;
    //路径
    String url="http://m.yunifang.com/yunifang/mobile/home";
    Bean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //imageLoding加载
        ImageLoaderConfiguration config=ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(config);
        DisplayImageOptions options=DisplayImageOptions.createSimple();

        rv= (RecyclerView) findViewById(R.id.rv);
        //管理器,设置分割线
        initView();

        //OkHttp网络获取
        getOkHttpData();
    }

    private void getOkHttpData() {
        OkHttp3Utils.doGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

               //开始解析数据
                //创建一个Gson对象
                Gson gson=new Gson();
                String sting=response.body().string();
                //bean赋值
                bean=gson.fromJson(sting,Bean.class);
                //判断数据是否请求成功
                if(response.isSuccessful()){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //实例化适配器
                            MyAdapter adapter= new MyAdapter();
                            rv.setAdapter(adapter);
                            //刷新
                            adapter.notifyDataSetChanged();
                        }
                    });
                }
            }
        });

    }

    private void initView() {
        //管理器
        LinearLayoutManager manager=new LinearLayoutManager(this);
        // GridLayoutManager manager=new GridLayoutManager(this,2);
        rv.setLayoutManager(manager);
        //添加分割线
        rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));


    }
    //适配器
    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
          public static  final  int TYPR_ONE=0;
          public static  final  int TYPR_TWO=1;

       private   MyViewHolder holder1;
        private   MyViewHolder2 holder2;


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType==TYPR_ONE)
            {
                View view=View.inflate(MainActivity.this,R.layout.item1,null);
                holder1=new MyViewHolder(view);

                return  holder1;
            }
            else
            {
                View view2=View.inflate(MainActivity.this,R.layout.item2,null);
                holder2=new MyViewHolder2(view2);
                return  holder2;
            }

        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
             if(holder1 instanceof MyViewHolder)
             {
                 ImageLoader.getInstance().displayImage(bean.getData().getSubjects().get(position).getDescImage(),holder1.item1_img);
                 holder1.item1_tv.setText(bean.getData().getSubjects().get(position).getTitle());
                 ((MyViewHolder) holder1).itemView.setTag(position);
             }
            if(holder2 instanceof  MyViewHolder2)
            {
                ImageLoader.getInstance().displayImage(bean.getData().getSubjects().get(position).getDescImage(),holder2.item2_img);
                holder2.item2_tv.setText(bean.getData().getSubjects().get(position).getTitle());
                ((MyViewHolder2) holder2).itemView.setTag(position);
            }

        }

        @Override
        public int getItemCount() {

             //返回集合的长度,判断不为空
            return bean.getData().getSubjects()==null ? 0 :bean.getData().getSubjects().size();
        }

        @Override
        public int getItemViewType(int position) {
            if(position%2==0)
            {
                return  TYPR_ONE;
            }
            else{
                return TYPR_TWO;
            }

        }

        //创建ViewHolder内部类
        class MyViewHolder extends  RecyclerView.ViewHolder{
             //要使用的控件
               ImageView item1_img;
            TextView item1_tv;
            public MyViewHolder(View itemView) {
                super(itemView);
                //找到要使用的控件
                item1_img=itemView.findViewById(R.id.item1_img);
                item1_tv=itemView.findViewById(R.id.item1_tv);
            }
        }
        class MyViewHolder2 extends  RecyclerView.ViewHolder{
            //要使用的控件
            ImageView item2_img;
            TextView item2_tv;
            public MyViewHolder2(View itemView) {
                super(itemView);
                //找到要使用的控件
                item2_img=itemView.findViewById(R.id.item2_img);
                item2_tv=itemView.findViewById(R.id.item2_tv);
            }
        }
    }



}

本文由【waitig】发表在等英博客
本文固定链接:OkHttp请求数据到RecyclerView多条目展示
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)