//依赖
compile 'com.google.code.gson:gson:2.8.0' compile 'com.jcodecraeer:xrecyclerview:1.3.2' compile 'com.squareup.okhttp:okhttp:2.4.0' compile 'com.github.bumptech.glide:glide:3.7.0'<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//Activity代码
public class MainActivity extends AppCompatActivity { int page=03; private String uri="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=201703"+page+""; private List<Bean.ResultBean.RowsBean> list; private XRecyclerView xRecyclerView; private RecyclerAdapter adapter; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xRecyclerView = (XRecyclerView) findViewById(R.id.recycler); xRecyclerView.setLayoutManager(new LinearLayoutManager(this)); setDate(1); xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() { public void onRefresh() { page=page++; uri="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=201703"+page+""; setDate(2); } public void onLoadMore() { page=page++; uri="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=201703"+page+""; setDate(3); } }); xRecyclerView.setLoadingMoreEnabled(true); } private void setDate(final int num){ OkHttpUtils okHttpUtils=OkHttpUtils.getHttpUtils(); okHttpUtils.loadDataFromNet(uri,Bean.class, new OkHttpUtils.CallBackListener<Bean>() { public void onSuccess(Bean result) { if (num==1){ list = result.getResult().getRows(); adapter = new RecyclerAdapter(MainActivity.this, list); xRecyclerView.setAdapter(adapter); }else if (num==2){ list.clear(); List<Bean.ResultBean.RowsBean> rows = result.getResult().getRows(); list.addAll(rows); xRecyclerView.refreshComplete(); adapter.notifyDataSetChanged(); }else if (num==3){ List<Bean.ResultBean.RowsBean> rows = result.getResult().getRows(); list.addAll(rows); xRecyclerView.loadMoreComplete(); adapter.notifyDataSetChanged(); } } public void onFail() { } }); } }//RecyclerAdapter
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyHolder>{ private Context context; private List<Bean.ResultBean.RowsBean> list; public RecyclerAdapter(Context context, List<Bean.ResultBean.RowsBean> list) { this.context=context; this.list=list; } public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false); return new MyHolder(view); } public void onBindViewHolder(MyHolder holder, int position) { holder.title.setText(list.get(position).getInfo().getAddress()); holder.body.setText(list.get(position). getInfo().getBaidu_lng()); holder.shijian.setText(list.get(position).getInfo().getCity_id()); Glide.with(context).load(list.get(position).getInfo().getDefault_image()).into(holder.iamge); } public int getItemCount() { return list.size(); } class MyHolder extends RecyclerView.ViewHolder{ private TextView title; private TextView shijian; private TextView body; private ImageView iamge; public MyHolder(View itemView) { super(itemView); title = (TextView) itemView.findViewById(R.id.textView); shijian = (TextView) itemView.findViewById(R.id.textView2); body = (TextView) itemView.findViewById(R.id.textView3); iamge = (ImageView) itemView.findViewById(imageView); } } }