kimi
2021-02-18 0ac056bb5b4c567293482286c80a1b83a467cd33
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
package com.mylhyl.circledialog.view;
 
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
 
import com.mylhyl.circledialog.callback.CircleItemLabel;
import com.mylhyl.circledialog.callback.CircleItemViewBinder;
import com.mylhyl.circledialog.internal.Controller;
import com.mylhyl.circledialog.params.DialogParams;
import com.mylhyl.circledialog.params.ItemsParams;
import com.mylhyl.circledialog.res.drawable.CircleDrawableSelector;
import com.mylhyl.circledialog.res.values.CircleColor;
import com.mylhyl.circledialog.view.listener.ItemsView;
import com.mylhyl.circledialog.view.listener.OnRvItemClickListener;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * Created by hupei on 2017/3/30.
 */
 
final class BodyListView extends ListView implements ItemsView {
    private BaseAdapter mAdapter;
    private DialogParams mDialogParams;
    private ItemsParams mItemsParams;
    private int mBackgroundColor;
    private int mBackgroundColorPress;
 
    public BodyListView(Context context, DialogParams dialogParams, ItemsParams itemsParams) {
        super(context);
        this.mDialogParams = dialogParams;
        this.mItemsParams = itemsParams;
        init();
    }
 
    @Override
    public void refreshItems() {
        mAdapter.notifyDataSetChanged();
    }
 
    @Override
    public void regOnItemClickListener(AdapterView.OnItemClickListener listener) {
        setOnItemClickListener(listener);
    }
 
    @Override
    public void regOnItemClickListener(OnRvItemClickListener listener) {
 
    }
 
    @Override
    public View getView() {
        return this;
    }
 
    private void init() {
 
        //如果没有背景色,则使用默认色
        this.mBackgroundColor = mItemsParams.backgroundColor != 0 ?
                mItemsParams.backgroundColor : mDialogParams.backgroundColor;
        this.mBackgroundColorPress = mItemsParams.backgroundColorPress != 0 ?
                mItemsParams.backgroundColorPress : mDialogParams.backgroundColorPress;
 
        setBackgroundColor(mBackgroundColor);
 
        CircleDrawableSelector bgItemNotRadius = new CircleDrawableSelector(Color.TRANSPARENT, mBackgroundColorPress);
 
        setSelector(bgItemNotRadius);
        setDivider(new ColorDrawable(CircleColor.divider));
        setDividerHeight(Controller.dp2px(getContext(), mItemsParams.dividerHeight));
 
        mAdapter = mItemsParams.adapter;
        if (mAdapter == null) {
            mAdapter = new ItemsAdapter(getContext(), mDialogParams, mItemsParams);
        }
        setAdapter(mAdapter);
    }
 
    static class ItemsAdapter<T> extends BaseAdapter {
        private Context mContext;
        private List<T> mItems;
        private ItemsParams mItemsParams;
        private DialogParams mDialogParams;
 
        public ItemsAdapter(Context context, DialogParams dialogParams, ItemsParams params) {
            this.mContext = context;
            this.mDialogParams = dialogParams;
            this.mItemsParams = params;
 
            Object entity = mItemsParams.items;
            if (entity != null && entity instanceof Iterable) {
                this.mItems = (List<T>) entity;
            } else if (entity != null && entity.getClass().isArray()) {
                this.mItems = Arrays.asList((T[]) entity);
            } else if (entity != null) {
                throw new IllegalArgumentException("entity must be an Array or an Iterable.");
            }
        }
 
        @Override
        public int getCount() {
            if (mItems != null)
                return mItems.size();
            return 0;
        }
 
        @Override
        public T getItem(int position) {
            if (mItems != null)
                return mItems.get(position);
            return null;
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                TextView textView = new TextView(mContext);
                if (mDialogParams.typeface != null) {
                    textView.setTypeface(mDialogParams.typeface);
                }
                textView.setGravity(Gravity.CENTER);
                textView.setTextSize(mItemsParams.textSize);
                textView.setTextColor(mItemsParams.textColor);
                textView.setHeight(Controller.dp2px(mContext, mItemsParams.itemHeight));
                if (mItemsParams.padding != null) {
                    textView.setPadding(Controller.dp2px(mContext, mItemsParams.padding[0]),
                            Controller.dp2px(mContext, mItemsParams.padding[1]),
                            Controller.dp2px(mContext, mItemsParams.padding[2]),
                            Controller.dp2px(mContext, mItemsParams.padding[3]));
                }
                if (mItemsParams.textGravity != Gravity.NO_GRAVITY) {
                    textView.setGravity(mItemsParams.textGravity);
                }
                viewHolder.item = textView;
                convertView = textView;
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
 
            bindView(position, viewHolder);
 
            return convertView;
        }
 
        private void bindView(int position, ViewHolder viewHolder) {
            String label;
            T item = getItem(position);
            if (item instanceof CircleItemLabel) {
                label = ((CircleItemLabel) item).getItemLabel();
            } else {
                label = item.toString();
            }
            viewHolder.item.setText(String.valueOf(label));
 
            CircleItemViewBinder viewBinder = mItemsParams.viewBinder;
            if (viewBinder != null) {
                viewBinder.onBinder(viewHolder.item, item, position);
            }
        }
 
        class ViewHolder {
            TextView item;
        }
    }
}