aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/notifications/adapters/NoteBlockAdapter.java
blob: 0d19c796b6f3d16d2d1cd299956bfb2c3d7ea40d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.wordpress.android.ui.notifications.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import org.wordpress.android.R;
import org.wordpress.android.ui.notifications.blocks.NoteBlock;

import java.util.List;

public class NoteBlockAdapter extends ArrayAdapter<NoteBlock> {

    private final LayoutInflater mLayoutInflater;
    private final int mBackgroundColor;

    private List<NoteBlock> mNoteBlockList;

    public NoteBlockAdapter(Context context, List<NoteBlock> noteBlocks, int backgroundColor) {
        super(context, 0, noteBlocks);

        mNoteBlockList = noteBlocks;
        mLayoutInflater = LayoutInflater.from(context);
        mBackgroundColor = backgroundColor;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public int getCount() {
        return mNoteBlockList == null ? 0 : mNoteBlockList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NoteBlock noteBlock = mNoteBlockList.get(position);

        // Check the tag for this recycled view, if it matches we can reuse it
        if (convertView == null || noteBlock.getBlockType() != convertView.getTag(R.id.note_block_tag_id)) {
            convertView = mLayoutInflater.inflate(noteBlock.getLayoutResourceId(), parent, false);
            convertView.setTag(noteBlock.getViewHolder(convertView));
        }

        // Update the block type for this view
        convertView.setTag(R.id.note_block_tag_id, noteBlock.getBlockType());

        noteBlock.setBackgroundColor(mBackgroundColor);

        return noteBlock.configureView(convertView);
    }

    public void setNoteList(List<NoteBlock> noteList) {
        if (noteList == null) return;

        mNoteBlockList = noteList;
        notifyDataSetChanged();
    }
}