4

My question id directly related to @Richard's one about the method onBindViewHolder() within the RecyclerView.Adapter is not called as he (and I) expected it to do.

I noticed that this method is called correctly until the end of my dataset (9 CardViews, scrolling down), but when I try to get back (scrolling up) it's not called anymore. The real problem is that in there I make my changes in the dataset and call notifyDataSetChanged(), but with this strange (to me) behavior my modifications don't take place when they are supposed to do.

The picture I attach wants to try to clarify:

  • I reach the bottom of the Rec.View (cardView - Supine: everything's fine);
  • dealing with the cards already showed completely or partially there is no problem (Supine, Gerund and Participle);
  • but when I reach the first cardView completely obscured, onBindViewHolder() is not called anymore and I can see from the debug that the dataset linked to the adapter is the "Supine" one, and here it is: the Supine cardView is showed.

Scrolling up from the bottom of the RecyclerView - Sorry for the quality

I thought that it was the exact same issue Richard faced in his question, and I tried his exact same solution: I forced setHasStableIds() to true in my Adapter's constructor,

public CardAdapter(List<Object> items){
    this.items = items;
    adapterList = new ArrayList<String>();
    formAdapt = new ConjFormAdapter(adapterList);
    itemMap = new HashMap<Object, Long>();
    setHasStableIds(true);
}

where itemMap is the Map I implement in my activity to set the unique ids of my dataset,

and overrode getItemId() this way:

public long getItemId(int position) {
    Object item = items.get(position);
    return itemMap.get(item);
}

But as you can see from the picture I still get this result: any idea, please?

Edit

The implementation of itemMap in my activity:

for(int i=0, j=0; i<conj_items.size(); i++, j++)
            conjAdapter.getItemMap().put(conj_items.get(i), (long) j);

where conj_items is the ArrayList I pass to the Adapter.

4

2 回答 2

7

当您setHasStableIds(true)必须getItemId(int position)使用return position.

您当前的一段代码setHasStableIds(true)仅告诉您的适配器,对于给定位置,项目不会更改,并且适配器无需再次为该位置调用 onBindViewHolder

于 2016-01-11T22:11:06.503 回答
0

我有同样的例外,但它实际上是由不同的问题引起的。在尝试setHasStableIds(true)and之后setLayoutTransition(null),每个都没有运气,我意识到我正在将视图添加到父onCreateViewHolder级(我不应该这样做,因为适配器会为你处理)。我删除了它,问题得到了解决。

@Override
public TopTrayIconAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    DMImageView icon = new DMImageView(mContext);
    LinearLayout.LayoutParams faceParams = new LinearLayout.LayoutParams(ICON_WIDTH, ICON_WIDTH);
    faceParams.gravity = Gravity.CENTER;
    faceParams.leftMargin = 15;
    faceParams.rightMargin = 15;

    parent.addView(icon, faceParams);  <- THIS WAS THE ISSUE
于 2017-06-19T15:41:27.150 回答