Android使用popueWindow自定义Spinner
Posted Jason_Lee155
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android使用popueWindow自定义Spinner相关的知识,希望对你有一定的参考价值。
其实就是很简单的控件自定义,只不过这两天项目用到,还是到处搜改了下,现简单记录。方便有需要的时候修改为自己使用:
需求
一个TextView或者Button点击的时候要弹出一些下拉选择框,可能TextView/Button还要变色或者图标变化:
实现思路
点击Textview的时候弹出一个popupWindow(后文简写),popWind布局是一个RelativeLayout或者LinearLayout,里面一个ListView/Recyclerview,它的item自己根据需要定义。
简单代码
1,MainActivity.java 程序入口,初始化数据,初始化自定义popWind,TextView点击之后显示popWind,处理ListView的点击事件:
/**
* 主Activity 用来实现popupwindow
* @author ansen
*/
public class MainActivity extends Activity
private SpinerPopWindow<String> mSpinerPopWindow;
private List<String> list;
private TextView tvValue;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
tvValue = (TextView) findViewById(R.id.tv_value);
tvValue.setOnClickListener(clickListener);
mSpinerPopWindow = new SpinerPopWindow<String>(this, list);
mSpinerPopWindow.setOnItemListener(dismissListener);
mSpinerPopWindow.setOnDismissListener(dismissListener);
/**
* 监听popupwindow取消
*/
private OnDismissListener dismissListener=new OnDismissListener()
@Override
public void onDismiss()
setTextImage(R.drawable.icon_down);
;
/**
* popupwindow显示的ListView的item点击事件
*/
private OnItemClickListener itemClickListener = new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
mSpinerPopWindow.dismiss();
tvValue.setText(list.get(position));
Toast.makeText(MainActivity.this, "点击了:" + list.get(position),Toast.LENGTH_LONG).show();
;
/**
* 显示PopupWindow
*/
private OnClickListener clickListener = new OnClickListener()
@Override
public void onClick(View v)
switch (v.getId())
case R.id.tv_value:
mSpinerPopWindow.setWidth(tvValue.getWidth());
mSpinerPopWindow.showAsDropDown(tvValue);
setTextImage(R.drawable.icon_up);
break;
;
/**
* 初始化数据
*/
private void initData()
list = new ArrayList<String>();
for (int i = 0; i < 5; i++)
list.add("test:" + i);
/**
* 给TextView右边设置图片
* @param resId
*/
private void setTextImage(int resId)
Drawable drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());// 必须设置图片大小,否则不显示
tvValue.setCompoundDrawables(null, null, drawable, null);
2,activity_main.xml 这个文件也没啥好说的,就是两个TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape_help_category_tv_bg"
android:drawableRight="@drawable/icon_down"
android:padding="10dp"
android:textColor="@color/content_color"
android:text="请选择父类别"
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/shape_help_category_tv_bg"
android:drawableRight="@drawable/icon_down"
android:textColor="@color/content_color"
android:padding="10dp"
android:text="请选择子类别"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
3,SpinerPopWindow.java 自定义Popupwindow类,初始化popWind显示的布局,以及一些参数,并且给listView设置适配器:
* 自定义PopupWindow 主要用来显示ListView
* @author Jason
* @create time 2022-3
*/
public class SpinerPopWindow<T> extends PopupWindow
private LayoutInflater inflater;
private ListView mListView;
private List<T> list;
private MyAdapter mAdapter;
public SpinerPopWindow(Context context,List<T> list)
super(context);
inflater=LayoutInflater.from(context);
this.list=list;
View view = inflater.inflate(R.layout.spiner_window_layout, null);
setContentView(view);
//大小怎么设置,或者不设置,根据自己需要
setWidth(LayoutParams.WRAP_CONTENT);
setHeight(LayoutParams.WRAP_CONTENT);
setFocusable(true);
ColorDrawable dw = new ColorDrawable(0x00);
setBackgroundDrawable(dw);
mListView = (ListView) view.findViewById(R.id.listview);
mAdapter=new MyAdapter()
mListView.setAdapter(mAdapter);
private void SetOnItemListener(OnItemClickListener clickListener)
if(mListView != null)
mListView.setOnItemClickListener(clickListener);
private class MyAdapter extends BaseAdapter
@Override
public int getCount()
return list.size();
@Override
public Object getItem(int position)
return list.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder holder=null;
if(convertView==null)
holder=new ViewHolder();
// 这个spiner_item_layout就是根据自己需要定义,比如仅一个textview
convertView=inflater.inflate(R.layout.spiner_item_layout, null);
holder.tvName=(TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(holder);
else
holder=(ViewHolder) convertView.getTag();
holder.tvName.setText(getItem(position).toString());
return convertView;
private class ViewHolder
private TextView tvName;
4,spiner_window_layout.xml,popWind显示的布局文件,里面就一个ListView控件:
<?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:layout_alignParentRight="true"
android:orientation="vertical">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_popupwindow_list_bg"
android:scrollbars="none" >
</ListView>
</LinearLayout>
各部分代码根据自身需要修改,比如listView-item,或者点击事件等。
以上是关于Android使用popueWindow自定义Spinner的主要内容,如果未能解决你的问题,请参考以下文章
“Android Utils“ 实现TextView 区域自定义点击
Spring SAML - 如何在 SP HTTP 请求上添加自定义字段?