OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.widget; | |
6 | |
7 import android.content.Context; | |
8 import android.support.annotation.Nullable; | |
9 import android.util.AttributeSet; | |
10 import android.view.LayoutInflater; | |
11 import android.view.View; | |
12 import android.widget.RadioButton; | |
13 import android.widget.RadioGroup; | |
14 | |
15 import org.chromium.chrome.R; | |
16 | |
17 import java.util.List; | |
18 | |
19 /** | |
20 * Manages a group of exclusive RadioButtons, automatically inserting a margin i n between the rows | |
21 * to prevent them from squishing together. | |
22 * | |
23 * ------------------------------------------------- | |
24 * | O | MESSAGE #1 | | |
25 * | O | MESSAGE #N | | |
26 * ------------------------------------------------- | |
27 */ | |
28 public final class RadioButtonLayout extends RadioGroup { | |
29 public static final int INVALID_INDEX = -1; | |
30 | |
31 private final int mMarginBetweenRows; | |
32 | |
33 public RadioButtonLayout(Context context) { | |
34 this(context, null); | |
35 } | |
36 | |
37 public RadioButtonLayout(Context context, AttributeSet attrs) { | |
38 super(context, attrs); | |
39 mMarginBetweenRows = context.getResources().getDimensionPixelSize( | |
40 R.dimen.infobar_control_margin_between_rows); | |
41 } | |
42 | |
43 /** | |
44 * Adds a set of standard radio buttons for the given messages and adds them to the layout. | |
45 * | |
46 * @param messages Messages to display for the options. | |
47 * @param tags Optional list of tags to attach to the buttons. | |
48 */ | |
49 public void addOptions(List<CharSequence> messages, @Nullable List<?> tags) { | |
50 if (tags != null) assert tags.size() == messages.size(); | |
51 | |
52 for (int i = 0; i < messages.size(); i++) { | |
53 RadioButton button = (RadioButton) LayoutInflater.from(getContext()) | |
54 .inflate(R.layout.radio_button_layout_e lement, null); | |
55 button.setText(messages.get(i)); | |
56 if (tags != null) button.setTag(tags.get(i)); | |
57 | |
58 addView(button, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutPa rams.WRAP_CONTENT)); | |
59 } | |
60 | |
61 updateMargins(); | |
62 } | |
63 | |
64 /** | |
65 * Marks a RadioButton child as being checked. | |
66 * | |
67 * Android doesn't provide a way of generating View IDs on the fly before AP I level 17, so this | |
Theresa
2017/05/01 15:21:09
Android's method of generating View IDs could be c
gone
2017/05/01 15:29:30
Figured that kind of logic be used elsewhere, but
Theresa
2017/05/01 15:43:08
Yes, fine to save for later. I think that we only
| |
68 * function requires passing in the child's index. Passing in {@link #INVAL ID_INDEX} marks them | |
69 * all as de-selected. | |
70 * | |
71 * @param childIndex Index of the child to select. | |
72 */ | |
73 public void selectChildAtIndex(int childIndex) { | |
74 int childCount = getChildCount(); | |
75 for (int i = 0; i < childCount; i++) { | |
76 RadioButton child = (RadioButton) getChildAt(i); | |
77 child.setChecked(i == childIndex); | |
78 } | |
79 } | |
80 | |
81 /** Sets margins between each of the radio buttons. */ | |
82 private void updateMargins() { | |
83 int childCount = getChildCount(); | |
84 for (int i = 0; i < childCount; i++) { | |
85 View child = getChildAt(i); | |
86 int margin = (i < childCount - 1) ? mMarginBetweenRows : 0; | |
87 ((MarginLayoutParams) child.getLayoutParams()).bottomMargin = margin ; | |
Theresa
2017/05/01 15:21:09
Is it possible to fold this into the loop in addOp
gone
2017/05/01 15:29:30
I had it there originally, but that doesn't work i
Theresa
2017/05/01 15:43:08
I think updateMargins() is a fine name. I wasn't t
| |
88 } | |
89 } | |
90 } | |
OLD | NEW |