多布局带type值和不带type值的适配器

      他的这里的解析的基本流程都一样

//进行解析的页面
public class MainActivity extends AppCompatActivity {


    private RecyclerView recycler;
    private String a= "http://192.168.1.150:8080/jsonstring/array2.json";
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            String jsonstring = (String) msg.obj;

//解析


            Gson gson = new Gson();

//适配器

            NewsBean[] newsBeans = gson.fromJson(jsonstring, NewsBean[].class);

            NewAdapter adapter = new NewAdapter(newsBeans, MainActivity.this);

//这里是线性布局管理器

            LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this,                                                                                                                                     LinearLayoutManager.VERTICAL, false);
            recycler.setLayoutManager(manager);
            recycler.setAdapter(adapter);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    private void initData() {

//HttpURLConnection 进行解析

//new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    URL url = new URL(path);
//                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//                     conn.connect();
//                     if (conn.getResponseCode() == 200) {
//                         InputStream inputStream = conn.getInputStream();
//                         BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream));
//                         String temp = null;
//                         StringBuffer sb = new StringBuffer();
//                        while ((temp = buffer.readLine()) != null) {
//                            sb.append(temp);
//                         }
// 
// 
//                         handler.obtainMessage(100, sb.toString()).sendToTarget();
//                     }
// 
// 
// 
// 
//                 } catch (Exception e) {
//                     e.printStackTrace();
//                 }
//             }

//         }).start();

//进行OkHttpClient 解析

new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request build = new Request.Builder().url(a).build();
                Call call = okHttpClient.newCall(build);
                try {
                    Response execute = call.execute();
                    String string = execute.body().string();
                    Message message = new Message();
                    message.obj=string;


                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

       
    }


    private void initView() {
        recycler = (RecyclerView) findViewById(R.id.recycler);
    }

}

//带type值的

public class NewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private NewsBean[] newsBeans;
    private Context mContext;


    public NewAdapter(NewsBean[] newsBeans, Context mContext) {
        this.newsBeans = newsBeans;
        this.mContext = mContext;
    }
//返回item的类型
    @Override
    public int getItemViewType(int position) {
        return newsBeans[position].getType();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                View firstview = LayoutInflater.from(mContext).inflate(R.layout.item_first, parent, false);
                holder = new FirstViewHolder(firstview);
                break;
            case 1:
                View otherview = LayoutInflater.from(mContext).inflate(R.layout.item_other, parent, false);
                holder = new OtherViewHolder(otherview);
                break;
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NewsBean newsBean = newsBeans[position];
        int type = newsBean.getType();


        switch (type) {
            case 0:
                ((FirstViewHolder) holder).titleTv.setText(newsBean.getTitle());
                String path0 = newsBean.getPic().get(0).getThumbnail_pic();
                String path1 = newsBean.getPic().get(1).getThumbnail_pic();
                String path2 = newsBean.getPic().get(2).getThumbnail_pic();


                Picasso.with(mContext).load(path0).into(((FirstViewHolder) holder).pic0_im);
                Picasso.with(mContext).load(path1).into(((FirstViewHolder) holder).pic1_im);
                Picasso.with(mContext).load(path2).into(((FirstViewHolder) holder).pic2_im);


                break;
            case 1:
                String otherpath0 = newsBean.getPic().get(0).getThumbnail_pic();
                Picasso.with(mContext).load(otherpath0).into(((OtherViewHolder) holder).pic0_im);
                ((OtherViewHolder) holder).titleTv.setText(newsBean.getTitle());
                break;




        }
    }


    @Override
    public int getItemCount() {
        return newsBeans.length;
    }




    //type  =0
    class FirstViewHolder extends RecyclerView.ViewHolder {


        private final ImageView pic0_im;
        private final ImageView pic1_im;
        private final ImageView pic2_im;
        private final TextView titleTv;


        public FirstViewHolder(View itemView) {
            super(itemView);


            pic0_im = itemView.findViewById(R.id.im_pic0);
            pic1_im = itemView.findViewById(R.id.im_pic1);
            pic2_im = itemView.findViewById(R.id.im_pic2);
            titleTv = itemView.findViewById(R.id.tv_title);
        }
    }


    // type=1
    class OtherViewHolder extends RecyclerView.ViewHolder {


        private final TextView titleTv;
        private final ImageView pic0_im;


        public OtherViewHolder(View itemView) {
            super(itemView);


            pic0_im = itemView.findViewById(R.id.im_pic0);
            titleTv = itemView.findViewById(R.id.tv_title);
        }
    }

}

//不带type值得

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{


    private List<User.ResultBean.DataBean> data;
    private Context mc;
    private onlistener listener;


    public MyAdapter(List<User.ResultBean.DataBean> data, Context mc) {
        this.data = data;
        this.mc = mc;
    }


    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mc).inflate(R.layout.buju1, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    //这里是判断解析的串里有几张图片

    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, final int position) {
        if (TextUtils.isEmpty(data.get(position).getThumbnail_pic_s02())&&
            TextUtils.isEmpty(data.get(position).getThumbnail_pic_s03())){
            Picasso.with(mc).load(data.get(position).getThumbnail_pic_s()).into(holder.img1);
            holder.img2.setVisibility(View.GONE);
            holder.img3.setVisibility(View.GONE);
            holder.title.setText(data.get(position).getTitle());


        }else if (TextUtils.isEmpty(data.get(position).getThumbnail_pic_s03())){
            Picasso.with(mc).load(data.get(position).getThumbnail_pic_s()).into(holder.img1);
            Picasso.with(mc).load(data.get(position).getThumbnail_pic_s02()).into(holder.img2);
            holder.img3.setVisibility(View.GONE);
            holder.title.setText(data.get(position).getTitle());
        }else {
            Picasso.with(mc).load(data.get(position).getThumbnail_pic_s()).into(holder.img1);
            Picasso.with(mc).load(data.get(position).getThumbnail_pic_s02()).into(holder.img2);
            Picasso.with(mc).load(data.get(position).getThumbnail_pic_s03()).into(holder.img3);
            holder.title.setText(data.get(position).getTitle());

        }

//点击事件当你点击item返回给你的position值

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.itemlistener(position);
            }
        });
    }


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


    public class ViewHolder extends RecyclerView.ViewHolder {


        private final ImageView img3;
        private final ImageView img1;
        private final ImageView img2;
        private final TextView title;


        public ViewHolder(View itemView) {
            super(itemView);
            img1 = itemView.findViewById(R.id.img1);
            img2 = itemView.findViewById(R.id.img2);
            img3 = itemView.findViewById(R.id.img3);
            title = itemView.findViewById(R.id.title);
        }

    }

//这里是给它的每条item

    public interface onlistener{
        void itemlistener(int position);
    }
    public void setonlistener(onlistener listener){
        this.listener=listener;
    }
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页