2015-10-04 72 views
0

我正在製作一個應用程序,該應用程序將使用具有交錯佈局的RecyclerView顯示圖片。我想從tumblr博客中拉出圖片,並在recyclerView中實現它們。我已經註冊了我的應用程序並收到了OAuth使用者密鑰和密鑰。現在,我是新來的,我不知道如何實現這個代碼到我已經設置的recyclerView中。這就是我想要完成的。只需在應用程序上顯示圖像。不需要登錄。沒有太多的功能,只顯示圖片。僅顯示來自Android應用程序的Tumblr博客的圖像文章

我給這個時候我選擇了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

首先,您需要爲回收站視圖創建一個適配器。

請注意,在代碼中,我使用picasso庫來獲取圖像。所以一定要把它包含在你的gradle文件中。

的回收站查看適配器

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); 
示例代碼