Android 自定义view 实现点击 展示下拉选项效果
思路:
使用PopWindow ,里面布局为listview,点击展示PopWindow,点击其他区域或者选择完成时 关闭PopWindow~
关键点:
1,实现头部视图(本文名为:pop_out_top_view)
2,实现底部下拉视图,使用popWindow,pop的布局使用listview(本文名为:pop_list_view)
3,实现listView的布局,使用adapter,结合listview的item(本文名为:pop_list_view_item),实现点击item回填数据与关闭PopWindow。
说明:
本Demo里数据展示是使用String~~~,实际开发回填的是文本,给后端需要传的应该是id,一般要是用对象~
public class DropDownListView extends LinearLayout {private TextView textView;private ImageView imageView;private PopupWindow popupWindow = null;private ArrayList<String> dataList = new ArrayList<String>();private View mView;public DropDownListView(Context context) {this(context, null);}public DropDownListView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public DropDownListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView();}public void initView() {String infServie = Context.LAYOUT_INFLATER_SERVICE;LayoutInflater layoutInflater;layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);//头部View视图mView = layoutInflater.inflate(R.layout.pop_out_top_view, this, true);//显示选择文本textView = (TextView) findViewById(R.id.text);textView.setTextColor(CFApplication.Companion.getMContext().getResources().getColor(R.color.se_aab9));imageView = (ImageView) findViewById(R.id.btn);//给整个头部设置点击事件this.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//※非必要。隐藏输入法,为了保证在输入法展示时,显示popwindow不会出问题InputMethodManager inputManager = (InputMethodManager) textView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);inputManager.hideSoftInputFromWindow(textView.getWindowToken(), 0);if (popupWindow == null) {showPopWindow();} else {closePopWindow();}}});}/*** 下拉展示的pop* 打开下拉列表弹窗*/private void showPopWindow() {// 加载popupWindow的布局文件String inflaterServie = Context.LAYOUT_INFLATER_SERVICE;LayoutInflater layoutInflater;layoutInflater = (LayoutInflater) getContext().getSystemService(inflaterServie);//popWindow里面的布局Listview视图View view = layoutInflater.inflate(R.layout.pop_list_view, null, false);ListView listView = (ListView) view.findViewById(R.id.pop_listView);listView.setAdapter(new DropDownListAdapter(getContext(), dataList));/*** 在new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)* 如果默认传入LayoutParams.WRAP_CONTENT,可能会显示的下拉view的宽度比头部要窄,* 为了保持一样宽度,使用this.getMeasuredWidth()*/int widthPop = LayoutParams.WRAP_CONTENT;if(this.getMeasuredWidth() >0){widthPop = this.getMeasuredWidth();}popupWindow = new PopupWindow(view, widthPop, LayoutParams.WRAP_CONTENT);
// popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_circle_info_bacacf_10));popupWindow.setOutsideTouchable(true);popupWindow.showAsDropDown(this);}/*** 关闭下拉列表弹窗*/private void closePopWindow() {popupWindow.dismiss();popupWindow = null;}/*** 设置数据** @param list*/public void setItemsData(ArrayList<String> list) {dataList = list;}/*** 设置默认文案(例如:请选择~)** @param text*/public void setDefaultText(String text) {textView.setText(text);//默认文案一般要与已选回填的文案颜色区分,所以在设置时设为相对暗色值textView.setTextColor(CFApplication.Companion.getMContext().getResources().getColor(R.color.se_aab9));}/*** 获取pop_out_top_view里 textview 的内容* @return*/public String getSelectText() {String selectText = textView.getText().toString();return selectText;}/*** 数据适配器** @author caizhiming*/class DropDownListAdapter extends BaseAdapter {Context mContext;ArrayList<String> mData;LayoutInflater inflater;public DropDownListAdapter(Context ctx, ArrayList<String> data) {mContext = ctx;mData = data;inflater = LayoutInflater.from(mContext);}@Overridepublic int getCount() {return mData.size();}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// 自定义视图ListItemView listItemView = null;if (convertView == null) {// 获取list_item布局文件的视图convertView = inflater.inflate(R.layout.pop_list_view_item, null);listItemView = new ListItemView();// 获取控件对象listItemView.tv = (TextView) convertView.findViewById(R.id.item_tv);listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);// 设置控件集到convertViewconvertView.setTag(listItemView);} else {listItemView = (ListItemView) convertView.getTag();}// 设置数据listItemView.tv.setText(mData.get(position));final String text = mData.get(position);listItemView.layout.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {textView.setText(text);textView.setTextColor(CFApplication.Companion.getMContext().getResources().getColor(R.color.se_0000));//关闭popWindowclosePopWindow();}});return convertView;}}private static class ListItemView {TextView tv;LinearLayout layout;}}
pop_out_top_view:
<?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:background="@drawable/bg_circle_login_e8eff2_10"android:orientation="horizontal"android:padding="15dp"><TextViewandroid:id="@+id/text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center_vertical"android:singleLine="true"android:textColor="@color/se_0000"android:textSize="12sp" /><ImageViewandroid:id="@+id/btn"android:layout_width="11dp"android:layout_height="7dp"android:layout_gravity="center_vertical"android:layout_marginLeft="15dp"android:layout_toRightOf="@+id/text"android:src="@drawable/icon_down" />
</LinearLayout>
pop_list_view:
<?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" ><ListViewandroid:id="@+id/pop_listView"android:layout_width="match_parent"android:layout_height="wrap_content"android:divider="#BACACF"android:dividerHeight="0.5dp"></ListView></LinearLayout>
pop_list_view_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout_container"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/item_tv"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:padding="15dp"android:textColor="@color/se_0000"android:textSize="12sp" /></LinearLayout>
使用时:
private val mSexList = arrayListOf<String>()
mSexList.add(resources.getString(R.string.man)) mSexList.add(resources.getString(R.string.woman))
sex_ddl.setDefaultText(resources.getString(R.string.unknow))
sex_ddl.setItemsData(mSexList)
//获取选中文案
sex_ddl.selectText~
Android 自定义view 实现点击 展示下拉选项效果相关推荐
- Android 自定义View(四)实现股票自选列表滑动效果
一.前言 Android 开发过程中自定义 View 真的是无处不在,随随便便一个 UI 效果,都会用到自定义 View.前面三篇文章已经讲过自定义 View 的一些案例效果,相关类和 API,还有事 ...
- html option ajax,Ajax实现简单下拉选项效果【推荐】
基本都是固定步骤!主要在JAVASCRIPT和PHP中的操作 1.HTML代码里就只有两个SELECT标签如下: 请选择 请选择 2.Javascript中进行创建选项和执行AJAX异步请求步骤如下 ...
- Android自定义View分享——仿微信朋友圈图片合并效果
写在前面 笔者近来在学习Android自定义View,收集了一些不算复杂但又"长得"还可以的自定义View效果实现,之前分享过两个效果:一个水平的进度条,一个圆形温度显示器,如果你 ...
- android自定义起止时间的时间刻度尺,Android 自定义View篇(六)实现时钟表盘效果...
前言 Android 自定义 View 是高级进阶不可或缺的内容,日常工作中,经常会遇到产品.UI 设计出花里胡哨的界面.当系统自带的控件不能满足开发需求时,就只能自己动手撸一个效果. 本文就带自定义 ...
- Android自定义View——仿ViVO X6 极速闪充动画效果
一直都在看自定义View,经过一个星期的坚持,基本上能够写出一些比较实用的控件效果了,今天天气太热,就待在家里玩手机,然后手机没电了,在充电的时候,看到了手机的充电动画,觉得挺酷,然后自己我就仔细的分 ...
- Android 自定义View实现文本水平方向的跑马灯效果
自定义View实现文本水平方向的跑马灯效果,可以设置文本相关属性及滚动速度,以及滚动方式 /*** Created by wyl on 2018/10/10.*/ public class Marqu ...
- Android自定义控制(五)仿新浪微博的下拉刷新
网上有很多很有名的开源框架,这里就来拉拉PullToRefresh这个框架,也就是我们平时用的下拉刷新啦,当然你问我这个有什么用啊?别人已经写好了,这里主要是学习以及练习,练习的次数多了,一切就顺其自 ...
- 【Android 自定义 View】--> 绘制时钟(表)效果
前言 本篇文章主要介绍使用自定义 View 来实现时钟效果,灵活地使用 Android 的 Canvas,Paint, Path 的 API 以及理清 Canvas 的 save 和 restore ...
- android实践项目一实现简单的验证码和spinner下拉选项效果
android练习demo1 Textview1 类似验证码的效果 点击之后生成一个随机的4位数,更换颜色. 代码如下 public class MainActivity extends Activi ...
最新文章
- 如何在Flutter(2021)中创建过滤器搜索列表视图
- spring项目使用redis分布式锁解决重复提交问题
- Ajax中文乱码问题解决方法(服务器端用servlet)
- Elasticsearch CURL命令
- mybatils多次查询问题
- 页面跳转多种方法(加传参)
- 论文浅尝 - ISWC2020 | KnowlyBERT: 知识图谱结合语言模型补全图谱查询
- jms架构_JMS架构和JMS API架构
- cf 0.2 版本发布,lua 开发的另一种美
- 计算机硬盘怎么优化,硬盘怎么优化_机械硬盘优化
- 获取微信所有聊天记录数据并通过Python制作词云图
- 黑马2021最新版 SpringCloud基础篇全技术栈导学(RabbitMQ+Docker+Redis+搜索+分布式)
- 期货突破(期货突破交易法)
- html 文字竖排效果
- Vue 事件绑定与解绑
- 破局“人工智能+大数据”产业痛点,保险极客百万医疗震撼出击
- vue首屏加载速度慢_Vue首屏加载速度优化如何提升80%?本文详解
- ffmpeg裁剪视频画面
- (转)用遗传算法优化BP神经网络的Matlab编程实例
- xxx牌JUC学习加油奥利给001初始篇章