自定义GridView+Dailog+RadioButton

1.效果

首先看下效果:
截图

1.1使用:

onCreate

1
2
3
4
5
6
7
8
9
10
11
12
/*初始化*/
public void initData() {
mSingleDataList = new ArrayList<String>();
for (int i = 0; i < 9; i++) {
String string1 = i + "线";
mSingleDataList.add(string1);
}
initDailog();
}
public void initDailog() {
mSingleChoiceDialog = new SingleChoiceDialog(this, mSingleDataList);
}

@OnClick

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case R.id.tv_banci_loginconfig:
//班次选择
boolean showing = mSingleChoiceDialog.isShowing();
if (!showing) {
mSingleChoiceDialog.setTitle("请选择班次");
mSingleChoiceDialog.setAdapterDate(mSingleDataList);
mSingleChoiceDialog.setOnOKButtonListener(new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
int selItem = mSingleChoiceDialog.getSelectItem();
tv_banci_loginconfig.setText(mSingleDataList.get(selItem));
right_go2.setImageDrawable(getDrawable(R.mipmap.right_goto));
}
});
mSingleChoiceDialog.show();
}
break;

Dialog

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
public class SingleChoiceDialog extends AbstractChoickDialog{
private SingleChoicAdapter<String> mSingleChoicAdapter;
public SingleChoiceDialog(Context context, List<String> list) {
super(context, list);
initData();
}
protected void initData() {
// TODO Auto-generated method stub
mSingleChoicAdapter = new SingleChoicAdapter<String>(mContext, mList,
R.drawable.selector_checkbox2);
mListView.setAdapter(mSingleChoicAdapter);
mListView.setOnItemClickListener(mSingleChoicAdapter);
AbViewUtil.setListViewHeightBasedOnChildren(mListView);
}
public int getSelectItem()
{
return mSingleChoicAdapter.getSelectItem();
}
public void setAdapterDate(List<String> list) {
this.mList=list;
this.mSingleChoicAdapter.notifyDataSetChanged();
}
}

AbstractChoickDialog

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
public abstract class AbstractChoickDialog extends Dialog implements OnClickListener {
protected Context mContext;
protected View mParentView;
protected View mRootView;
protected ScrollView mScrollView;
protected TextView mTVTitle;
protected Button mButtonOK;
protected Button mButtonCancel;
protected GridView mListView;
protected List<String> mList;
protected OnClickListener mOkClickListener;
public AbstractChoickDialog(Context context, List<String> list) {
super(context);
// TODO Auto-generated constructor stub
mContext = context;
mList = list;
initView(mContext);
}
protected void initView(Context context) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
p.width = d.getWidth() - 100; //设置dialog的宽度为当前手机屏幕的宽度-100
getWindow().setAttributes(p);
setContentView(R.layout.popwindow_listview_layout);
mScrollView = (ScrollView) findViewById(R.id.scrollView);
mTVTitle = (TextView) findViewById(R.id.tvTitle);
mButtonOK = (Button) findViewById(R.id.btnOK);
mButtonOK.setOnClickListener(this);
mButtonCancel = (Button) findViewById(R.id.btnCancel);
mButtonCancel.setOnClickListener(this);
mListView = (GridView) findViewById(R.id.listView);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setTitle(String title) {
mTVTitle.setText(title);
}
public void setOnOKButtonListener(OnClickListener onClickListener) {
mOkClickListener = onClickListener;
}
public void onClick(View v) {
int i = v.getId();
if (i == R.id.btnOK) {
onButtonOK();
} else if (i == R.id.btnCancel) {
onButtonCancel();
}
}
protected void onButtonOK() {
dismiss();
if (mOkClickListener != null) {
mOkClickListener.onClick(this, 0);
}
}
protected void onButtonCancel() {
dismiss();
}
}

布局

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/dailog_white_shape"
android:gravity="center_vertical"
android:orientation="horizontal">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="18dp"
android:layout_marginTop="18dp"
android:gravity="center"
android:textColor="#000"
android:textSize="16sp" />
<View
android:id="@+id/grey_line"
android:layout_width="259dp"
android:layout_height="1dp"
android:layout_below="@+id/tvTitle"
android:layout_centerHorizontal="true"
android:background="#c0c0c0" />
<GridView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/grey_line"
android:numColumns="2"
android:paddingLeft="70dp"
android:paddingRight="70dp"
android:scrollbars="none" />
<!--android:cacheColorHint="#00000000"-->
<!--android:divider="@drawable/pop_listview_diver"-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listView"
android:paddingLeft="60dp"
android:paddingRight="60dp"
android:paddingBottom="20dp">
<Button
android:id="@+id/btnCancel"
android:layout_width="68dp"
android:layout_height="27dp"
android:layout_weight="1"
android:background="@drawable/button_gray_shape"
android:text="取消"
android:textColor="#fff"
android:textSize="16sp" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<Button
android:id="@+id/btnOK"
android:layout_width="68dp"
android:layout_height="27dp"
android:layout_weight="1"
android:background="@drawable/button_green_shape"
android:text="确定"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>

selector_checkbox2

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_checked="false" android:drawable="@mipmap/checkbut_unsel" />
<item android:state_enabled="true" android:state_checked="true" android:drawable="@mipmap/checkbut_sel" />
<item android:drawable="@mipmap/checkbut_unsel" />
</selector>

SingleChoicAdapter

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
public class SingleChoicAdapter<T> extends BaseAdapter implements OnItemClickListener {
private Context mContext;
private List<T> mObjects = new ArrayList<T>();
private int mCheckBoxResourceID = 0;
private int mSelectItem = 0;
private LayoutInflater mInflater;
public SingleChoicAdapter(Context context, int checkBoxResourceId) {
init(context, checkBoxResourceId);
}
public SingleChoicAdapter(Context context, List<T> objects, int checkBoxResourceId) {
init(context, checkBoxResourceId);
if (objects != null)
{
mObjects = objects;
}
}
private void init(Context context, int checkBoResourceId) {
mContext = context;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mCheckBoxResourceID = checkBoResourceId;
}
public void refreshData(List<T> objects)
{
if (objects != null)
{
mObjects = objects;
setSelectItem(0);
}
}
public void setSelectItem(int selectItem)
{
if (selectItem >= 0 && selectItem < mObjects.size())
{
mSelectItem = selectItem;
notifyDataSetChanged();
}
}
public int getSelectItem()
{
return mSelectItem;
}
public void clear() {
mObjects.clear();
notifyDataSetChanged();
}
public int getCount() {
return mObjects.size();
}
public T getItem(int position) {
return mObjects.get(position);
}
public int getPosition(T item) {
return mObjects.indexOf(item);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.choice_list_item_layout, null);
viewHolder = new ViewHolder();
viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textView);
viewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
if (mCheckBoxResourceID != 0)
{
viewHolder.mCheckBox.setButtonDrawable(mCheckBoxResourceID);
}
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mCheckBox.setChecked(mSelectItem == position);
T item = getItem(position);
if (item instanceof CharSequence) {
viewHolder.mTextView.setText((CharSequence)item);
} else {
viewHolder.mTextView.setText(item.toString());
}
return convertView;
}
public static class ViewHolder
{
public TextView mTextView;
public CheckBox mCheckBox;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
if (position != mSelectItem)
{
mSelectItem = position;
notifyDataSetChanged();
}
}
}

choice_list_item_layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:gravity="center">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"/>
<TextView
android:id="@+id/textView"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"/>
</LinearLayout>

button_gray_shape.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#c0c0c0"/>
<!--圆角弧度可自己修改-->
<corners android:radius="8dp"/>
<stroke
android:width="0.5dp"
android:color="#EDEDEF"/>
</shape>

button_green_shape.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#32b16c"/>
<!--圆角弧度可自己修改-->
<corners android:radius="8dp"/>
<stroke
android:width="0.5dp"
android:color="#EDEDEF"/>
</shape>

OK 该有的都有了…

如果还有不清楚的可邮件联系我liuxinmingab@163.com