Skip to content
何も考えずにGlideを使う(Android)
androidios-tips
2019-06-30

取り急ぎ細部は考えずにRESTからのURLで画像を表示する。

public class ListRecycleViewAdapter extends RecyclerView.Adapter<ListViewHolder> {

    private List<InfoRowData> list;
    private Context context;


    public ListRecycleViewAdapter(Context context, List<InfoRowData> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ListViewHolder holder, final int position) {
        //  非同期取得
        if (list.get(position).getLogoUri() != null) {
            String url = list.get(position).getLogoUri();
            Glide.with(context)
                    .load(url)
                    .placeholder(R.drawable.ic_default_icon)
                    .into(holder.LogoIcon);
        } else {
            Glide.with(context)
                    .load(R.drawable.ic_default_icon)
                    .placeholder(R.drawable.ic_default_icon)
                    .into(holder.LogoIcon);
        }
    }
}
public class DetailsFragment extends Fragment {

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

        View view = inflater.inflate(R.layout.fragment_details, container, false);

        ImageView iconView = view.findViewById(R.id.logoIcon);
        //  非同期取得
        if (data.getLogoUrl() != null) {
            String url = data.getLogoUrl();
            Glide.with(getContext())
                    .load(url)
                    .placeholder(R.drawable.ic_default_icon)
                    .into(iconView);
        } else {
            Glide.with(getContext())
                    .load(R.drawable.ic_default_icon)
                    .placeholder(R.drawable.ic_default_icon)
                    .into(iconView);
        }

    }
}

案件固有名詞をわざわざ除去するのは面倒だな。そういうツールも作るべきかね。

https://github.com/bumptech/glide/wiki

https://bumptech.github.io/glide/doc/getting-started.html