先上图看看效果
效果大概就是图上面的了,自定义一个对话框,然后可以滑动选择数据。
在这里我使用了DiscreteScrollView框架,这个框架给出的demo是在Activity中这样显示,但既然在Activity都可以实现此效果,那在dialog中自然也可以实现,给出框架地址,大家可以去看一下。
框架地址
下面来介绍如何实现此效果。
1.添加依赖:
implementation 'com.yarolegovich:discrete-scrollview:1.4.9'
2.自定义dialog布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/backgrounddialogshape">
<TextView
android:layout_alignParentTop="true"
android:id="@+id/tv_show_cjfq"
android:text=""
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:textColor="#328dff"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<com.yarolegovich.discretescrollview.DiscreteScrollView
android:layout_below="@+id/tv_show_cjfq"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:id="@+id/picker"
android:layout_width="match_parent"
android:layout_height="100dp"
app:dsv_orientation="vertical" />
<LinearLayout
android:id="@+id/ll_address"
android:layout_below="@+id/picker"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<TextView
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:text="确定"
android:background="@color/top"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@color/white"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eee"/>
<TextView
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#666"/>
</LinearLayout>
</RelativeLayout>
3.dialog相关操作
//首先自定义一些变量
private DiscreteScrollView picker;
private InfiniteScrollAdapter infiniteAdapter;
private List<ItemBean> data;
private String name;
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.3f;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutInflater inflater = LayoutInflater.from(this);
View inflate = inflater.inflate(R.layout.dialog_address_partition_live, null);
final AlertDialog.Builder dialog = new AlertDialog.Builder(this, R.style.MyDialog).setCancelable(false);
dialog.setView(inflate);
final AlertDialog alertDialog = dialog.create();
alertDialog.show();
data = getData();
picker = (DiscreteScrollView)inflate.findViewById(R.id.picker);
picker.setOrientation(DSVOrientation.VERTICAL); //设置滑动的水平方向
picker.addOnItemChangedListener(this); //设置监听,获取获得到的哪一条数据
infiniteAdapter = InfiniteScrollAdapter.wrap(new AddressAdapter(data)); //适配器
picker.setAdapter(infiniteAdapter); //设置适配器
picker.setItemTransitionTimeMillis(150); //设置动画时间
picker.setItemTransformer(new ScaleTransformer.Builder()
.setMinScale(0.6f) //设置上下数据的与中间数据的字体大小
.build());
inflate.findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1.0f; //值越大越透明,即不暗
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//确定按钮,这里可以进行确定后的一些操作,比如跳转界面,将数据传递到下一个界面
}
});
//取消按钮
inflate.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1.0f; //值越大越透明,即不暗
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
});
break;
}
}
@Override
public void onCurrentItemChanged(@Nullable RecyclerView.ViewHolder viewHolder, int position) {
int positionInDataSet = infiniteAdapter.getRealPosition(position);
ItemBean bean = data.get(positionInDataSet);
name = bean.getName();
Log.e(TAG,"当前选中的数据是:" + name);
}
private List<ItemBean> getData() {
return Arrays.asList(
new ItemBean( "Everyday Candle"),
new ItemBean("Small Porcelain Bowl"),
new ItemBean( "Favourite Board"),
new ItemBean( "Earthenware Bowl"),
new ItemBean( "Porcelain Dessert Plate"),
new ItemBean( "Detailed Rolling Pin"));
}
适配器代码:
public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {
private List<ItemBean> data;
public AddressAdapter(List<ItemBean> data) {
this.data = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_address, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ItemBean itemBean = data.get(position);
String name = itemBean.getName();
holder.tv_show_address.setText(String.valueOf(name));
}
@Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView tv_show_address;
public ViewHolder(View itemView) {
super(itemView);
tv_show_address = (TextView) itemView.findViewById(R.id.tv_show_address);
}
}
}
适配器布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="30dp">
<TextView
android:id="@+id/tv_show_address"
android:textColor="@color/top"
android:textSize="20dp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RelativeLayout>
ItemBean:
public class ItemBean {
private final String name;
public ItemBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
有些不懂的地方可以去看看框架里面的解释,实在不懂的可以在下面留言或者私聊我。
附上xml文件中其它代码:
backgrounddialogshape
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#ffffff" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="10dip" />
</shape>
MyDialog
<style name="MyDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>