在recycleview中使用xutils加载网络图片,关于xutils
的使用可以在这篇博客去学习,先看看效果图
如果不需要则将带Refreshlayout代码删去
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
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.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import org.xutils.common.Callback;
import org.xutils.common.util.DensityUtil;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;
@ContentView(R.layout.activity_main4_1)
public class Main4Activity_1 extends AppCompatActivity {
RecyclerView recyclerView;
MyAdapter myAdapter;
Message message;
@ViewInject(R.id.textView33)
TextView textView3_1;
@ViewInject(R.id.textView333)
TextView textView3_2;
@ViewInject(R.id.textView3333)
TextView textView3_3;
Listlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4_1);
x.Ext.init(getApplication());
x.Ext.setDebug(BuildConfig.DEBUG);// 是否输出debug日志, 开启debug会影响性能.
x.view().inject(Main4Activity_1.this);//没有用到view注解可以先不用
//网络连接
RequestParams params = new RequestParams("http://148.70.46.9/chatlist");
x.http().get(params, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {//主线程
//解析json,获得数据
Gson gson=new Gson();
message=gson.fromJson(result,Message.class);
Log.i("Main", "onSuccess: "+message.chatlist.get(0).name);
//加载图片
CircleImageView headimg3 =findViewById(R.id.headimg);
ImageOptions imageOptions;
imageOptions = new ImageOptions.Builder()
.setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))
.setRadius(DensityUtil.dip2px(5))
// 如果ImageView的大小不是定义为wrap_content, 不要crop.
.setCrop(true) // 很多时候设置了合适的scaleType也不需要它.
// 加载中或错误图片的ScaleType
//.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
.setLoadingDrawableId(R.mipmap.ic_launcher)
.setFailureDrawableId(R.mipmap.ic_launcher)
.build();
x.image().bind(headimg3, message.headimg, imageOptions);//一般使用到这就可以
//进行recycleview的设置与实现
myAdapter = new MyAdapter();
recyclerView =findViewById(R.id.recycleview);
recyclerView.setAdapter(new MyAdapter());
recyclerView.setLayoutManager(new LinearLayoutManager(Main4Activity_1.this));
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.i("Main", "onError: "+ex.getMessage());
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
recyclerView =findViewById(R.id.recycleview);
RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
myAdapter.notifyDataSetChanged();
refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
}
});
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
myAdapter.notifyDataSetChanged();
refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
}
});
}
public class MyViewHolder extends RecyclerView.ViewHolder{//用来包装每个子项的布局信息
public TextView textView1;
public TextView textView2;
public TextView textView3;
public CircleImageView headimag;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.textView33);
textView2 = itemView.findViewById(R.id.textView333);
textView3 = itemView.findViewById(R.id.textView3333);
headimag=itemView.findViewById(R.id.headimg3);
}
}
public class MyAdapter extends RecyclerView.Adapter{
//用来配合recycleview进行加载子项以及填充子项的数据
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(Main4Activity_1.this).inflate(R.layout.messages,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {//在这里进行子项的文本图片数据填充
holder.textView1.setText(message.chatlist.get(position).name);
holder.textView2.setText(message.chatlist.get(position).content);
holder.textView3.setText(message.chatlist.get(position).time);
ImageOptions imageOptions;
imageOptions = new ImageOptions.Builder()
.setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))
.setRadius(DensityUtil.dip2px(5))
// 如果ImageView的大小不是定义为wrap_content, 不要crop.
.setCrop(true) // 很多时候设置了合适的scaleType也不需要它.
// 加载中或错误图片的ScaleType
//.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
.setLoadingDrawableId(R.mipmap.ic_launcher)
.setFailureDrawableId(R.mipmap.ic_launcher)
.build();
x.image().bind(holder.headimag, message.chatlist.get(position).head, imageOptions);//一般使用到这就可以
}
@Override
public int getItemCount() {//返回子项的数目,即加载多少列表项
return message.chatlist.size();
}
}
}