为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过 json 将本地图像(可绘制图像文件)显示到 cardview。

如何通过 json 将本地图像(可绘制图像文件)显示到 cardview。

慕哥6287543 2021-06-29 09:15:43
如何将本地图像(可绘制图像文件)显示到cardviewvia json. 我想离线加载图像而不是使用毕加索库我正在使用选项卡布局和我的 Frament.java@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    View rootView = inflater.inflate(            R.layout.fragment, container, false);    return rootView;}@Overridepublic void onViewCreated(View view, @Nullable Bundle savedInstanceState) {    super.onViewCreated(view, savedInstanceState);    final ArrayList<Website> babyList = Website.getbabyFromFile("baby&kids.json", getActivity());    adapter = new WebsiteAdapter(this, babyList);    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);    recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2));    recyclerView.setAdapter(adapter);}
查看完整描述

1 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

如果你有绘制文件夹下的本地图片为所有一)myntra B)firstCry C)跳房子等等,那么你只是单纯的需要得到基于URL正确绘制。您可以创建工厂类,它将为您提供@DrawableRes


    public static @DrawableRes int getDrawable(String url){

        if(url.contains("Myntra")) {

            return R.drawable.myntra;

        } else if(url.contains("Snapdeal")) {

            return R.drawable.snapdeal;

        }

        // Add other if else to support other website url

    }

这是修改后的 onBindViewHolder 方法。


@Override

public void onBindViewHolder(WebsiteAdapter.ViewHolder holder, int position) {

   Website website = mDataSource.get(position);

   holder.url.setText(website.getUrl());

   holder.thumbnailImageView.setImageResource(getDrawable(website.getUrl()));

}


查看完整回答
反对 回复 2021-07-07
  • 1 回答
  • 0 关注
  • 136 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信