MVP实现xRecyclerview多条目加载

 
  
public interface IRecyModel {
   void recy(int type, Callback callback);
}
================================================================
public class RecyModel implements IRecyModel {
    @Override
    public void recy(int type, Callback callback) {
        OkHttpUtils.getInstance().doGet("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+type+"&size=10&offset=0",callback);

    }
}
=================================================================
public interface IRecyView {
    void showrecy(RecyBean rb);
    int getID(); 


}
==================================================================
public class RecyPresenter {
    IRecyModel model;
    IRecyView  iRecyView;
    Context context;

    public RecyPresenter(IRecyView iRecyView, Context context) {
        this.iRecyView = iRecyView;
        this.context = context;
        model=new RecyModel();
    }
    //请求数据的方法
    public void Showrecy(){
      int type=iRecyView.getID();
       model.recy(type, new OnUiCallback() {
           @Override
           public void onFailed(Call call, IOException e) {

           }

           @Override
           public void onSuccess(String result) throws IOException {
               RecyBean rb=new Gson().fromJson(result,RecyBean.class);
               iRecyView.showrecy(rb);
           }
       });
    }
}
============================================================================
public class MyAdapter extends RecyclerView.Adapter {

    Context context;
    RecyBean rb;

    public MyAdapter(Context context, RecyBean rb) {
        this.context = context;
        this.rb = rb;
    }
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;

    List listHeader = new ArrayList<>();
    public void addHeader(View view){
        listHeader.add(view);
    }

    OnItemClickListener listener;
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType == TYPE_HEADER){
            View view = View.inflate(context, R.layout.recy_header1,null);
            MyHeaderHolder headerHolder = new MyHeaderHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(view, (Integer) view.getTag());
                }
            });
            return headerHolder;
        }else if(viewType == TYPE_ONE){
            View view = View.inflate(context, R.layout.recy_item1,null);
            MyViewHolder holder = new MyViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(view, (Integer) view.getTag());
                }
            });
            return holder;
        }else{
            View view = View.inflate(context, R.layout.recy_item11,null);
            MyViewHolder1 holder1 = new MyViewHolder1(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(view, (Integer) view.getTag());
                }
            });
            return holder1;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof MyHeaderHolder){
            holder.itemView.setTag(position);
            ((MyHeaderHolder) holder).header_title.setText(rb.getBillboard().getName());
            ((MyHeaderHolder) holder).header_info.setText(rb.getBillboard().getComment());
            ImageLoader.getInstance().displayImage(rb.getBillboard().getPic_s192(),((MyHeaderHolder) holder).header_iv);
        }
        if(holder instanceof MyViewHolder){
            holder.itemView.setTag(position);
            ((MyViewHolder) holder).author.setText(rb.getSong_list().get(position).getAuthor());
//            ((MyViewHolder) holder).name.setText(rc.getSong_list().get(position).getArtist_name());
            ((MyViewHolder) holder).title.setText(rb.getSong_list().get(position).getAlbum_title());
            ImageLoader.getInstance().displayImage(rb.getSong_list().get(position).getPic_big(),((MyViewHolder) holder).iv);
        }
        if(holder instanceof MyViewHolder1){
            holder.itemView.setTag(position);
            ((MyViewHolder1) holder).author1.setText(rb.getSong_list().get(position).getAuthor());
//            ((MyViewHolder1) holder).name1.setText(rc.getSong_list().get(position).getArtist_name());
            ((MyViewHolder1) holder).title1.setText(rb.getSong_list().get(position).getAlbum_title());
            ImageLoader.getInstance().displayImage(rb.getSong_list().get(position).getPic_big(),((MyViewHolder1) holder).iv1);
        }
    }

    @Override
    public int getItemViewType(int position) {

        if(position < listHeader.size()){
            return TYPE_HEADER;
        }else if(position %2 == 0){
            return TYPE_ONE;
        }else{
            return TYPE_TWO;
        }
    }

    @Override
    public int getItemCount() {
        return rb.getSong_list() == null ? 0 : rb.getSong_list().size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView title,name,author;
        ImageView iv;

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

            author = itemView.findViewById(R.id.author);
            name = itemView.findViewById(R.id.name);
            title = itemView.findViewById(R.id.title);
            iv = itemView.findViewById(R.id.iv);

        }
    }

    class MyHeaderHolder extends RecyclerView.ViewHolder{


        TextView header_title,header_name,header_info,header_author;
        ImageView header_iv;

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

            header_author = itemView.findViewById(R.id.header_author);
            header_info = itemView.findViewById(R.id.header_info);
            header_name = itemView.findViewById(R.id.header_name);
            header_title = itemView.findViewById(R.id.header_title);
            header_iv = itemView.findViewById(R.id.header_iv);

        }
    }

    class MyViewHolder1 extends RecyclerView.ViewHolder{

        TextView title1,name1,author1;
        ImageView iv1;

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

            author1 = itemView.findViewById(R.id.author1);
            name1 = itemView.findViewById(R.id.name1);
            title1 = itemView.findViewById(R.id.title1);
            iv1 = itemView.findViewById(R.id.iv1);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }
}

===================================================================
public class InfoActivity extends AppCompatActivity implements IRecyView {

    private XRecyclerView mXrv;
    private RecyPresenter presenter;
    int type=21;
    private String s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        initView();
        presenter=new RecyPresenter(this,this);
        presenter.Showrecy();
        //System.out.println(s.equals("any string"));
    }

    private void initView() {
        mXrv = (XRecyclerView) findViewById(R.id.xrv);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        mXrv.setLayoutManager(manager);
        mXrv.setLoadingMoreEnabled(true);

    }

    @Override
    public void showrecy(RecyBean rb) {
        MyAdapter adapter = new MyAdapter(InfoActivity.this,rb);
        TextView tv = new TextView(this);
        adapter.addHeader(tv);
        mXrv.setAdapter(adapter);

        adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(InfoActivity.this,"1112",Toast.LENGTH_SHORT).show();

            }
        });

        mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                type++;
                presenter.Showrecy();
                mXrv.refreshComplete();



            }

            @Override
            public void onLoadMore() {

            }
        });

    }

    @Override
    public int getID() {
        return type;
    }
}


 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(MVP实现xRecyclerview多条目加载)