常用底部滚动选择框
程序员文章站
2022-05-31 16:49:27
...
效果:
代码:
public class SelectDialog extends BaseDialog {
private OnSelectListener selectListener;
private List<SelectItemBean> dataList;
private WheelVerticalView wheel_select;
private TextView tv_cancel;
private RelativeLayout rl_select_layout;
public SelectDialog(Context context) {
super(context);
}
public SelectDialog(Context context, List dataList, boolean canceledOnTouchOutside, OnSelectListener selectListener) {
super(context, canceledOnTouchOutside);
this.dataList = dataList;
this.selectListener = selectListener;
}
@Override
protected int getLayoutId() {
return R.layout.dlg_select;
}
@Override
protected void initView(View rootView) {
wheel_select = (WheelVerticalView) rootView.findViewById(R.id.wheel);
tv_cancel = (TextView) rootView.findViewById(R.id.tv_cancel);
rl_select_layout = (RelativeLayout) rootView.findViewById(R.id.rl_select_bankcard_layout);
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SelectDialog.this.dismiss();
}
});
rl_select_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SelectDialog.this.dismiss();
}
});
rootView.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectListener.onSelect(wheel_select.getCurrentItem());
SelectDialog.this.dismiss();
}
});
getWindow().setWindowAnimations(R.style.anim_bottom_alpha);
}
@Override
protected void initData() {
if (dataList == null || dataList.size() == 0)
return;
wheel_select.setViewAdapter(new ArrayWheelAdapter<>(mContext, R.layout.item_select_dialog, R.id.tv_content, dataList));
}
public void setSelection(int position) {
wheel_select.setCurrentItem(position);
}
public interface OnSelectListener {
void onSelect(int position);
}
private static class ArrayWheelAdapter<T> extends AbstractWheelTextAdapter {
private List<T> items;
public ArrayWheelAdapter(Context context, int itemResource, int itemTextResource, List<T> items) {
super(context, itemResource, itemTextResource);
this.items = items;
}
@Override
public CharSequence getItemText(int index) {
if (index >= 0 && index < items.size()) {
T item = items.get(index);
if (item instanceof SelectItemBean) {
return ((SelectItemBean) item).content;
}
return item.toString();
}
return null;
}
@Override
public int getItemsCount() {
return items.size();
}
}
}
布局:dlg_select.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_select_bankcard_layout"
android:clickable="true">
<LinearLayout
android:id="@+id/lay_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:orientation="vertical"
android:clickable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="96px"
android:background="@color/color_f6f6f6">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tv_confirm"
android:layout_alignBottom="@+id/tv_confirm"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center"
android:paddingLeft="30px"
android:text="@string/cancel"
android:textColor="@color/color_333333"
android:textSize="30px" />
<TextView
android:id="@+id/tv_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:paddingRight="30px"
android:text="@string/confirm"
android:textColor="@color/color_0075cc"
android:textSize="30px" />
</RelativeLayout>
<com.szzc.widget.view.spinnerwheel.WheelVerticalView
android:id="@+id/wheel"
android:layout_width="match_parent"
app:selectionDivider="@color/color_eeeeee"
android:layout_height="384px" />
</LinearLayout>
</RelativeLayout>
布局:item_select_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<com.zhy.autolayout.AutoRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="70px"
android:background="@drawable/selector_base_select_list_item"
android:textColor="@color/color_333333"
android:gravity="center"
android:layout_centerVertical="true"
android:textSize="32px"
android:text="@string/cancel"/>
<ImageView
android:id="@+id/iv_item_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:layout_marginRight="30px"
android:background="@mipmap/icon_select"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:background="@color/color_eeeeee" />
</com.zhy.autolayout.AutoRelativeLayout>
使用:
SelectDialog dialog = new SelectDialog(context, relationList, true, new SelectDialog.OnSelectListener() {
@Override
public void onSelect(int position) {
mFrtRelationWithProposer.setText(relationList.get(position));
}
});
dialog.show();