IOS
hefeixia
2021-02-18 49f3c1374873f73dbde2983ca0fcf1fb10bfedbf
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.mylhyl.circledialog;
 
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.util.SparseArray;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;
 
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.StringRes;
 
import com.mylhyl.circledialog.internal.Controller;
 
/**
 * Created by hupei on 2020/5/22.
 */
public class CircleViewHolder {
 
    private final View dialogView;
    private final SparseArray<View> views;
 
    public CircleViewHolder(View dialogView) {
        this.dialogView = dialogView;
        this.views = new SparseArray<>();
    }
 
    public CircleViewHolder setBackgroundColor(@ColorInt int color) {
        dialogView.setBackgroundColor(color);
        return this;
    }
 
    public CircleViewHolder setBackgroundRes(@DrawableRes int backgroundRes) {
        dialogView.setBackgroundResource(backgroundRes);
        return this;
    }
 
    public CircleViewHolder setBackgroundDrawable(Drawable background) {
        if (Controller.SDK_JELLY_BEAN) {
            dialogView.setBackground(background);
        } else {
            dialogView.setBackgroundDrawable(background);
        }
        return this;
    }
 
    public View getDialogView() {
        return dialogView;
    }
 
    public CircleViewHolder setText(@IdRes int viewId, CharSequence value) {
        TextView view = findViewById(viewId);
        view.setText(value);
        return this;
    }
 
    public CircleViewHolder setText(@IdRes int viewId, @StringRes int strId) {
        TextView view = findViewById(viewId);
        view.setText(strId);
        return this;
    }
 
    public CircleViewHolder setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {
        ImageView view = findViewById(viewId);
        view.setImageResource(imageResId);
        return this;
    }
 
    public CircleViewHolder setBackgroundColor(@IdRes int viewId, @ColorInt int color) {
        View view = findViewById(viewId);
        view.setBackgroundColor(color);
        return this;
    }
 
    public CircleViewHolder setBackgroundRes(@IdRes int viewId, @DrawableRes int backgroundRes) {
        View view = findViewById(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }
 
    public CircleViewHolder setBackgroundDrawable(@IdRes int viewId, Drawable background) {
        View view = findViewById(viewId);
        if (Controller.SDK_JELLY_BEAN) {
            view.setBackground(background);
        } else {
            view.setBackgroundDrawable(background);
        }
        return this;
    }
 
    public CircleViewHolder setVisibility(@IdRes int viewId, int visibility) {
        View view = findViewById(viewId);
        view.setVisibility(visibility);
        return this;
    }
 
    public CircleViewHolder setChecked(@IdRes int viewId, boolean checked) {
        View view = findViewById(viewId);
        if (view instanceof Checkable) {
            ((Checkable) view).setChecked(checked);
        }
        return this;
    }
 
    public CircleViewHolder setEnabled(@IdRes int viewId, boolean enable) {
        View view = findViewById(viewId);
        view.setEnabled(enable);
        return this;
    }
 
    public CircleViewHolder addTextChangedBeforeListener(@IdRes int viewId, final TextWatcherBefore textWatcherBefore) {
        View view = findViewById(viewId);
        if (view instanceof TextView) {
            ((TextView) view).addTextChangedListener(new android.text.TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    textWatcherBefore.beforeTextChanged(s, start, count, after);
                }
 
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
 
                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
        return this;
    }
 
    public CircleViewHolder addTextChangedListener(@IdRes int viewId, final TextWatcher textWatcher) {
        View view = findViewById(viewId);
        if (view instanceof TextView) {
            addTextChangedListener(((TextView) view), textWatcher);
        }
        return this;
    }
 
 
    public CircleViewHolder addTextChangedListener(TextView textView, final TextWatcher textWatcher) {
        textView.addTextChangedListener(new android.text.TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textWatcher.onTextChanged(s, start, before, count);
            }
 
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
 
        return this;
    }
 
    public CircleViewHolder addTextChangedAfterListener(@IdRes int viewId, final TextWatcherAfter textWatcherAfter) {
        View view = findViewById(viewId);
        if (view instanceof TextView) {
            ((TextView) view).addTextChangedListener(new android.text.TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
 
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
 
                @Override
                public void afterTextChanged(Editable s) {
                    textWatcherAfter.afterTextChanged(s);
                }
            });
        }
        return this;
    }
 
 
    public <T extends View> T findViewById(@IdRes int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = dialogView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }
 
 
    public interface TextWatcherBefore {
        void beforeTextChanged(CharSequence s, int start, int count, int after);
    }
 
    public interface TextWatcher {
        void onTextChanged(CharSequence s, int start, int before, int count);
    }
 
    public interface TextWatcherAfter {
        void afterTextChanged(Editable s);
    }
}