实现滚动单选控件

 

效果图如下:

66b3f59b68a769b0311379518b2c0bb0cb6.jpg

添加依赖

   implementation 'com.weigan:loopView:0.1.2'

新建一个WheelViewDialog类, 代码如下

public class WheelViewDialog extends Dialog{

    private LoopView loopView;
    private Button btn_ok;
    private Button btn_cancle;

    private OnSelectedListener listener;

    private ArrayList<String> stringArrayList = new ArrayList<>();
    private int position = 0;

    public WheelViewDialog(Context context, ArrayList<String> arrayList) {
        super(context,R.style.alert_dialog);
        stringArrayList = arrayList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_view);
        setCanceledOnTouchOutside(false);//设置显示dialog后,触屏屏幕不会使dialog消失

        loopView =  findViewById(R.id.loopView);
        btn_ok =  findViewById(R.id.btn_ok);
        btn_cancle =  findViewById(R.id.btn_cancle);

        // 设置原始数据
        loopView.setItems(stringArrayList);
        loopView.setNotLoop();
        // 滚动监听
//        loopView.setListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(int index) {
//                if (!TextUtils.isEmpty( stringArrayList.get( index) ) ){
//                    Toast.makeText(getContext() ,"选择了" + stringArrayList.get( index) , Toast.LENGTH_SHORT).show();
//                    position = index;//存储选择的位序
//                }
//            }
//        });
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int i) {
                if (!TextUtils.isEmpty( stringArrayList.get( i) ) ){
                    Toast.makeText(getContext() ,"选择了" + stringArrayList.get( i) , Toast.LENGTH_SHORT).show();
                    position = i;//存储选择的位序
                }
            }
        });


        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    if (!TextUtils.isEmpty( stringArrayList.get( position) ) ){
                        listener.getData( stringArrayList.get( position) );
                    }
                }
                dismiss();
            }
        });

        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    public interface OnSelectedListener{
        void getData(String data);
    }
    public void setOnSelectedListener(OnSelectedListener listener){
        this.listener = listener;
    }
}

新建 dialog_view.xml文件, 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@drawable/dialog_background"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">



    <TextView
        android:text="请选择"
        android:textColor="#000000"
        android:textSize="20sp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <com.weigan.loopview.LoopView
        android:id="@+id/loopView"
        app:awv_centerTextColor = "@color/colorAccent"
        app:awv_outerTextColor = "@color/colorGray"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:awv_textsize="20" />

    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_cancle"
            android:text="取消"
            android:textSize="18sp"
            android:background="#00000000"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginRight="20dp"
            android:layout_height="40dp" />
        <Button
            android:id="@+id/btn_ok"
            android:text="确定"
            android:textSize="18sp"
            android:layout_marginLeft="20dp"
            android:background="#00000000"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="40dp" />
    </LinearLayout>
</LinearLayout>

背景图片是这个:

然后就可以在需要调用的地方调用了

比如点击一个按钮, 弹出dialog, 示例代码:

public class DialogActivity extends AppCompatActivity {

    private Button showDialogBtn;
    private ArrayList<String> stringArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        showDialogBtn = findViewById(R.id.act_show_dialog_btn);

        for (int i = 0; i < 15; i++) {
            stringArrayList.add("item " + i);
        }

        showDialogBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showDialog();
            }
        });

    }

    private void showDialog() {
        WheelViewDialog dialog = new WheelViewDialog( DialogActivity.this, stringArrayList );
        dialog.setOnSelectedListener(new WheelViewDialog.OnSelectedListener() {
            @Override
            public void getData(String data) {
                Toast.makeText(DialogActivity.this, ""+data, Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();

    }
}

ok完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值