Android无需回收即可访问recyclerview项目

时间:2015-07-04 09:56:17

标签: android

我试图突出显示RecyclerView中的一些项目,我成功地以编程方式执行此操作但是当我尝试突出显示其中一些不是Recycled时,我得到空指针。有没有办法以编程方式scrooling或其他东西回收我突出显示的项目?

到目前为止我已尝试过:

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

1 个答案:

答案 0 :(得分:0)

我做错了的是,在我onBindViewHolder上我已经有类似这样的事情时,我太担心在recyclerview中设置项目了:

@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));
    }
} 

如果标记为高亮显示的项目不在屏幕上,则在流口水时突出显示。

当我试图突出显示一个项目时,我会检查viewholder的位置是否为null,如果不是,我可以进行更改,如果为null,我等待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();
相关问题