先上圖看看效果
效果大概就是圖上面的了,自定義一個對話框,然後可以滑動選擇資料。
在這裡我使用了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>