在activity中使用adapter加载数据

用AsyncTask加载图片

private GridAdapter3 gridAdapter;
        public ImageLoadTask(Context context,GridAdapter3 gridAdapter)
        {
            this.gridAdapter=gridAdapter;
        }

        @Override
        protected Void doInBackground(String... params) {

            for(int i=0;i<gridAdapter.getCount();i++)
            {
                ImageInfo imageInfo= (ImageInfo) gridAdapter.getItem(i);
                String imagePath = imageInfo.getImagePath();
                Bitmap bitmap = getImagefromNetWork(imagePath);
                imageInfo.setBitmap(bitmap);
                //吧下载好的信息传出去
                publishProgress();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            Log.e("Main","刷新");
            //上面只是把bitmap放到了实体类里面  此处刷新可在界面上显示 
            gridAdapter.notifyDataSetChanged();
        }

使用谷歌工具加载图片
compile ‘com.github.bumptech.glide:glide:3.7.0’
其中placeholder占位符 centerCrop居中裁剪 然后可以缓存 最后放到容器中

Glide.with(context).load(imageInfo.getImagePath()).
placeholder(R.mipmap.ic_launcher).centerCrop().into(holder.imageView);
在 Android ,RecyclerView 是一个非常常用的控件,用于展示大量的数据列表。在使用 RecyclerView 时,我们需要创建 ViewHolder 和 Adapter。 ViewHolder 是一个容器,用于存放 RecyclerView 的每个 item 的视图,我们可以在其找到 item 的各个控件。Adapter 则是将数据与 ViewHolder 绑定在一起,用于将数据显示在 RecyclerView 上。 在 RecentApp ,我们可以按照以下步骤使用 RecyclerView、ViewHolder 和 Adapter: 1. 在 XML 布局文件添加 RecyclerView 控件: ``` <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 创建 ViewHolder 类,用于存放 RecyclerView 的 item 视图: ``` public class RecentAppViewHolder extends RecyclerView.ViewHolder { public TextView appName; public ImageView appIcon; public RecentAppViewHolder(@NonNull View itemView) { super(itemView); appName = itemView.findViewById(R.id.app_name); appIcon = itemView.findViewById(R.id.app_icon); } } ``` 3. 创建 Adapter 类,用于将数据与 ViewHolder 绑定在一起: ``` public class RecentAppAdapter extends RecyclerView.Adapter<RecentAppViewHolder> { private List<AppInfo> appInfoList; public RecentAppAdapter(List<AppInfo> appInfoList) { this.appInfoList = appInfoList; } @NonNull @Override public RecentAppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_recent_app, parent, false); return new RecentAppViewHolder(view); } @Override public void onBindViewHolder(@NonNull RecentAppViewHolder holder, int position) { AppInfo appInfo = appInfoList.get(position); holder.appName.setText(appInfo.getName()); holder.appIcon.setImageDrawable(appInfo.getIcon()); } @Override public int getItemCount() { return appInfoList.size(); } } ``` 4. 在 Activity 或 Fragment 初始化 RecyclerView、ViewHolder 和 Adapter: ``` RecyclerView recyclerView = findViewById(R.id.recycler_view); RecentAppAdapter adapter = new RecentAppAdapter(appInfoList); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); ``` 这样,就可以在 RecentApp 使用 RecyclerView、ViewHolder 和 Adapter 来显示最近打开的应用程序了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值