自己总结的Recyclerview加载不同布局

package cn.lizhangqu.kb.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import java.net.ConnectException;
import java.net.PortUnreachableException;
import java.util.ArrayList;
import java.util.List;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.datas.PopularCourseview_Bean;
import cn.lizhangqu.kb.datas.Recommend_Bean;
import cn.lizhangqu.kb.viewholder.commonViewHoder;

/**
 * Created by xuzhaokai on 2016/12/11.
 */

public class myPopularCourseAdapter extends RecyclerView.Adapter {

    private Context context;
    private List datas = new ArrayList<>();//数据集
    private CommonAdapter Function_Adapter;//自己定义的GridView的adapter

    public static final int TYPE_ONE = 0;
    public static final int TYPE_TWO = 1;
    public static final int TYPE_THREE = 2;//三种不同的布局

    public myPopularCourseAdapter(Context context, List datas) {
        this.context = context;
        this.datas = datas;
    }

    public int getItemViewType(int position) {  //poistion 为当前显示的布局 
        if (position == 0) {
            return TYPE_ONE;//第一种布局
        } else if (position == 1) {
            return TYPE_ONE;//第一种布局
        } else if (position == 2) {
            return TYPE_THREE;//第三种布局
        } else if (position == 3) {
            return TYPE_THREE;//第三种布局
        } else if (position == 4) {
            return TYPE_TWO;//第2种布局
        } else if (position == 5) {
            return TYPE_TWO;//第2种布局
        }
        return super.getItemViewType(position);
    }

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ONE:
                return new MyViewHolderOne(LayoutInflater.from(parent.getContext()).inflate(R.layout
                        .popularcourse_content_friststyle, parent, false));
            case TYPE_TWO:
                return new MyViewHolderTwo(LayoutInflater.from(parent.getContext()).inflate(R.layout.
                        popularcourse_content_secondstyle, parent, false));
            case TYPE_THREE:
                return new MyViewHolderThree(LayoutInflater.from(parent.getContext()).inflate(R.layout.
                        popularcourse_content_threestyle, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolderOne) {
            bindTypeOne((MyViewHolderOne) holder, position);
        } else if (holder instanceof MyViewHolderTwo) {
            bindTypeTwo((MyViewHolderTwo) holder, position);
        } else if (holder instanceof MyViewHolderThree) {
            bindTypeThree((MyViewHolderThree) holder, position);
        }
    }


    @Override
    public int getItemCount() {
        return 5;//我要显示多少个Item
    }


    public class MyViewHolderOne extends RecyclerView.ViewHolder {
        private GridView gridView;
        private TextView textView;

        public MyViewHolderOne(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.popular_content_friststyle_title);
            gridView = (GridView) itemView.findViewById(R.id.id_popular_course_content_frist_gridview);
        }
    }

    public class MyViewHolderTwo extends RecyclerView.ViewHolder {
        private GridView gridView;
        private TextView textView;

        public MyViewHolderTwo(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.popularcoures_content_secondstyle_title);
            gridView = (GridView) itemView.findViewById(R.id.id_popularcoures_content_secondstyle_gridview);
        }
    }

    public class MyViewHolderThree extends RecyclerView.ViewHolder {

        private RecyclerView recyclerView;
        private TextView textView;

        public MyViewHolderThree(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.popularcoures_content_three_style_rev);
            textView = (TextView) itemView.findViewById(R.id.popularcoures_content_three_style_title);
        }
    }


    private void bindTypeOne(MyViewHolderOne holderOne, int position) {
        List popularCourseview_been = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getWeight() == 1) {
                popularCourseview_been.add(datas.get(i));
            }
        }
        Log.e("testdatasize", "----------------------------" + popularCourseview_been.size());

        holderOne.textView.setText("第一个");
        initPupularcoursefristAdapter(popularCourseview_been, holderOne);
    }

    private void bindTypeTwo(MyViewHolderTwo holderTwo, int position) {
        List popularCourseview_been = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getWeight() == 2) {
                popularCourseview_been.add(datas.get(i));
            }
        }
        Log.e("testdatasize2", "----------------------------" + popularCourseview_been.size());
        holderTwo.textView.setText("第二");
        initPupularcourseSecondAdapter(popularCourseview_been, holderTwo);
    }

    private void bindTypeThree(MyViewHolderThree holder, int position) {  //在其中镶嵌一个RecyclerView
        HorizonSlidePopularCouresAdapter p = new HorizonSlidePopularCouresAdapter(datas, context);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 1);
        gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        holder.recyclerView.setLayoutManager(gridLayoutManager);
        holder.recyclerView.setItemAnimator(new DefaultItemAnimator());
        holder.recyclerView.setAdapter(p);
        holder.textView.setText("disangge");

    }


    private void initPupularcoursefristAdapter(List Pupularcourse_datas, MyViewHolderOne holderOne) {
        Function_Adapter = new CommonAdapter(context, Pupularcourse_datas, R.layout.popular_course_second_item_stlye) {
            @Override
            public void convert(commonViewHoder hoder, PopularCourseview_Bean Bean) {
                hoder.setText(R.id.tv_popular_course_second_item_style, Bean.getTitle()).setImagerSimpleDraweeView(R.id.iv_popular_course_second_item_style, Bean.getIconUrl());
            }
        };
        holderOne.gridView.setAdapter(Function_Adapter);
//        FunctionGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView parent, View view, int position, long id) {
//                switch (position){
//                    case 0:mCallback.Course_function_Click(0);break;
//                    case 1:mCallback.Course_function_Click(1);break;
//                    case 2:mCallback.Course_function_Click(2);break;
//                    case 3:mCallback.Course_function_Click(3);break;
//                    case 4:mCallback.Course_function_Click(4);break;
//                    case 5:mCallback.Course_function_Click(5);break;
//                    case 6:mCallback.Course_function_Click(6);break;
//                    case 7:mCallback.Course_function_Click(7);break;
//                    default:break;
//
//                }
//            }
//        });
    }

    private void initPupularcourseSecondAdapter(List Pupularcourse_datas, //这个是我自定义的网格按钮的adapter
                                                MyViewHolderTwo holderTwo) {
        Function_Adapter = new CommonAdapter(context, Pupularcourse_datas,
                R.layout.popular_course_frist_item_stlye) {
            @Override
            public void convert(commonViewHoder hoder, PopularCourseview_Bean Bean) {
                hoder.setText(R.id.tv_popular_course_frist_style, Bean.getTitle()).
                        setImagerSimpleDraweeView(R.id.iv_popular_course_frist_style, Bean.getIconUrl());
            }
        };
        holderTwo.gridView.setAdapter(Function_Adapter);
//        FunctionGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView parent, View view, int position, long id) {
//                switch (position){
//                    case 0:mCallback.Course_function_Click(0);break;
//                    case 1:mCallback.Course_function_Click(1);break;
//                    case 2:mCallback.Course_function_Click(2);break;
//                    case 3:mCallback.Course_function_Click(3);break;
//                    case 4:mCallback.Course_function_Click(4);break;
//                    case 5:mCallback.Course_function_Click(5);break;
//                    case 6:mCallback.Course_function_Click(6);break;
//                    case 7:mCallback.Course_function_Click(7);break;
//                    default:break;
//
//                }
//            }
//        });
    }


}

你可能感兴趣的:(自己总结的Recyclerview加载不同布局)