Android&在recycleview中使用xutils加载网络图片

Android&在recycleview中使用xutils加载网络图片,第1张

概述在recycleview中使用xutils加载网络图片,关于xutils的使用可以在这篇博客去学习,先看看效果图在这里直接将java代码讲解,在这里有个刷新的功能,需要添加依赖implementation'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2'//1.0.5及以前版本的老用户升级需谨慎,API

在recyclevIEw中使用xutils加载网络图片,关于xutils
的使用可以在这篇博客去学习,先看看效果图

在这里直接将java代码讲解,在这里有个刷新的功能,需要添加依赖

 implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2'  //1.0.5及以前版本的老用户升级需谨慎,API改动过大    implementation 'com.scwang.smartrefresh:SmartRefreshheader:1.1.2'  //没有使用特殊header,可以不加这行

在activity布局中

    <com.scwang.smartrefresh.layout.SmartRefreshLayout        androID:ID="@+ID/refreshLayout"        androID:layout_wIDth="match_parent"        androID:layout_height="0dp"        app:layout_constrainttop_toBottomOf="@+ID/search"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintVertical_bias="0.0"        tools:layout_editor_absoluteX="0dp"        >        <com.scwang.smartrefresh.layout.header.Classicsheader            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content" />        <androIDx.recyclervIEw.Widget.RecyclerVIEw            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:ID="@+ID/recyclevIEw" />    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

如果不需要则将带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;    List<Message>List;    @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<String>() {            @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 one rror(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 onl oadMoreListener() {            @OverrIDe            public voID onl oadMore(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<MyVIEwHolder>{//用来配合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();        }    } }
总结

以上是内存溢出为你收集整理的Android&在recycleview中使用xutils加载网络图片全部内容,希望文章能够帮你解决Android&在recycleview中使用xutils加载网络图片所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1062826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存