我有一些url字符串,一些以.png结尾,另一些以.svg结尾。 .png文件可以很好地加载到recyclerview中,但.svg文件却无处可见。我搜索了很多论坛,但到目前为止还没有任何工作。
我的recyclerviewadapter的onBindViewHolder中有以下代码,该代码确定URL是否为.svg。如果有人有任何想法,我将不胜感激。
String[] homeUrlSplit = mData.get(i).getHomeTeamBadge().split("\\.");
String homeImageType = homeUrlSplit[homeUrlSplit.length-1];
if(homeImageType.equals("svg")){
// do something here
}
else {
Glide
.with(mContext)
.load(mData.get(i).getHomeTeamBadge())
.into(myViewHolder.homeTeamBadge);
}
谢谢
编辑:我不太理解“可能重复”答案下注释中包含的解决方案。如果有人知道任何其他解决方案,或者能够以通俗易懂的方式解释该特定解决方案,我将不胜感激。
这是适配器设置:
RecyclerViewAdapterFixtures recyclerViewAdapterFixtures = new RecyclerViewAdapterFixtures(getContext(), matchesList, getActivity());
recyclerViewFixtures.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerViewFixtures.setAdapter(recyclerViewAdapterFixtures);
这是recylerviewadapter代码
public class RecyclerViewAdapterFixtures extends RecyclerView.Adapter<RecyclerViewAdapterFixtures.MyViewHolder> {
Context mContext;
List<Match> mData;
Activity mActivity;
public RecyclerViewAdapterFixtures(Context mContext, List<Match> mData, Activity mActivity) {
this.mContext = mContext;
this.mData = mData;
this.mActivity = mActivity;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v;
v = LayoutInflater.from(mContext).inflate(R.layout.item_fixture, viewGroup, false);
MyViewHolder vHolder = new MyViewHolder(v);
return vHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
myViewHolder.textViewHomeTeam.setText(mData.get(i).getHomeTeamName());
String[] homeUrlSplit = mData.get(i).getHomeTeamBadge().split("\\.");
String homeImageType = homeUrlSplit[homeUrlSplit.length-1];
if(homeImageType.equals("svg")){
SvgLoader.pluck()
.with(mActivity)
.setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)
.load(mData.get(i).getHomeTeamBadge(), myViewHolder.homeTeamBadge);
}
else {
Glide
.with(mContext)
.load(mData.get(i).getHomeTeamBadge())
.into(myViewHolder.homeTeamBadge);
}
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textViewVersus;
private EditText homeTeamScore;
private EditText awayTeamScore;
private TextView textViewHomeTeam;
private TextView textViewAwayTeam;
private ImageView homeTeamBadge;
private ImageView awayTeamBadge;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textViewVersus = (TextView) itemView.findViewById(R.id.textViewVersus);
homeTeamScore = (EditText) itemView.findViewById(R.id.homeTeamScore);
awayTeamScore = (EditText) itemView.findViewById(R.id.awayTeamScore);
textViewHomeTeam = (TextView) itemView.findViewById(R.id.textViewHomeTeam);
textViewAwayTeam = (TextView) itemView.findViewById(R.id.textViewAwayTeam);
homeTeamBadge = (ImageView) itemView.findViewById(R.id.homeTeamBadge);
awayTeamBadge = (ImageView) itemView.findViewById(R.id.awayTeamBadge);
}
}
}
答案 0 :(得分:0)
将此内容放置在ur build.gradle文件(应用级)中:
implementation 'com.github.ar-android:AndroidSvgLoader:1.0.1'
在build.gradle项目级别中,您应该添加maven {url'https://jitpack.io'},以便在大多数情况下看起来像这样
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
然后
String[] homeUrlSplit = mData.get(i).getHomeTeamBadge().split("\\.");
String homeImageType = homeUrlSplit[homeUrlSplit.length - 1];
if (homeImageType.equals("svg")) {
SvgLoader.pluck()
.with(mActivity) // ur activity
.setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)//use ur place holder
.load(mData.get(i).getHomeTeamBadge(), myViewHolder.homeTeamBadge);
} else {
Glide
.with(mContext)
.load(mData.get(i).getHomeTeamBadge())
.into(myViewHolder.homeTeamBadge);
}
如果有的话,从ur项目中删除Glide依赖实现,因为AndroidSVGLoader已经拥有它们