admin
2021-07-28 c0269fcfa876b9c5cf309b2006462b4d09c5ef95
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.hanju.video.app.ui.sohuutils;
 
import java.util.ArrayList;
import java.util.List;
 
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.hanju.video.app.R;
 
public class OptionAdapter extends BaseAdapter {
    private Context mContext;
    private boolean isMulti;
 
    private List<OptionItem> mItems;
 
    public OptionAdapter(Context context, boolean multi) {
        this.mContext = context;
        this.isMulti = multi;
    }
 
    public void setItems(List<OptionItem> items) {
        add(items, false);
    }
 
    public void appendItems(List<OptionItem> items) {
        add(items, true);
    }
 
    public void addItem(OptionItem item) {
        List<OptionItem> items = new ArrayList<OptionAdapter.OptionItem>();
        items.add(item);
        add(items, true);
    }
 
    private void add(List<OptionItem> items, boolean append) {
        if (mItems == null) {
            mItems = new ArrayList<OptionAdapter.OptionItem>();
        } else {
            if (!append) {
                mItems.clear();
            }
        }
        if (items != null) {
            mItems.addAll(items);
        }
        notifyDataSetChanged();
    }
 
    public Integer[] getSelecteds() {
        ArrayList<Integer> selecteds = new ArrayList<Integer>();
        for (int i = 0; i < mItems.size(); i++) {
            if (mItems.get(i).seleted) {
                selecteds.add(i);
            }
        }
        return selecteds.toArray(new Integer[0]);
    }
 
    public boolean isSelected(int index) {
        if (index < 0 || index >= getCount()) {
            return false;
        }
        OptionItem item = (OptionItem) getItem(index);
        return item.seleted;
    }
 
    public void update(int index) {
        if (index < 0 || index >= getCount()) {
            return;
        }
        OptionItem item = (OptionItem) getItem(index);
        if (isMulti) {
            item.seleted = !item.seleted;
        } else {
            clearSelected();
            item.seleted = true;
        }
        notifyDataSetChanged();
    }
 
    private void clearSelected() {
        for (OptionItem item : mItems) {
            item.seleted = false;
        }
    }
 
    @Override
    public int getCount() {
        return mItems == null ? 0 : mItems.size();
    }
 
    @Override
    public Object getItem(int position) {
        return mItems == null ? null : mItems.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return 0;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
 
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.option_item, null);
            holder = new ViewHolder();
            holder.mOption = convertView.findViewById(R.id.option);
            holder.mSelected = convertView
                    .findViewById(R.id.selected);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
 
        OptionItem item = mItems.get(position);
 
        if (item == null) {
            return null;
        }
 
        holder.mOption.setText(item.option);
        int imgId = -1;
        if (isMulti) {
            imgId = item.seleted ? R.drawable.checkbox_selected
                    : R.drawable.checkbox_normal;
        } else {
            imgId = item.seleted ? R.drawable.radio_btn_selected
                    : R.drawable.radio_btn_normal;
        }
        holder.mSelected.setImageResource(imgId);
        return convertView;
    }
 
    private class ViewHolder {
        TextView mOption;
        ImageView mSelected;
    }
 
    public static class OptionItem {
        String option;
        boolean seleted;
 
        public OptionItem(String option, boolean selected) {
            this.option = option;
            this.seleted = selected;
        }
 
        public void setSelected(boolean selected) {
            this.seleted = selected;
        }
    }
}