package com.example.zh.week2.view.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.zh.week2.R;
import com.example.zh.week2.model.bean.ZhanShiBean;
import java.util.List;
public class ZhanShiAdapter extends RecyclerView.Adapter<ZhanShiAdapter.VVH> {
Context context;
List<ZhanShiBean.NewslistBean> beans;
LayoutInflater layoutInflater;
public ZhanShiAdapter(Context context, List<ZhanShiBean.NewslistBean> beans) {
this.layoutInflater = LayoutInflater.from(context);
this.context = context;
this.beans = beans;
}
@NonNull
@Override
public ZhanShiAdapter.VVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = layoutInflater.inflate(R.layout.reseceview, viewGroup, false);
VVH vvh = new VVH(view);
return vvh;
}
@Override
public void onBindViewHolder(@NonNull ZhanShiAdapter.VVH vvh, int i) {
vvh.text.setText(beans.get(i).getTitle());
Glide.with(context).load(beans.get(i).getPicUrl()).into(vvh.img);
}
@Override
public int getItemCount() {
return beans.size();
}
public class VVH extends RecyclerView.ViewHolder {
private final ImageView img;
private final TextView text;
public VVH(@NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.img);
text = itemView.findViewById(R.id.text);
}
}
}
public class ZSPresenter {
private final Okhttp getdanli;
ZhanShiActivity zhyanShi;
public ZSPresenter(ZhanShiActivity zhyanShi1) {
getdanli = Okhttp.getdanli();
this.zhyanShi = zhyanShi1;
}
public void getzcpresenter(String url){
getdanli.get(url, new Okhttp.getokhttp() {
@Override
public void getcheng(String s) {
zhyanShi.getview(s);
}
});
}
}
public class ZhanShiActivity extends AppCompatActivity implements IManintenter {
private RecyclerView recyclerView;
private String path = "http://api.tianapi.com/meinv/?key=11500f2566e70105ee1c28a12f5f750a";
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
String s = (String) msg.obj;
inten(s);
}
}
};
private void inten(String s) {
Gson gson = new Gson();
ZhanShiBean zhanShiBean = gson.fromJson(s, ZhanShiBean.class);
List<ZhanShiBean.NewslistBean> newslist = zhanShiBean.getNewslist();
LinearLayoutManager layoutManager = new LinearLayoutManager(ZhanShiActivity.this);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
ZhanShiAdapter zhanShiAdapter = new ZhanShiAdapter(ZhanShiActivity.this, newslist);
recyclerView.setAdapter(zhanShiAdapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhyan_shi);
recyclerView = findViewById(R.id.recyclerView);
ZSPresenter zsPresenter = new ZSPresenter(this);
zsPresenter.getzcpresenter(path);
}
@Override
public void getview(String s) {
Log.i("AA", "展示" + s);
Message message = new Message();
message.what = 1;
message.obj = s;
handler.sendMessage(message);
}
}