我正在创建一个应用程序,它将使用具有交错布局的RecyclerView显示图片。我想从tumblr博客中提取图像,并在recyclerView中实现它们。我已经注册了我的应用并收到了OAuth Consumer Key和Secret Key。现在,我是新手,我不知道如何将这个代码实现到我设置的recyclerView中。这就是我想要完成的。只需在应用程序上显示图像即可。无需登录。功能不多,只需显示图片。
当我选择OAuth身份验证选项
时,我得到了这个// Authenticate via OAuth
JumblrClient client = new JumblrClient("key");
client.setToken("key");
// Make the request
Map<String, Object> params = new HashMap<String, Object>();
params.put("type", "photo");
List<Post> posts = client.blogPosts("url", params);
当我选择API密钥身份验证选项
时,我得到了这个// Authenticate via API Key
JumblrClient client = new JumblrClient("key");
// Make the request
Map<String, Object> params = new HashMap<String, Object>();
params.put("type", "photo");
List<Post> posts = client.blogPosts("url", params);
有一个JSON格式的示例输出,它不提供URL,因此我无法在应用程序中解析JSON。我只剩下这个java代码,而我正在使用的教程是使用JSON HERE
答案 0 :(得分:0)
首先,您需要为recycler视图创建一个适配器。
请注意,我在代码中使用picasso库来获取图像。因此,请务必将其包含在您的gradle文件中。
Recycler View Adapter的示例代码
package com.sample.demo.adaptor;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ImagesRecyclerViewAdaptor extends RecyclerView.Adapter<ImagesRecyclerViewAdaptor.ImagesViewHolder> {
private List<Post> posts;
public ImagesRecyclerViewAdaptor(List<Post> _posts) {
this.posts = _posts;
}
@Override
public int getItemCount() {
return posts.size();
}
@Override
public void onBindViewHolder(ImagesViewHolder imageViewHolder, int i) {
Post ci = posts.get(i);
if (ci != null) {
// EXTRACT THE URL FROM POST NOT SURE WHAT FUNCTION TO USE
Uri uri = Uri.parse();
Context context = imageViewHolder.vPostImage.getContext();
Picasso.with(context).load(uri).into(imageViewHolder.vPostImage);
}
}
@Override
public ImagesViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.image_cardview, viewGroup, false);
ImagesViewHolder viewHolder = new ImagesViewHolder(itemView);
return viewHolder;
}
public static class ImagesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected ImageView vPostImage;
public ImagesViewHolder(View v) {
super(v);
v.setOnClickListener(this);
vPostImage = (ImageView) v.findViewById(R.id.card_hotel_image);
}
@Override
public void onClick(View v) {
}
}
}
从您的主要活动或片段调用
// Authenticate via API Key
JumblrClient client = new JumblrClient("key");
// Make the request
Map<String, Object> params = new HashMap<String, Object>();
params.put("type", "photo");
List<Post> posts = client.blogPosts("url", params);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
RecyclerView imageList = (RecyclerView) rootView.findViewById(R.id.imageList);
imageList.setHasFixedSize(true);
imageList.setLayoutManager(layoutManager);
ImagesRecyclerViewAdaptor imageAdaptor = new ImagesRecyclerViewAdaptor(posts);
imageList.setAdapter(imageAdaptor);