2015-07-04 64 views
0

我想突出RecyclerView中的一些项目,我成功地做到了这一点,但当我试图突出其中一些不是Recycled我得到空指针。有没有一种方法可以回收我以编程方式或者其他方式突出显示的项目?Android访问recyclerview项目不被回收

我试过到目前为止:

layoutManager.scrollToPositionWithOffset(position that I'm highlighting, 2); 

回答

0

我在做什么错,是我担心太多的时候就设置在recyclerview项目我onBindViewHolder我已经有这样的事情:

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    // - get element from your dataset at this position 
    // - replace the contents of the view with that element 
    holder.mTextView.setText(mDataset[position]); 
    if(selectedItem == position){ 
     holder.itemView.setBackgroundColor(Color.argb(40, 100, 100, 230)); 
    } 
    else { 
     holder.itemView.setBackgroundColor(Color.argb(0, 0, 0, 0)); 
    } 
} 

如果标记为突出显示的项目不在屏幕上以在sc highlight上突出显示。

当我试图突出显示一个项目时,我检查视图的位置是否为空,如果不是,我可以进行更改,如果为空,我等待recyclerView突出显示它。

mAdapter.setSelectedItem(myDataList.indexOf(name)); 
    if(musicList.findViewHolderForPosition(myDataList.indexOf(name)) != null){ 
     musicList.findViewHolderForPosition(myDataList.indexOf(name)).itemView.setBackgroundColor(Color.argb(40, 100, 100, 230)); 
    } 
    mAdapter.notifyDataSetChanged();