效果图:
首先要添加依赖:
//xrecyclerview
implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
//Gson
implementation 'com.google.code.gson:gson:2.2.4'
//okhttputils
implementation 'com.zhy:okhttputils:2.0.0'
权限:
<uses-permission android:name="android.permission.INTERNET" />
布局:
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/RV_all"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.jcodecraeer.xrecyclerview.XRecyclerView>
All_Fragment:
public class All_Fragment extends Fragment {
private XRecyclerView RV_all;//XRecyclerView
private int i=0;//页数
private Adapter_All adapter_all;//适配器
private ArrayList<Bean_All.MessageBean.AnchorsBean> data;//把获取的数据放到集合里
private int con=0;//判断con是否等于0等于0就执行适配器和瀑布流con不等于0就刷新界面
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_all_, container, false);
initView(inflate);
data = new ArrayList<>();
//okhttp解析
okhttp(i);
return inflate;
}
private void okhttp(final int i) {
OkHttpUtils.get()
.url(Cans.HOME_PAGE + "" + i + "&index=0")//地址
.build().execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
Log.e("Song-All-onError", request + "");
}
@Override
public void onResponse(String response) {
Log.e("Song-All-onResponse", response + "");
//解析所有数据
all(response);
}
});
}
//解析所有数据
private void all(String response) {
//Gson解析
Gson gson = new Gson();
//获取Bean类的数据
Bean_All bean_all = gson.fromJson(response, Bean_All.class);
final List<Bean_All.MessageBean.AnchorsBean> anchors = bean_all.getMessage().getAnchors();
//把Bean类获取的数据放到集合中
data.addAll(anchors);
//判断con是否等于0等于0就执行适配器和瀑布流con不等于0就刷新界面
if (con == 0){
//瀑布流
RV_all.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
//适配器
adapter_all = new Adapter_All(data, getActivity());
//添加适配器
RV_all.setAdapter(adapter_all);
}else {
adapter_all.notifyDataSetChanged();//刷新界面
}
//XRecyclerView的点击事件
adapter_all.setmOnItemListener(new Adapter_RecyclerView.OnItemListener() {
@Override
public void onItemClick(int position) {
//Intent跳转
Intent intent = new Intent(getActivity(), All_Activity.class);
intent.putExtra("roomid", data.get(position).getRoomid());
startActivity(intent);
}
});
/**
*设定下拉刷新和上拉加载监听
*/
RV_all.setLoadingListener(new XRecyclerView.LoadingListener() {
//上拉加载监听
@Override
public void onLoadMore() {
i++;//上拉加载添加数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
con=1;//con不等于0就执行上啦加载
okhttp(i);//okhttp解析数据
RV_all.loadMoreComplete(); //加载数据完成(取消加载动画)
}
},2000);
}
//下拉刷新监听
@Override
public void onRefresh() {
//清除集合
data.clear();
okhttp(0);//初始化数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
adapter_all.notifyDataSetChanged();//刷新界面
RV_all.refreshComplete(); //刷新数据完成(取消刷新动画)
}
},2000);
}
});
}
private void initView(View inflate) {
RV_all = (XRecyclerView) inflate.findViewById(R.id.RV_all);
}
}
适配器:
public class Adapter_All extends XRecyclerView.Adapter<Adapter_All.ViewHolder>{
private List<Bean_All.MessageBean.AnchorsBean> list;
private Context context;
public Adapter_All(List<Bean_All.MessageBean.AnchorsBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public Adapter_All.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.layout_all, parent, false);
ViewHolder viewHolder = new ViewHolder(inflate);
return viewHolder;
}
@Override
public void onBindViewHolder(Adapter_All.ViewHolder holder, int position) {
Glide.with(context).load(list.get(position).getPic74()).into(holder.img_all);
holder.tv_name.setText(list.get(position).getName());
holder.tv_online.setText(""+list.get(position).getFocus()+"人");
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final ImageView img_all;
private final TextView tv_name;
private final TextView tv_online;
public ViewHolder(View itemView) {
super(itemView);
img_all = itemView.findViewById(R.id.img_all);
tv_name = itemView.findViewById(R.id.tv_name);
tv_online = itemView.findViewById(R.id.tv_online);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnItemListener!=null){
mOnItemListener.onItemClick(getPosition());
}
}
});
}
}
public interface OnItemListener{
void onItemClick(int position);
}
public Adapter_RecyclerView.OnItemListener mOnItemListener;
public void setmOnItemListener(Adapter_RecyclerView.OnItemListener mOnItemListener){
this.mOnItemListener = mOnItemListener;
}
}