Android实现底部弹出列表选择器
16lz
2022-06-20
在大多数app中都会有选择器,而现在比较流行的选择器除了仿ios的三级联动城市滚动选择以外,还有就是单个选择器,类似于android中的PopWindow弹出选择。
下面就是我在项目中使用到的一个关于运输方式选择的一个选择器,供大家,包括我家猫猫,嘿嘿,参考使用。
视图如下:
首先,在android中是没有这个控件的,所以我们要自定义视图,代码如下:
package com.fastlogistical.view;import android.app.Dialog;import android.content.Context;import android.graphics.Color;import android.view.Display;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.ScrollView;import android.widget.TextView;import com.fastlogistical.R;import java.util.ArrayList;import java.util.List;/** * 自定义弹出菜单视图类 */public class ActionSheetDialog { private Context context; private Dialog dialog; private TextView txt_title; private TextView txt_cancel; private LinearLayout lLayout_content; private ScrollView sLayout_content; private boolean showTitle = false; private List sheetItemList; private Display display; public ActionSheetDialog(Context context) { this.context = context; WindowManager windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); display = windowManager.getDefaultDisplay(); } public ActionSheetDialog builder() { View view = LayoutInflater.from(context).inflate( R.layout.view_actionsheet, null); view.setMinimumWidth(display.getWidth()); sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content); lLayout_content = (LinearLayout) view .findViewById(R.id.lLayout_content); txt_title = (TextView) view.findViewById(R.id.txt_title); txt_cancel = (TextView) view.findViewById(R.id.txt_cancel); txt_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog = new Dialog(context, R.style.ActionSheetDialogStyle); dialog.setContentView(view); Window dialogWindow = dialog.getWindow(); dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); lp.x = 0; lp.y = 0; dialogWindow.setAttributes(lp); return this; } public ActionSheetDialog setTitle(String title) { showTitle = true; txt_title.setVisibility(View.VISIBLE); txt_title.setText(title); return this; } public ActionSheetDialog setCancelable(boolean cancel) { dialog.setCancelable(cancel); return this; } public ActionSheetDialog setCanceledOnTouchOutside(boolean cancel) { dialog.setCanceledOnTouchOutside(cancel); return this; } public ActionSheetDialog addSheetItem(String strItem, SheetItemColor color, OnSheetItemClickListener listener) { if (sheetItemList == null) { sheetItemList = new ArrayList(); } sheetItemList.add(new SheetItem(strItem, color, listener)); return this; } private void setSheetItems() { if (sheetItemList == null || sheetItemList.size() <= 0) { return; } int size = sheetItemList.size(); if (size >= 7) { LayoutParams params = (LayoutParams) sLayout_content .getLayoutParams(); params.height = display.getHeight() / 2; sLayout_content.setLayoutParams(params); } for (int i = 1; i <= size; i++) { final int index = i; SheetItem sheetItem = sheetItemList.get(i - 1); String strItem = sheetItem.name; SheetItemColor color = sheetItem.color; final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener; TextView textView = new TextView(context); textView.setText(strItem); textView.setTextSize(18); textView.setGravity(Gravity.CENTER); if (size == 1) { if (showTitle) { textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector); } else { textView.setBackgroundResource(R.drawable.actionsheet_single_selector); } } else { if (showTitle) { if (i >= 1 && i < size) { textView.setBackgroundResource(R.drawable.actionsheet_middle_selector); } else { textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector); } } else { if (i == 1) { textView.setBackgroundResource(R.drawable.actionsheet_top_selector); } else if (i < size) { textView.setBackgroundResource(R.drawable.actionsheet_middle_selector); } else { textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector); } } } if (color == null) { textView.setTextColor(Color.parseColor(SheetItemColor.Blue .getName())); } else { textView.setTextColor(Color.parseColor(color.getName())); } float scale = context.getResources().getDisplayMetrics().density; int height = (int) (45 * scale + 0.5f); textView.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT, height)); textView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listener.onClick(index); dialog.dismiss(); } }); lLayout_content.addView(textView); } } public void show() { setSheetItems(); dialog.show(); } public interface OnSheetItemClickListener { void onClick(int which); } public class SheetItem { String name; OnSheetItemClickListener itemClickListener; SheetItemColor color; public SheetItem(String name, SheetItemColor color, OnSheetItemClickListener itemClickListener) { this.name = name; this.color = color; this.itemClickListener = itemClickListener; } } public enum SheetItemColor { Blue("#037BFF"), Red("#FD4A2E"); private String name; private SheetItemColor(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }}
自定义视图布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp" > <TextView android:id="@+id/txt_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/actionsheet_top_normal" android:gravity="center" android:minHeight="45dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:textColor="@color/actionsheet_gray" android:textSize="13sp" android:visibility="gone" /> <ScrollView android:id="@+id/sLayout_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:fadingEdge="none" > <LinearLayout android:id="@+id/lLayout_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > LinearLayout> ScrollView> <TextView android:id="@+id/txt_cancel" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="8dp" android:background="@drawable/actionsheet_single_selector" android:gravity="center" android:text="取消" android:textColor="@color/actionsheet_blue" android:textSize="18sp" android:textStyle="bold" />LinearLayout>
每一个item的选择器:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/actionsheet_bottom_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/actionsheet_bottom_normal"/>selector>
那么,当弹出的视图完成后,就来看下在Activity中怎样调用这个视图,并添加相关内容:
package com.fastlogistical.activity;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.fastlogistical.R;import com.fastlogistical.view.ActionSheetDialog;/** * * Company: xxx * Author : wujie * Time : 2016/12/19 8:56 * Usage : * desc : * other : *
*/public class PopActivity extends BaseActivity{ private Button pop_btn; @Override protected void initView() { setContentView(R.layout.pop_activity); pop_btn= (Button) findViewById(R.id.pop_btn); } @Override protected void initListener() { pop_btn.setOnClickListener(this); } @Override protected void initData() { } @Override public void onClick(View v) {switch (v.getId()){ case R.id.pop_btn: new ActionSheetDialog(PopActivity.this) .builder() .setTitle("选择运输方式") .setCancelable(true) .setCanceledOnTouchOutside(true) .addSheetItem("汽运", ActionSheetDialog.SheetItemColor.Blue , new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { //填写事件 Toast.makeText(PopActivity.this, "选择了汽运", Toast.LENGTH_SHORT).show(); } }) .addSheetItem("铁路", ActionSheetDialog.SheetItemColor.Blue , new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { //填写事件 Toast.makeText(PopActivity.this, "选择了铁路", Toast.LENGTH_SHORT).show(); } }) .addSheetItem("水路", ActionSheetDialog.SheetItemColor.Blue , new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { //填写事件 Toast.makeText(PopActivity.this, "选择了水路", Toast.LENGTH_SHORT).show(); } }) .addSheetItem("航运", ActionSheetDialog.SheetItemColor.Blue , new ActionSheetDialog.OnSheetItemClickListener() { @Override public void onClick(int which) { //填写事件 Toast.makeText(PopActivity.this, "选择了航运", Toast.LENGTH_SHORT).show(); } }).show();} }}
注意:我在Activity中是继承的我自己封装的BaseActivity,大家在使用时可以用自己写的Activity,只要将关键的代码放入即可。
更多相关文章
- Android4.0 input touch解析
- Android自定义底部显示对话框
- Android控件开发之四----ListView(3)
- android 开发使用 kotlin 进行点击事件监听和界面跳转,直接传也方
- Android(安卓)窗口Flags详解
- Android: 用Instrumentation类发送鼠标或按键事件
- android 按钮的四种点击事件
- android 开发环境搭建
- Android使用EventBus传递事件