封装OkHttpClient网络请求
public class Tools {
public Tools(){}
public Tools get(String path){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.get()
.url(path)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
int code = response.code();
if(code==200){
ResponseBody body = response.body();
String string = body.string();
data.getData(string);
}
}
});
return this;
}
private HttpData data;
public void setData(HttpData data){
this.data = data;
}
public interface HttpData{
void getData(String s);
}
}
多条目适配器
public class RecycleViewAdapter extends RecyclerView.Adapter {
private Context mContext;
private ArrayList<RecycleUser.DataBean> list;
public RecycleViewAdapter(Context context, ArrayList<RecycleUser.DataBean> list) {
mContext = context;
this.list = list;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
View view;
RecyclerView.ViewHolder holder = null;
switch (position){
case 1:
view = LayoutInflater.from(mContext).inflate(R.layout.item, viewGroup, false);
holder = new MyViewHolder(view);
break;
case 2:
view = LayoutInflater.from(mContext).inflate(R.layout.item1, viewGroup, false);
holder = new MyViewHolder2(view);
break;
}
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
RecycleUser.DataBean bean = list.get(position);
switch (getItemViewType(position)){
case 1:
Picasso.with(mContext).load(bean.getThumbnail_pic_s()).into(((MyViewHolder) viewHolder).image);
((MyViewHolder) viewHolder).text.setText(bean.getAuthor_name());
break;
case 2:
Picasso.with(mContext).load(bean.getThumbnail_pic_s03()).into(((MyViewHolder2) viewHolder).image1);
Picasso.with(mContext).load(bean.getThumbnail_pic_s02()).into(((MyViewHolder2) viewHolder).image2);
((MyViewHolder2) viewHolder).text1.setText(bean.getAuthor_name());
break;
}
}
@Override
public int getItemViewType(int position) {
if(position%2!=0){
return 1;
}else{
return 2;
}
}
@Override
public int getItemCount() {
return list.size(); //或list==null?0:list.size()
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView text;
ImageView image;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text1);
image = itemView.findViewById(R.id.image1);
}
}
class MyViewHolder2 extends RecyclerView.ViewHolder{
TextView text1;
ImageView image1;
ImageView image2;
public MyViewHolder2(@NonNull View itemView) {
super(itemView);
text1 = itemView.findViewById(R.id.text2);
image1 = itemView.findViewById(R.id.image2);
image2 = itemView.findViewById(R.id.image3);
}
}
}
public class MainActivity extends AppCompatActivity {
private ArrayList<RecycleUser.DataBean> list = new ArrayList<>();
private String path = "http://www.xieast.com/api/news/news.php?page=1";
private RecyclerView recycle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycle = findViewById(R.id.recycler);
Tools tools = new Tools();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recycle.setLayoutManager(linearLayoutManager);
tools.get(path).setData(new Tools.HttpData() {
@Override
public void getData(String s) {
Gson gson = new Gson();
RecycleUser user = gson.fromJson(s, RecycleUser.class);
list.addAll(user.getData());
runOnUiThread(new Runnable() {
@Override
public void run() {
RecycleViewAdapter adapter = new RecycleViewAdapter(getApplicationContext(),list);
recycle.setAdapter(adapter);
}
});
}
});
}
}