RecycleView适配器单条目编辑展示

package com.example.pandademo.view.pandaguanchaadapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.pandademo.R;
import com.example.pandademo.view.pandaguanchabean.AllBean;
import com.example.pandademo.view.pandaguanchabean.PandaGuanChaListBean;
import com.google.gson.Gson;

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

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyPandaGuanchaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public List<AllBean> list;
    private List<PandaGuanChaListBean.ListBean> listPandaList;
    private Context mContext;
    int BIG_IMAGE = 1;
    int RECYCLER_PANDAGUANCHA = 2;

    public MyPandaGuanchaAdapter(List<AllBean> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case 1:
                View view1 = LayoutInflater.from(mContext).inflate(R.layout.item_pandaguancha_bigimg, parent, false);
                viewHolder = new My_Panda_guancha_BigImg(view1);
                break;
            case 2:
                View view2 = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_pandaguancha, parent, false);
                viewHolder = new My_Panda_guancha_recycler(view2);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof My_Panda_guancha_BigImg) {
            Glide.with(mContext).load(list.get(0).getData().getBigImg().get(0).getImage()).into(((My_Panda_guancha_BigImg) holder).item_pandaguancha_bigimg);
            ((My_Panda_guancha_BigImg) holder).item_pandaguancha_title.setText(list.get(0).getData().getBigImg().get(0).getTitle());
        }else if (holder instanceof My_Panda_guancha_recycler){
            String listurl = list.get(0).getData().getListurl();
            OkHttpClient client = new OkHttpClient.Builder().build();
            Request request = new Request.Builder().url(listurl).build();
            client.newCall(request).enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String string = response.body().string();
                    Gson gson = new Gson();
                    PandaGuanChaListBean pandaGuanChaListBean = gson.fromJson(string, PandaGuanChaListBean.class);
                    listPandaList = pandaGuanChaListBean.getList();

                    ((Activity) mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MyPandaGuanchaRecyclerAdapter myPandaGuanchaRecyclerAdapter = new MyPandaGuanchaRecyclerAdapter(listPandaList,mContext);
                            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
                            ((My_Panda_guancha_recycler) holder).recycler_pandaguancha.setLayoutManager(linearLayoutManager);
                            ((My_Panda_guancha_recycler) holder).recycler_pandaguancha.setAdapter(myPandaGuanchaRecyclerAdapter);
                        }
                    });
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return BIG_IMAGE;
        } else if (position == 1) {
            return RECYCLER_PANDAGUANCHA;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    public class My_Panda_guancha_BigImg extends RecyclerView.ViewHolder {

        private final ImageView item_pandaguancha_bigimg;
        private final TextView item_pandaguancha_title;

        public My_Panda_guancha_BigImg(View itemView) {
            super(itemView);
            item_pandaguancha_bigimg = itemView.findViewById(R.id.item_pandaguancha_bigimg);
            item_pandaguancha_title = itemView.findViewById(R.id.item_pandaguancha_title);
        }
    }

    public class My_Panda_guancha_recycler extends RecyclerView.ViewHolder {

        private final RecyclerView recycler_pandaguancha;

        public My_Panda_guancha_recycler(View itemView) {
            super(itemView);
            recycler_pandaguancha = itemView.findViewById(R.id.recycler_pandaguancha);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41832319/article/details/80794362