MainActivity类:
public class MainActivity extends AppCompatActivity {
private SwipyRefreshLayout srl;
private RecyclerView rv;
private Banner banner;
private List<String> images = new ArrayList<>();
private List<NewsBean> news = new ArrayList<>();
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String json = (String) msg.obj;
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray top_stories = jsonObject.getJSONArray("top_stories");
for (int i = 0; i < top_stories.length() ; i++) {
JSONObject top = top_stories.getJSONObject(i);
String image = top.getString("image");
images.add(image);
}
banner.setIndicatorGravity(Banner.CENTER);
banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);
banner.isAutoPlay(true);
banner.setDelayTime(2000);
banner.setImages(images);
JSONArray stories = jsonObject.getJSONArray("stories");
String image = null;
for (int i = 0; i < stories.length(); i++) {
JSONObject stories1 = stories.getJSONObject(i);
String title = stories1.getString("title");
JSONArray images = stories1.getJSONArray("images");
for (int j = 0; j < images.length() ; j++) {
image = images.getString(j);
Log.i("image",image);
}
NewsBean newsBean = new NewsBean(title,image);
news.add(newsBean);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
rv.setLayoutManager(linearLayoutManager);
MyAdapter adapter = new MyAdapter(MainActivity.this,news);
rv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
break;
case 1:
String json1 = (String) msg.obj;
try {
JSONObject jsonObject = new JSONObject(json1);
JSONArray stories = jsonObject.getJSONArray("stories");
String image = null;
for (int i = 0; i < stories.length(); i++) {
JSONObject stories1 = stories.getJSONObject(i);
String title = stories1.getString("title");
JSONArray images = stories1.getJSONArray("images");
for (int j = 0; j < images.length() ; j++) {
image = images.getString(j);
Log.i("image",image);
}
NewsBean newsBean = new NewsBean(title,image);
news.add(newsBean);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
rv.setLayoutManager(linearLayoutManager);
MyAdapter adapter = new MyAdapter(MainActivity.this,news);
rv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
srl = (SwipyRefreshLayout) findViewById(R.id.srl);
rv = (RecyclerView) findViewById(R.id.rv);
banner = (Banner) findViewById(R.id.banner);
dataGet();
srl.setDirection(SwipyRefreshLayoutDirection.BOTH);
srl.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
srl.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh(int index) {
dataGet();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "下拉刷新了", Toast.LENGTH_SHORT).show();
srl.setRefreshing(false);
}
},2000);
}
@Override
public void onLoad(int index) {
doGet();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "上拉加载了", Toast.LENGTH_SHORT).show();
srl.setRefreshing(false);
}
},2000);
}
});
}
public void dataGet(){
String url = "http://news-at.zhihu.com/api/4/news/latest";
OkHttp3Utils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = handler.obtainMessage(0,json);
message.sendToTarget();
}
});
}
public void doGet(){
String url = "http://news-at.zhihu.com/api/4/news/before/20131119";
OkHttp3Utils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = handler.obtainMessage(1,json);
message.sendToTarget();
}
});
}
}
Adapter类:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context context;
private List<NewsBean> list;
public MyAdapter(Context context, List<NewsBean> list) {
this.context = context;
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//获取视图
View view = LayoutInflater.from(context).inflate(R.layout.rv_item,null);
//得到WindowManager
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//得到屏幕的宽
int width = windowManager.getDefaultDisplay().getWidth();
RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.width = width;
view.setLayoutParams(params);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.title.setText(list.get(position).getTitle());
Picasso.with(context).load(list.get(position).getImage()).placeholder(R.mipmap.ic_launcher).into(holder.image);
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image_rv);
title = itemView.findViewById(R.id.title_rv);
}
}
}