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

如何将本地图像(可绘制图像文件)显示到cardviewvia json. 我想离线加载图像而不是使用毕加索库


我正在使用选项卡布局和我的 Frament.java


@Nullable

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(

            R.layout.fragment, container, false);

    return rootView;



}


@Override

public 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);

}


慕哥6287543
浏览 137回答 1
1回答

喵喵时光机

如果你有绘制文件夹下的本地图片为所有一)myntra B)firstCry C)跳房子等等,那么你只是单纯的需要得到基于URL正确绘制。您可以创建工厂类,它将为您提供@DrawableRes&nbsp; &nbsp; public static @DrawableRes int getDrawable(String url){&nbsp; &nbsp; &nbsp; &nbsp; if(url.contains("Myntra")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return R.drawable.myntra;&nbsp; &nbsp; &nbsp; &nbsp; } else if(url.contains("Snapdeal")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return R.drawable.snapdeal;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Add other if else to support other website url&nbsp; &nbsp; }这是修改后的 onBindViewHolder 方法。@Overridepublic void onBindViewHolder(WebsiteAdapter.ViewHolder holder, int position) {&nbsp; &nbsp;Website website = mDataSource.get(position);&nbsp; &nbsp;holder.url.setText(website.getUrl());&nbsp; &nbsp;holder.thumbnailImageView.setImageResource(getDrawable(website.getUrl()));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java