MultiSelectDialog.java 13.8 KB
Newer Older
konghaorui committed
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
package com.yidianling.avchatkit.common.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.yidianling.im.R;
import com.yidianling.avchatkit.common.adapter.TAdapter;
import com.yidianling.avchatkit.common.adapter.TAdapterDelegate;
import com.yidianling.avchatkit.common.adapter.TViewHolder;
import com.yidianling.avchatkit.common.util.ScreenUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 多选对话框.
 */

public class MultiSelectDialog extends Dialog {
    private ListView listView;
    private int itemSize = 0;
    private List<Pair<String, Boolean>> itemTextList = new LinkedList<Pair<String, Boolean>>();
    private BaseAdapter listAdapter;
    private AdapterView.OnItemClickListener itemListener;

    private Context context;

    public static final int NO_TEXT_COLOR = -99999999;

    public static final int NO_TEXT_SIZE = -99999999;

    private View titleView;

    private ImageButton titleBtn;

    private TextView titleTV;

    private TextView messageTV;

    private TextView message2TV;

    private Button positiveBtn, negativeBtn;

    private View btnDivideView;

    private CharSequence title = "", message = "", message2 = "", positiveBtnTitle = "", negativeBtnTitle = "";

    private int titleTextColor = NO_TEXT_COLOR, msgTextColor = NO_TEXT_COLOR,
            positiveBtnTitleTextColor = NO_TEXT_COLOR, negativeBtnTitleTextColor = NO_TEXT_COLOR;

    private float titleTextSize = NO_TEXT_SIZE, msgTextSize = NO_TEXT_SIZE, positiveBtnTitleTextSize = NO_TEXT_SIZE,
            negativeBtnTitleTextSize = NO_TEXT_SIZE;

    private int resourceId;

    private boolean isPositiveBtnVisible = true, isNegativeBtnVisible = false;

    private boolean isTitleVisible = false, isMessageVisble = true, isTitleBtnVisible = false;

    private View.OnClickListener positiveBtnListener, negativeBtnListener;

    private HashMap<Integer, View.OnClickListener> mViewListener = new HashMap<Integer, View.OnClickListener>();

    public MultiSelectDialog(Context context, int resourceId) {
        super(context, R.style.dialog_default_style);
        this.context = context;
        if (-1 != resourceId) {
            setContentView(resourceId);
            this.resourceId = resourceId;
        }
        WindowManager.LayoutParams Params = getWindow().getAttributes();
        Params.width = WindowManager.LayoutParams.MATCH_PARENT;
        Params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes((WindowManager.LayoutParams) Params);
        initAdapter();
    }

    public void setTitle(CharSequence title) {
        isTitleVisible = TextUtils.isEmpty(title) ? false : true;
        setTitleVisible(isTitleVisible);
        if (null != title) {
            this.title = title;
            if (null != titleTV)
                titleTV.setText(title);
        }
    }

    public void setTitleVisible(boolean visible) {
        isTitleVisible = visible;
        if (titleView != null) {
            titleView.setVisibility(isTitleVisible ? View.VISIBLE : View.GONE);
        }
    }

    public void setTitleBtnVisible(boolean visible) {
        isTitleBtnVisible = visible;
        if (titleBtn != null) {
            titleBtn.setVisibility(isTitleBtnVisible ? View.VISIBLE : View.GONE);
        }
    }

    public void setTitleTextColor(int color) {
        titleTextColor = color;
        if (null != titleTV && NO_TEXT_COLOR != color)
            titleTV.setTextColor(color);
    }

    public void setMessageTextColor(int color) {
        msgTextColor = color;
        if (null != messageTV && NO_TEXT_COLOR != color)
            messageTV.setTextColor(color);

    }

    public void setMessageTextSize(float size) {
        msgTextSize = size;
        if (null != messageTV && NO_TEXT_SIZE != size)
            messageTV.setTextSize(size);
    }

    public void setTitleTextSize(float size) {
        titleTextSize = size;
        if (null != titleTV && NO_TEXT_SIZE != size)
            titleTV.setTextSize(size);
    }

    public void setMessageVisible(boolean visible) {
        isMessageVisble = visible;
        if (messageTV != null) {
            messageTV.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    public void setMessage(CharSequence message) {
        if (null != message) {
            this.message = message;
            if (null != messageTV)
                messageTV.setText(message);
        }
    }

    public void setMessage2(CharSequence message) {
        if (!TextUtils.isEmpty(message)) {
            this.message2 = message;
            if (null != message2TV) {
                message2TV.setText(message);
            }
        }
    }

    public void addPositiveButton(CharSequence title, int color, float size,
                                  View.OnClickListener positiveBtnListener) {
        isPositiveBtnVisible = true;
        positiveBtnTitle = TextUtils.isEmpty(title) ? context
konghaorui committed
170
                .getString(R.string.im_ok) : title;
konghaorui committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        positiveBtnTitleTextColor = color;
        positiveBtnTitleTextSize = size;
        this.positiveBtnListener = positiveBtnListener;

        if (positiveBtn != null) {
            positiveBtn.setText(positiveBtnTitle);
            positiveBtn.setTextColor(positiveBtnTitleTextColor);
            positiveBtn.setTextSize(positiveBtnTitleTextSize);
            positiveBtn.setOnClickListener(positiveBtnListener);
        }
    }

    public void addNegativeButton(CharSequence title, int color, float size,
                                  View.OnClickListener negativeBtnListener) {
        isNegativeBtnVisible = true;
        negativeBtnTitle = TextUtils.isEmpty(title) ? context
konghaorui committed
187
                .getString(R.string.im_cancel) : title;
konghaorui committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        negativeBtnTitleTextColor = color;
        negativeBtnTitleTextSize = size;
        this.negativeBtnListener = negativeBtnListener;

        if (negativeBtn != null) {
            negativeBtn.setText(negativeBtnTitle);
            negativeBtn.setTextColor(negativeBtnTitleTextColor);
            negativeBtn.setTextSize(negativeBtnTitleTextSize);
            negativeBtn.setOnClickListener(negativeBtnListener);
        }
    }

    public void addPositiveButton(CharSequence title,
                                  View.OnClickListener positiveBtnListener) {
        addPositiveButton(title, NO_TEXT_COLOR, NO_TEXT_SIZE,
                positiveBtnListener);
    }

    public void addNegativeButton(CharSequence title,
                                  View.OnClickListener negativeBtnListener) {
        addNegativeButton(title, NO_TEXT_COLOR, NO_TEXT_SIZE,
                negativeBtnListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(resourceId);
        try {
            ViewGroup root = (ViewGroup) findViewById(R.id.multi_select_dialog_layout);
            if (root != null) {
                ViewGroup.LayoutParams params = root.getLayoutParams();
                params.width = (int) ScreenUtil.getDialogWidth();
                root.setLayoutParams(params);
            }

            titleView = findViewById(R.id.multi_select_dialog_title_view);
            if (titleView != null) {
                setTitleVisible(isTitleVisible);
            }
            titleBtn = (ImageButton) findViewById(R.id.multi_select_dialog_title_button);
            if (titleBtn != null) {
                setTitleBtnVisible(isTitleBtnVisible);
            }
            titleTV = (TextView) findViewById(R.id.multi_select_dialog_title_text_view);
            if (titleTV != null) {
                titleTV.setText(title);
                if (NO_TEXT_COLOR != titleTextColor)
                    titleTV.setTextColor(titleTextColor);
                if (NO_TEXT_SIZE != titleTextSize)
                    titleTV.setTextSize(titleTextSize);
            }

            messageTV = (TextView) findViewById(R.id.multi_select_dialog_message_text_view);
            if (messageTV != null) {
                messageTV.setText(message);
                setMessageVisible(isMessageVisble);
                if (NO_TEXT_COLOR != msgTextColor)
                    messageTV.setTextColor(msgTextColor);
                if (NO_TEXT_SIZE != msgTextSize)
                    messageTV.setTextSize(msgTextSize);
            }

            message2TV = (TextView) findViewById(R.id.multi_select_dialog_message_2);
            if (message2TV != null && !TextUtils.isEmpty(message2)) {
                message2TV.setVisibility(View.VISIBLE);
                message2TV.setText(message2);
            }

            positiveBtn = (Button) findViewById(R.id.multi_select_dialog_positive_btn);
            if (isPositiveBtnVisible && positiveBtn != null) {
                positiveBtn.setVisibility(View.VISIBLE);
                if (NO_TEXT_COLOR != positiveBtnTitleTextColor) {
                    positiveBtn.setTextColor(positiveBtnTitleTextColor);
                }
                if (NO_TEXT_SIZE != positiveBtnTitleTextSize) {
                    positiveBtn.setTextSize(positiveBtnTitleTextSize);
                }
                positiveBtn.setText(positiveBtnTitle);
                positiveBtn.setOnClickListener(positiveBtnListener);
            }

            boolean hasChecked = false;
            for (Pair<String, Boolean> pair : itemTextList) {
                if (pair.second == true) {
                    hasChecked = true;
                }
            }
            positiveBtn.setEnabled(hasChecked);


            negativeBtn = (Button) findViewById(R.id.multi_select_dialog_negative_btn);
            btnDivideView = findViewById(R.id.multi_select_dialog_btn_divide_view);
            if (isNegativeBtnVisible) {
                negativeBtn.setVisibility(View.VISIBLE);
                btnDivideView.setVisibility(View.VISIBLE);
                if (NO_TEXT_COLOR != this.negativeBtnTitleTextColor) {
                    negativeBtn.setTextColor(negativeBtnTitleTextColor);
                }
                if (NO_TEXT_SIZE != this.negativeBtnTitleTextSize) {
                    negativeBtn.setTextSize(negativeBtnTitleTextSize);
                }
                negativeBtn.setText(negativeBtnTitle);
                negativeBtn.setOnClickListener(negativeBtnListener);
            }

            if (mViewListener != null && mViewListener.size() != 0) {
                Iterator iter = mViewListener.entrySet().iterator();
                View view = null;
                while (iter.hasNext()) {
                    Map.Entry<Integer, View.OnClickListener> entry = (Map.Entry) iter.next();
                    view = findViewById(entry.getKey());
                    if (view != null && entry.getValue() != null) {
                        view.setOnClickListener(entry.getValue());
                    }
                }
            }
            listView = (ListView) findViewById(R.id.multi_select_dialog_list_view);
            if (itemSize > 0) {
                updateListView();
            }
        } catch (Exception e) {

        }
    }

    public int getResourceId() {
        return resourceId;
    }

    public void setResourceId(int resourceId) {
        this.resourceId = resourceId;
    }

    public Button getPositiveBtn() {
        return positiveBtn;
    }

    public Button getNegativeBtn() {
        return negativeBtn;
    }

    public void setViewListener(int viewId, View.OnClickListener listener) {
        mViewListener.put(viewId, listener);
    }


    private void initAdapter() {
        listAdapter = new TAdapter<>(context, itemTextList, new TAdapterDelegate() {
            @Override
            public int getViewTypeCount() {
                return itemTextList.size();
            }

            @Override
            public Class<? extends TViewHolder> viewHolderAtPosition(int position) {
                return MultiSelectDialogViewHolder.class;
            }

            @Override
            public boolean enabled(int position) {
                return true;
            }
        });
        itemListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                itemTextList.set(position, new Pair<String, Boolean>(itemTextList.get(position).first, !itemTextList.get(position).second));
                boolean hasChecked = false;
                for (Pair<String, Boolean> pair : itemTextList) {
                    if (pair.second == true) {
                        hasChecked = true;
                    }
                }
                positiveBtn.setEnabled(hasChecked);
                listAdapter.notifyDataSetChanged();
            }
        };
    }

    public MultiSelectDialog(Context context) {
konghaorui committed
369
        this(context, R.layout.im_multi_select_dialog_default_layout);
konghaorui committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    }

    private void updateListView() {
        listAdapter.notifyDataSetChanged();
        if (listView != null) {
            listView.setAdapter(listAdapter);
            listView.setOnItemClickListener(itemListener);
        }
    }

    public void addItem(String itemText, boolean isChecked) {
        itemTextList.add(new Pair<String, Boolean>(itemText, isChecked));
        itemSize = itemTextList.size();
    }

    public void clearData() {
        itemTextList.clear();
        itemSize = 0;
    }

    @Override
    public void show() {
        if (itemSize <= 0) {
            return;
        }
        updateListView();
        super.show();
    }

    public List<Pair<String, Boolean>> getItemTextList() {
        return itemTextList;
    }
}