//依赖
//主代码
//适配器
compile 'com.jcodecraeer:xrecyclerview:1.2.0'
//布局
<?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="activity.example.com.xrecyclerview.MainActivity">
- <com.jcodecraeer.xrecyclerview.XRecyclerView
- android:id="@+id/xrecyclerview"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"></com.jcodecraeer.xrecyclerview.XRecyclerView>
- </RelativeLayout>
- package activity.example.com.xrecyclerview;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.LinearLayoutManager;
- import com.jcodecraeer.xrecyclerview.ProgressStyle;
- import com.jcodecraeer.xrecyclerview.XRecyclerView;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class MainActivity extends AppCompatActivity {
- private XRecyclerView xrecyclerview;
- private int i=0;
- private List<ListBean> list;
- private XRAdapter xrAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //获取id
- xrecyclerview = (XRecyclerView)findViewById(R.id.xrecyclerview);
- //设置可上拉
- xrecyclerview.setPullRefreshEnabled(true);
- xrecyclerview.setLoadingMoreEnabled(true);
- //设置上拉下拉样式
- xrecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
- xrecyclerview.setLaodingMoreProgressStyle(ProgressStyle.BallClipRotate);
- // 线性布局管理器 VERTICAL默认样式/竖向显示 第三个参数是数据是否到过来显示
- LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
- //添加布局管理器
- xrecyclerview.setLayoutManager(manager);
- //创建数据集合
- list = new ArrayList<>();
- //设置适配器
- xrAdapter = new XRAdapter(MainActivity.this, list);
- xrecyclerview.setAdapter(xrAdapter);
- //设置监听
- xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
- @Override
- public void onRefresh() {
- i=0;
- list.clear();
- loaddata(i);
- xrecyclerview.refreshComplete();
- }
- @Override
- public void onLoadMore() {
- i++;
- loaddata(i);
- xrecyclerview.loadMoreComplete();
- }
- });
- //加载数据
- loaddata(i);
- }
- private void loaddata(int i) {
- Map<String, String> map = new HashMap<>();
- HttpUtils.getInstance().get("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset="+i,map,new CallBack(){
- @Override
- public void onSuccess(Object o) {
- Bean bean = (Bean)o;
- List<Bean.SongListBean> song_list = bean.getSong_list();
- for(int i=0;i<song_list.size();i++){
- list.add(new ListBean(song_list.get(i).getAlbum_1000_1000(),song_list.get(i).getAuthor()));
- }
- xrAdapter.notifyDataSetChanged();
- }
- @Override
- public void onFailed(Exception e) {
- }
- },Bean.class);
- }
- }
- package activity.example.com.xrecyclerview;
- import android.content.Context;
- import android.support.v7.widget.RecyclerView;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.bumptech.glide.Glide;
- import java.util.List;
- public class XRAdapter extends RecyclerView.Adapter<XRAdapter.ViewHolder>{
- private Context context;
- private List<ListBean> list;
- public XRAdapter(Context context, List<ListBean> list) {
- this.context = context;
- this.list = list;
- }
- //创建ViewHolder
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- //子条目布局
- View view = View.inflate(context, R.layout.item, null);
- ViewHolder holder = new ViewHolder(view);
- return holder;
- }
- //绑定ViewHolder,把数据和视图进行绑定
- @Override
- public void onBindViewHolder(ViewHolder holder, final int position) {
- Glide.with(context).load(list.get(position).getImg()).into(holder.image);
- holder.tv_title.setText(list.get(position).getName());
- }
- @Override
- public int getItemCount() {
- return list.size();
- }
- class ViewHolder extends RecyclerView.ViewHolder{
- private TextView tv_title;
- private ImageView image;
- public ViewHolder(View itemView) {
- super(itemView);
- //itemView一个条目的视图
- image = (ImageView)itemView.findViewById(R.id.image);
- tv_title = (TextView)itemView.findViewById(R.id.tv_title);
- }
- }
- }