2016-12-06 26 views
1

我正在使用this庫來處理我的RecycleView以添加項目。我有一個ImageView和一個類的佈局。我有一個對象列表,我想將它們添加到RecycleViewRecycleviewer - 如何重用佈局?

所以基本上是這樣的:

item_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/frame" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"> 

    <ImageView 
     android:id="@+id/cover_image" 
     android:layout_width="180dp" 
     android:layout_height="264dp" 
     android:scaleType="centerCrop" /> 

</FrameLayout> 

activity_main.xml(不是全部):

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/swipe_refresh_main" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 

     android:scrollbars="vertical"/> 
</android.support.v4.widget.SwipeRefreshLayout> 

而且我的課MyObject

public class MyObject extends AbstractItem<MyObject, MyObject.ViewHolder> implements Serializable { 
    @SerializedName(value="id") 
    private int id; 
    @SerializedName(value="name") 
    private String name; 
    @Expose 
    private String linkToImage; 

    public int getId() { 
     return id; 
    } 
    public String getName() { 
     return name; 
    } 

    @Override 
    public int getType() { 
     return R.id.image; 
    } 

    //The layout to be used for this type of item 
    @Override 
    public int getLayoutRes() { 
     return R.layout.item_main; 
    } 

    @Override 
    public void bindView(ViewHolder viewHolder, List<Object> payloads) { 
     //call super so the selection is already handled for you 
     super.bindView(viewHolder, payloads); 

     //bind our data 
     //set the text for the name 
     Picasso.with(viewHolder.itemView.getContext()) 
       .load(linkToImage) 
       .into(viewHolder.image); 
    } 

    //The viewHolder used for this item. This viewHolder is always reused by the RecyclerView so scrolling is blazing fast 
    protected static class ViewHolder extends RecyclerView.ViewHolder { 
     protected ImageView image; 
     public ViewHolder(View view) { 
      super(view); 
      this.image = (ImageView) view.findViewById(R.id.cover_image); 
     } 
    } 
} 

這很好。現在的事情是,我想重複使用另一個Activity與不同的RecycleViewList<MyObject>item_main.xml相同。

我試圖使用與MainActivity中相同的代碼。它似乎添加項目(它在onClick中打印出名稱),但它不加載圖像。

這是我在MainActivity代碼:

RecyclerView mRecyclerView; 
mRecyclerView = (RecyclerView) findViewById(R.id.mainRecycleView); 
mRecyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2)); 

FastItemAdapter fastAdapter = new FastItemAdapter(); 

mRecyclerView.setAdapter(fastAdapter); 
fastAdapter.add(listOfMyObjects); 
DefaultItemAnimator animator = new DefaultItemAnimator() { 
    @Override 
    public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) { 
     return true; 
    } 
}; 
fastAdapter.withSelectable(true); 
fastAdapter.withOnClickListener(new FastAdapter.OnClickListener<MyObject>() { 
    @Override 
    public boolean onClick(View v, IAdapter<MyObject> adapter, MyObjectitem, int position) { 
     Log.d("DEBUG", item.getName()); 
     return true; 
    } 
}); 
mRecyclerView.setItemAnimator(animator); 

任何想法?謝謝。

+0

它是否在logcat中引發任何錯誤? –

+0

@ReazMurshed - 不,它正常啓動活動,加載數據(列表),甚至將其加載到RecycleView中,它只是不顯示圖像 – user3024750

+0

請參閱下面的答案。這裏有一個潛在的錯誤。 –

回答

1

我在您的ViewHolder中發現了一個潛在的錯誤,但我真的不知道它在MainActivity中的工作方式。

你已經設置item_main.xmlgetLayoutRes功能,在你ViewHolder你要是非佈局ID的ImageView參考。

所以你的ViewHolder應該看起來像這樣。

protected static class ViewHolder extends RecyclerView.ViewHolder { 
    protected ImageView image; 
    public ViewHolder(View view) { 
     super(view); 

     // Set the proper layout id cover_image here 
     this.image = (ImageView) view.findViewById(R.id.cover_image); 
    } 
} 

更新

當您不慎把錯誤的佈局ID的問題,所以我認爲這個錯誤是不存在的。但無論如何,我在這裏發現了其他問題,您可能需要檢查。

在您的MainActivity中,您將首先設置適配器,然後您將在適配器中添加對象。所以,你可以這樣做

// Add the items first 
fastAdapter.add(listOfMyObjects); 

// Then set the adapter 
mRecyclerView.setAdapter(fastAdapter); 

或者你可以在你的方式

mRecyclerView.setAdapter(fastAdapter); 
fastAdapter.add(listOfMyObjects); 

// Notify the adapter that new elements have added 
fastAdapter.notifyDatasetChanged(); 

做到這一點,請檢查圖像鏈接都有效。

就我個人而言,我使用Glide來加載圖像。你也可以看看Glide的圖片加載。

Glide.with(viewHolder.itemView.getContext()) 
    .load(linkToImage) 
    .into(viewHolder.image); 
+0

我的不好,我編輯了一些名稱/ ids,使其更簡單,似乎忘記將圖像更改爲R.id.cover_image。在我的代碼中,它一直像你擁有它。非常感謝! – user3024750

+0

好的。據此編輯問題。 –

+0

請參閱最新的答案。 –