我想使用 Tumblr API 从 Tumblr 博客中获取一些信息,现在我想获取Name( blog_name )和下面Json
的图片封面( Header_image )
我下面的代码工作正常,但是当我在TextView
和ImageView
中设置 Name 和 ImageCover 时,发生了一些奇怪的事情,我发现了first在recyclview
的第3个项目中发布博客,或在“图像封面”中添加第4个博客,甚至在
我将滚动滚动到recyclview
的顶部和底部,名称和图像会随机更改,我不知道为什么。
这是 myAdapterTumblr.java ,以及 对不起,如果我不能解释清楚
public class myAdapterTumblr extends RecyclerView.Adapter<myAdapterTumblr.ViewHolder> {
RequestQueue requestQueue;
TextView name;
private List<PostTumblr> postTumblrs;
private Context context;
private RecyclerViewClickListenerTumblr mClickListener;
private String APIKey = "";
public myAdapterTumblr(List<PostTumblr> postTumblrs, Context context, RecyclerViewClickListenerTumblr clickListener) {
this.postTumblrs = postTumblrs;
this.context = context;
this.mClickListener = clickListener;
}
public List<PostTumblr> getItems() {
return postTumblrs;
}
@Override
public myAdapterTumblr.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tumblr_profil,
parent, false);
return new myAdapterTumblr.ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindModel(postTumblrs.get(position));
}
@Override
public int getItemCount() {
return postTumblrs.size();
}
public void getJsonDataCover(PostTumblr mPost, final RequestOptions options, final ProgressBar progressBarCover, final ImageView imgcover) {
String jsonStr = "https://api.tumblr.com/v2/blog/" + mPost.getTumblrname() + ".tumblr.com/posts/photo?api_key=" + APIKey;
final StringRequest stringRequest = new StringRequest(Request.Method.GET, jsonStr, new Response.Listener<String>() {
@SuppressLint("CheckResult")
@Override
public void onResponse(String s) {
try {
String cover = null;
JSONObject obj = new JSONObject(s);
JSONObject response = obj.getJSONObject("response");
JSONArray posts = response.getJSONArray("posts");
for (int i = 0; i < posts.length(); ++i) {
JSONObject post = posts.getJSONObject(i);
JSONArray trail = post.getJSONArray("trail");
for (int j = 0; j < trail.length(); ++j) {
JSONObject trailobj = trail.getJSONObject(j);
JSONObject blog = trailobj.getJSONObject("blog");
JSONObject theme = blog.getJSONObject("theme");
cover = theme.getString("header_image");
Glide.with(context).asBitmap()
.load(cover)
.apply(options)
.listener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
progressBarCover.setVisibility(View.GONE);
return false;
}
}).into(imgcover);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
requestQueue.add(stringRequest);
}
private void getJsonDataText(PostTumblr mPost, final TextView nametv) {
String jsonStr = "https://api.tumblr.com/v2/blog/" + mPost.getTumblrname() + ".tumblr.com/posts/photo?api_key=" + APIKey;
final StringRequest stringRequest = new StringRequest(Request.Method.GET, jsonStr, new Response.Listener<String>() {
@SuppressLint("CheckResult")
@Override
public void onResponse(String s) {
try {
JSONObject obj = new JSONObject(s);
JSONObject response = obj.getJSONObject("response");
JSONArray posts = response.getJSONArray("posts");
String name = null;
for (int i = 0; i < posts.length(); ++i) {
JSONObject post = posts.getJSONObject(i);
name = post.getString("blog_name");
}
nametv.setText(name);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
requestQueue.add(stringRequest);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
PostTumblr mPost;
ImageView image, imgcover;
ProgressBar progressBarProfile, progressBarCover;
TextView facebooktv;
TextView instagramtv;
TextView siettv;
ViewHolder(View v) {
super(v);
image = itemView.findViewById(R.id.profilepic);
imgcover = itemView.findViewById(R.id.coverpic);
progressBarProfile = itemView.findViewById(R.id.progressprofile);
progressBarCover = itemView.findViewById(R.id.progresscover);
requestQueue = Volley.newRequestQueue(context);
name = itemView.findViewById(R.id.name);
facebooktv = itemView.findViewById(R.id.fb);
instagramtv = itemView.findViewById(R.id.instagram);
siettv = itemView.findViewById(R.id.tumblr);
progressBarProfile.getIndeterminateDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
progressBarCover.getIndeterminateDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
v.setOnClickListener(this);
}
@SuppressLint("CheckResult")
void bindModel(final PostTumblr postTumblr) {
/*profil*/
this.mPost = postTumblr;
final RequestOptions options = new RequestOptions();
options.diskCacheStrategy(DiskCacheStrategy.ALL)
.priority(Priority.HIGH)
.dontAnimate()
.dontTransform();
Glide.with(context).asBitmap()
.load("https://api.tumblr.com/v2/blog/" + postTumblr.getTumblrname() + ".tumblr.com/avatar/512")
.apply(options)
.listener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
progressBarProfile.setVisibility(View.GONE);
return false;
}
}).into(image);
getJsonDataText(mPost, name);
getJsonDataCover(mPost, options, progressBarCover, imgcover);
facebooktv.setText(mPost.getTumblrname());
instagramtv.setText(mPost.getTumblrname());
siettv.setText(mPost.getTumblrname() + ".tumblr.com");
}
@Override
public void onClick(View view) {
mClickListener.onClick(view, mPost);
}
}
}
答案 0 :(得分:0)
您正在错置数据获取的代码实现。
RecyclerView
(随其名称一起使用)可重用其创建的视图,因此,在一段时间后异步获取数据时,视图可能不属于已获取的数据。
要解决此问题,您应该将数据获取代码移到myAdapterTumblr
代码之外,一旦获取了所有数据,就将数据设置到适配器中并调用适配器的notifyDataSetChanged()
方法。< / p>