簡體   English   中英

RecyclerView.Adapter中的ViewHolder並非特定於位置

[英]ViewHolder in RecyclerView.Adapter not specific to position

以下是我的onBindViewHolder代碼的onBindViewHolder (在MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>內部MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    StatusItem item = mDataset.get(position);
    //......
    //Add content and timing to the textview
    String content = item.getContent();

    holder.mTextViewTime.setText(timing);
    //Set the img
    holder.imgViewIcon.setImageDrawable(item.getProfileDrawable());
    //Set content image (for Instagram)
    holder.mImageViewContentPic.setImageDrawable(item.getContentDrawable());
    //HIDE THE VIEW Start
    if(item.getContentDrawable() == null){
        holder.mImageViewContentPic.setVisibility(View.GONE);
    }
    //HIDE THE VIEW End
}

HIDE THE VIEW部分未按預期工作。 當我向下滾動時,視圖正常工作。 然而,當我開始向上滾動,即重新審視之前的看法,即應該是ImageViews VISIBLE變得GONE ,雖然我檢查我的數據集,並證實它沒有被修改。 嘗試在視圖上調用其他方法也會產生不穩定的結果(數據集中的位置和項目不匹配)。

似乎視圖持有者沒有綁定到RecyclerView內的特定位置。

如果我刪除HIDE THE VIEW部分,代碼將按預期工作。 有沒有辦法解決這個問題,並在我的情況下動態隱藏視圖?

注意:我使用了一些AsyncTasks來更新數據集並調用notifyDataSetChanged() ,如果這是相關的。

###This is the solution to your problem:###

holder.mImageViewContentPic.setVisibility(View.VISIBLE);
if(item.getContentDrawable() == null){
        holder.mImageViewContentPic.setVisibility(View.GONE);
    }

由於RecyclerView非常好地使用了回收, ViewHolder A可能會被用作ViewHolder B,因此如果某些屬性附加到錯誤的對象,則需要特定ViewHolder每個屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM