当notifyItemChanged()调用时,将调用RecyclerView.Adapter onCreateViewHolder

时间:2017-05-19 13:19:14

标签: android android-recyclerview

我有简单的适配器,看起来完全像这样: Adapter

除了数据项之外,它不仅仅是一个字符串。

public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.TopicContentViewHolder> {

    private final Context mContext;
    private List<ModelTopic> mData;

    public void add(ModelTopic s,int position) {
        position = position == -1 ? getItemCount()  : position;
        mData.add(position, s);
        notifyItemInserted(position);
    }

    public void updateNightMode(boolean isNightMode) {
        if (mData.size() > 0) {
            mData.get(0).setNightMode(isNightMode);
            notifyItemChanged(0);
        }
    }

    static class TopicContentViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.view_pager)
        FixedAspectRatioViewPager viewPager;
        @BindView(R.id.circlePageIndicator)
        CirclePageIndicator circlePageIndicator;

        @BindView(R.id.wv_details_text)
        WebView wvDetailsText;

    }

    @Override
    public void onBindViewHolder(TopicContentViewHolder vh, final int position) {
        Log.d("SimpleAdapter", "BindViewHolder");
        ModelTopic topic =  mData.get(position);

        final int curTextColor = NightModeFragment.getCurrentTextColor(mContext, topic.isNightMode());
        vh.detailsHeaderText.setTextColor(curTextColor);

正如你所看到的,我有一个WebView内部元素,它可能比屏幕大小大,所以单个元素不适合屏幕内部。

我想更改 TopicContentViewHolder 中某些textView的颜色 所以我根据我的数据设置颜色。然后,当我想改变颜色时,我会改变数据并调用

notifyItemChanged(position)

但我有意想不到的滚动。 我认为这是因为当我调用 notifyItemChanged

时调用我的方法 onCreateViewHolder

这是我的日志输出:

05-19 17:15:38.620 27303-27303 D/SimpleAdapter: createViewHolder
05-19 17:15:38.640 27303-27303 D/SimpleAdapter: BindViewHolder
//all above called when I enter fragment and adapter is created

05-19 17:15:48.535 27303-27303 D/SimpleAdapter: createViewHolder
05-19 17:15:48.555 27303-27303 D/SimpleAdapter: BindViewHolder
//above called when I change notifyItemChanged() first time

05-19 17:15:50.447 27303-27303 D/SimpleAdapter: BindViewHolder
//above called when I change notifyItemChanged() 2 time

05-19 17:15:52.910 27303-27303 D/SimpleAdapter: BindViewHolder
//above called when I change notifyItemChanged() 3 time

05-19 17:15:53.183 27303-27303 D/SimpleAdapter: BindViewHolder
//above called when I change notifyItemChanged() 4 time

如您所见,onCreateViewHolder调用了两次。第一次是显而易见的,但第二次对我来说是一个谜。 这里有什么帮助吗?

0 个答案:

没有答案