private List<MyBean.DataBean> list;
private PullLoadMoreRecyclerView recyclerview;
private int page = 1;
private MyAdapter myAdapter;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String json = (String) msg.obj;
Gson gson = new Gson();
MyBean bean = gson.fromJson(json , MyBean.class);
list = bean.getData();
//设置布局管理器
recyclerview.setLinearLayout();
//设置适配器
myAdapter = new MyAdapter(MainActivity.this , list);
recyclerview.setAdapter(myAdapter);
//item条目点击
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this , list.get(position).getIntroduction() , Toast.LENGTH_SHORT).show();
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview = (PullLoadMoreRecyclerView) findViewById(R.id.recycler_view);
//上拉刷新下拉加载
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recyclerview.setPullLoadMoreCompleted();
}
},2000);
}
@Override
public void onLoadMore() {
page++;
jiexi();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recyclerview.setPullLoadMoreCompleted();
}
},2000);
}
});
jiexi();
}
//解析数据
private void jiexi() {
String url = "http://www.yulin520.com/a2a/impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=20&gender=2&ts=1871746850&page="+page;
OkHttp3Utils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Message message=new Message();
message.obj=string;
handler.sendMessage(message);
}
});
}
}
适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {
private Context context;
private List<MyBean.DataBean> list;
public MyAdapter(Context context, List<MyBean.DataBean> list) {
this.context = context;
this.list = list;
}
private OnItemClickListener onitemclicklistener = null;
//define interface
public static interface OnItemClickListener {
void onItemClick(View view , int position);
}
public void setOnitemclicklistener(OnItemClickListener onitemclicklistener) {
this.onitemclicklistener = onitemclicklistener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycleritem , parent , false);
MyViewHolder v = new MyViewHolder(view);
view.setOnClickListener(this);
return v;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Glide.with(context).load(list.get(position).getUserImg()).into(holder.imageView);
holder.age.setText(list.get(position).getUserAge()+"");
holder.name.setText(list.get(position).getUserName());
holder.title.setText(list.get(position).getIntroduction());
//将position保存在itemView的Tag中,以便点击时进行获取
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onClick(View v) {
if (onitemclicklistener != null){
onitemclicklistener.onItemClick(v , (Integer) v.getTag());
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView name,age,title;
public MyViewHolder(View itemView ) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.image_view);
age = (TextView) itemView.findViewById(R.id.text_age);
name = (TextView) itemView.findViewById(R.id.text_name);
title = (TextView) itemView.findViewById(R.id.text_title);
}
}
}
//定义一个点击事件的回调接口
public interface MyItemClickLIsten {
public void onItemClick(View view, int postion);
}