Skip to content

Commit

Permalink
省市区选择器支持隐藏区级别 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
duanhong169 committed Aug 16, 2018
1 parent 78064d9 commit 21897dd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import top.defaults.view.Division;
import top.defaults.view.DivisionPickerView;

public class DivisionPickerActivity extends AppCompatActivity {

@BindView(R.id.divisionPicker) DivisionPickerView divisionPicker;
@BindView(R.id.textView) TextView textView;
@OnCheckedChanged(R.id.toggleType)
void toggleType(boolean isChecked) {
divisionPicker.setType(isChecked ? DivisionPickerView.TYPE_ALL : DivisionPickerView.TYPE_PROVINCE_AND_CITY);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_division_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:preferredMaxOffsetItemCount="4"
app:divisionPickerType="provinceAndCity"
android:background="#e7e7e7"/>

<TextView
Expand All @@ -22,4 +23,11 @@
android:textColor="@color/colorAccent"
android:gravity="center"/>

<ToggleButton
android:id="@+id/toggleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="省市区"
android:textOff="省市"/>

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package top.defaults.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import java.util.List;
Expand All @@ -15,6 +16,10 @@ public class DivisionPickerView extends PickerViewGroup {
private PickerView cityPicker;
private PickerView divisionPicker;

public static final int TYPE_ALL = 0;
public static final int TYPE_PROVINCE_AND_CITY = 1;
private int type = TYPE_ALL;

public DivisionPickerView(Context context) {
this(context, null);
}
Expand All @@ -25,13 +30,34 @@ public DivisionPickerView(Context context, AttributeSet attrs) {

public DivisionPickerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
provincePicker = new PickerView(context);
cityPicker = new PickerView(context);
divisionPicker = new PickerView(context);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DivisionPickerView);
type = typedArray.getInt(R.styleable.DivisionPickerView_divisionPickerType, TYPE_ALL);
typedArray.recycle();

provincePicker = new PickerView(context);
settlePickerView(provincePicker);

cityPicker = new PickerView(context);
settlePickerView(cityPicker);

divisionPicker = new PickerView(context);
settlePickerView(divisionPicker);

configure();
}

public void setType(int type) {
this.type = type;
configure();
}

private void configure() {
if (type == TYPE_PROVINCE_AND_CITY) {
divisionPicker.setVisibility(GONE);
} else {
divisionPicker.setVisibility(VISIBLE);
}
}

public interface OnSelectedDivisionChangedListener {
Expand Down Expand Up @@ -93,7 +119,10 @@ public PickerView getDivisionPicker() {
}

public Division getSelectedDivision() {
Division division = divisionPicker.getSelectedItem(Division.class);
Division division = null;
if (type == TYPE_ALL) {
division = divisionPicker.getSelectedItem(Division.class);
}
if (division == null) {
division = cityPicker.getSelectedItem(Division.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@
<enum name="thirty" value="30"/>
</attr>
</declare-styleable>

<declare-styleable name="DivisionPickerView">
<attr name="divisionPickerType" format="enum">
<enum name="all" value="0"/>
<enum name="provinceAndCity" value="1"/>
</attr>
</declare-styleable>
</resources>

0 comments on commit 21897dd

Please sign in to comment.