xRecyclerView的刷新,加载更多,多条目

导入依赖:compile 'com.jcodecraeer:xrecyclerview:1.3.2'

--------适配器--------------
package com.example.zhengwenjie20171021.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zhengwenjie20171021.R;
import com.example.zhengwenjie20171021.bean.JavaBean;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by zwj on 2017/10/21.
 */

public class MyAdapter extends RecyclerView.Adapter {

    private Context context;
    private List song_list;
    private static final int TYPE1 = 1;
    private static final int TYPE2 = 2;
    /**
     * 设置接口回调
     */
    private OnListener onListener;

    public interface OnListener {
        public void onItemClick(JavaBean.SongListBean listBean);
    }

    public void setOnListener(OnListener onListener) {
        this.onListener = onListener;
    }

    /**
     * 构造器
     * @param context
     * @param song_list
     */
    public MyAdapter(Context context, List song_list) {
        this.context = context;
        this.song_list = song_list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == TYPE1) {
            View view = LayoutInflater.from(context).inflate(R.layout.music_item, parent, false);
            viewHolder = new MyViewHolder(view);
        } else {
            View view1 = LayoutInflater.from(context).inflate(R.layout.music_item2, parent, false);
            viewHolder = new MyViewHolder1(view1);
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder){
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            final JavaBean.SongListBean songListBean = song_list.get(position);
            myViewHolder.tv_author.setText(songListBean.author);
            myViewHolder.tv_album.setText(songListBean.album_title);
            myViewHolder.iv.setImageURI(songListBean.pic_small);
            myViewHolder.ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onListener != null) {
                        onListener.onItemClick(songListBean);
                    }
                }
            });
        }else{
            MyViewHolder1 myViewHolder1= (MyViewHolder1) holder;
            final JavaBean.SongListBean songListBean = song_list.get(position);
            myViewHolder1.tv_author2.setText(songListBean.author);
            myViewHolder1.tv_album2.setText(songListBean.album_title);
            myViewHolder1.ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onListener!=null){
                        onListener.onItemClick(songListBean);
                    }
                }
            });

        }


    }

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

    /**
     * 多条目判断
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        String copy_type = song_list.get(position).copy_type;
        if (copy_type.equals("1")) {
            return TYPE1;
        } else {
            return TYPE2;
        }
    }

    /**
     * 初始化组件
     */
    public class MyViewHolder1 extends RecyclerView.ViewHolder {
        private final TextView tv_album2;
        private final TextView tv_author2;
        private final LinearLayout ll1;

        public MyViewHolder1(View itemView) {
            super(itemView);
            tv_author2 = itemView.findViewById(R.id.tv_author2);
            tv_album2 = itemView.findViewById(R.id.tv_album2);
            ll1 = itemView.findViewById(R.id.ll);
        }
    }

    /**
     * 初始化组件
     */
    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_author;
        private final SimpleDraweeView iv;
        private final TextView tv_album;
        private final LinearLayout ll;


        public MyViewHolder(View itemView) {
            super(itemView);
            tv_author = itemView.findViewById(R.id.tv_author);
            iv = itemView.findViewById(R.id.iv);
            tv_album = itemView.findViewById(R.id.tv_album);
            ll = itemView.findViewById(R.id.ll);

        }
    }

}

--------------Activity-----------------------------
 
  
package com.example.zhengwenjie20171021.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.example.zhengwenjie20171021.R;
import com.example.zhengwenjie20171021.adapter.MyAdapter;
import com.example.zhengwenjie20171021.bean.JavaBean;
import com.example.zhengwenjie20171021.net.HttpUtil;
import com.example.zhengwenjie20171021.net.OnNetListener;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

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

public class MusicActivity extends AppCompatActivity {

    private XRecyclerView mXrv;
    private List song_list;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        initView();
        getData();
        getRefreshLoadDate();

    }

    /**
     * 设置数据
     */
    public void getData() {
        HttpUtil.getInstance(this).doGet("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0", JavaBean.class, new OnNetListener() {


            @Override
            public void onSuccess(JavaBean javaBean) throws IOException {
                song_list = javaBean.song_list;
                mXrv.setLayoutManager(new LinearLayoutManager(MusicActivity.this));
                myAdapter = new MyAdapter(MusicActivity.this, song_list);
                mXrv.setAdapter(myAdapter);
                myAdapter.setOnListener(new MyAdapter.OnListener() {
                    @Override
                    public void onItemClick(JavaBean.SongListBean listBean) {
                        Toast.makeText(MusicActivity.this,"点击了",Toast.LENGTH_SHORT).show();
                    }
                });

            }

            @Override
            public void onError(IOException e) {

            }
        });
    }

    /**
     * 设置刷新操作
     */
    public void setRefresh() {

        HttpUtil.getInstance(MusicActivity.this).doGet("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=11&size=10&offset=0", JavaBean.class, new OnNetListener() {
            @Override
            public void onSuccess(JavaBean javaBean) throws IOException {
                List list = javaBean.song_list;
                song_list.clear();
                song_list.addAll(list);
                myAdapter.notifyDataSetChanged();
                mXrv.refreshComplete();
            }

            @Override
            public void onError(IOException e) {

            }
        });

    }



    /**
     * 加载更多
     */
    public void setLoad() {

        HttpUtil.getInstance(MusicActivity.this).doGet("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=22&size=10&offset=0", JavaBean.class, new OnNetListener() {
            @Override
            public void onSuccess(JavaBean javaBean) throws IOException {
                List list = javaBean.song_list;
                song_list.addAll(list);
                myAdapter.notifyDataSetChanged();
                mXrv.loadMoreComplete();
            }

            @Override
            public void onError(IOException e) {

            }
        });

    }

    /**
     * 加载和刷新操作
     */
    public void getRefreshLoadDate() {
        //加载和刷新的方法
        mXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                setRefresh();
            }

            @Override
            public void onLoadMore() {
                setLoad();
            }
        });
    }

    private void initView() {
        mXrv = (XRecyclerView) findViewById(R.id.xrv);

    }
}

 
 

你可能感兴趣的:(Android)