首先添加依赖:
implementation 'com.brucetoo.pickview:library:1.2.3'
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="276dp"
android:background="@color/white"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/bar_height">
<TextView
android:id="@+id/sure"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="@dimen/padding16"
android:paddingRight="@dimen/padding16"
android:text="确定"
android:textColor="#2995F1"
android:textSize="@dimen/d15"
android:textStyle="bold" />
<TextView
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:paddingLeft="@dimen/padding16"
android:paddingRight="@dimen/padding16"
android:text="取消"
android:textColor="@color/textGray"
android:textSize="@dimen/d15"
android:textStyle="bold" />
</RelativeLayout>
<com.bruce.pickerview.LoopView
android:id="@+id/equipLoopView"
android:layout_width="match_parent"
android:layout_height="231dp"
app:centerTextColor="@color/textBlack"
app:drawItemCount="7"
app:lineColor="@color/lineColor"
app:textSize="@dimen/d18"
app:topBottomTextColor="@color/textGray" />
</LinearLayout>
public void OnSwitchClick() {
View bottomDialog = LayoutInflater.from(this).inflate(R.layout.bottom_select_smart_equip, null);
TextView sure = bottomDialog.findViewById(R.id.sure);
TextView cancel = bottomDialog.findViewById(R.id.cancel);
LoopView equipLoopView = bottomDialog.findViewById(R.id.equipLoopView);
//设置默认的选中哪一个
equipLoopView.setInitPosition(0);
equipLoopView.setCanLoop(false);
mLoopStayPosition=0;
//选中的条目的监听
equipLoopView.setLoopListener(new LoopScrollListener() {
@Override
public void onItemSelect(int item) {
mLoopStayPosition = item;
}
});
List<String> list = new ArrayList<>();
for (SmartEquipDataBean.DataBean dataBean :
mSmartEquipDataList) {
int cabinetNumber = dataBean.getCabinet_number();
list.add(cabinetNumber + "号柜子");
}
equipLoopView.setDataList(list);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismissBottomDialog();
}
});
sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
KLog.e("mLoopStayPosition:"+mLoopStayPosition);
equipSelectedId = mSmartEquipDataList.get(mLoopStayPosition).getId();
mEquipDetailsAdapter.setCounterName(mSmartEquipDataList.get(mLoopStayPosition).getCabinet_number());
getEquipCountersPositionData(equipSelectedId);
dismissBottomDialog();
}
});
showBottomDialog(bottomDialog);
}