第一步
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
第二步
在布局文件中使用
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.jcodecraeer.xrecyclerview.XRecyclerView
第三步在main中使用
xRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
adapter = new MainAdapter(MainActivity.this, song_list);
xRecyclerView.setAdapter(adapter);
第四步添加刷新和下拉的方法
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "下拉", Toast.LENGTH_SHORT).show();
// 当您停止刷新或加载更多的工作
xRecyclerView.refreshComplete();
}
@Override
public void onLoadMore() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 通知加载更多的工作完成
xRecyclerView.loadMoreComplete();
Toast.makeText(MainActivity.this, "shanfsad", Toast.LENGTH_SHORT).show();
}
});
第五步
适配的写法和recycleview是一样的
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
Context context;
List<Homebean.DataBean.Ad5Bean> ad5;
public HomeAdapter(Context context, List<Homebean.DataBean.Ad5Bean> ad5) {
this.context = context;
this.ad5 = ad5;
}
@Override
public HomeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
context).inflate(R.layout.home_item_grid_center, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(HomeAdapter.MyViewHolder holder, int position) {
holder.tv.setText(ad5.get(position).getTitle());
Glide.with(context).load(ad5.get(position).getImage()).into(holder.imageView);
}
@Override
public int getItemCount() {
return ad5.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
ImageView imageView;
public MyViewHolder(View view) {
super(view);
tv = (TextView) view.findViewById(R.id.home_item_grid_center_textView);
imageView = (ImageView) view.findViewById(R.id.home_item_grid_center_image);
}
}
}