Skip to content

Instantly share code, notes, and snippets.

@edBaev
Created March 13, 2014 10:46
Show Gist options
  • Save edBaev/9526075 to your computer and use it in GitHub Desktop.
Save edBaev/9526075 to your computer and use it in GitHub Desktop.
Стало
package com.buyfolio.control;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.buyfolio.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Dmitriy Dovbnya on 07.03.14.
*/
public class LinearRadioGroup extends RadioGroup {
private static final String PLUS = "+";
private LayoutInflater inflater;
private List<String> values;
private List<RadioButton> views;
public LinearRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
this.inflater = LayoutInflater.from(context);
}
public void setValues(List<String> values) {
this.values = values;
}
public void createViews() {
removeAllViews();
if (views == null) {
views = new ArrayList<RadioButton>();
} else {
views.clear();
}
int last = values.size() - 1;
addView(createLeftView(values.get(0)));
for (int i = 1; i < last; i++) {
addView(createView(values.get(i)));
}
addView(createRightView(values.get(last)));
}
public void checkValue(String value) {
if (!TextUtils.isEmpty(value)) {
int checked = values.indexOf(value);
if (checked >= 0) {
views.get(checked).setChecked(true);
} else {
views.get(0).setChecked(true);
}
}
}
private RadioButton createLeftView(String value) {
RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.linear_radiobutton_left, this, false);
radioButton.setId(value.hashCode());
radioButton.setTag(value);
radioButton.setText(getResources().getString(R.string.radio_buttons_any));
return radioButton;
}
private RadioButton createView(String value) {
RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.linear_radiobutton_item, this, false);
radioButton.setId(value.hashCode());
radioButton.setTag(value);
radioButton.setText(value + PLUS);
return radioButton;
}
private RadioButton createRightView(String value) {
RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.linear_radiobutton_right, this, false);
radioButton.setId(value.hashCode());
radioButton.setTag(value);
radioButton.setText(value + PLUS);
return radioButton;
}
public void addView(RadioButton child) {
super.addView(child);
views.add(child);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment