PopupWindow学习
来源:互联网 发布:qq飞车噬魂剃刀数据 编辑:程序博客网 时间:2024/05/19 17:48
显示效果
Java代码
MainActivity
package com.example.administrator.textexample;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.widget.GridView;import android.widget.PopupWindow;import java.util.List;import java.util.Map;public class MySelectpoupwindow extends PopupWindow { private MySelectAdapter heroAdapter; public MySelectpoupwindow(Context context, List<Map<String, String>> list, final HeroSelectListener eventListener) { // 下拉框的宽高 this.setWidth(WindowManager.LayoutParams.MATCH_PARENT); this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); // 外边界面是否可以点击 this.setOutsideTouchable(true); View view = LayoutInflater.from(context).inflate(R.layout.my_select_layout, null); setContentView(view); heroAdapter = new MySelectAdapter(context, list); heroAdapter.setListener(new MySelectAdapter.ItemListener() { @Override public void itemListener(int position) { // 回调 eventListener.click(position); heroAdapter.setClickitem(position); } }); GridView selectGrid = (GridView) view.findViewById(R.id.hero_select_grid); selectGrid.setAdapter(heroAdapter); if(list != null && list.size() > 0){ heroAdapter.setClickitem(0); } // 外边界面点击后关闭下拉框 View overlayView = view.findViewById(R.id.hero_select_overlay); overlayView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } public interface HeroSelectListener { void click(int i); }}
MySelectpoupwindow
package com.example.administrator.textexample;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.WindowManager;import android.widget.GridView;import android.widget.PopupWindow;import java.util.List;import java.util.Map;public class MySelectpoupwindow extends PopupWindow { private MySelectAdapter heroAdapter; public MySelectpoupwindow(Context context, List<Map<String, String>> list, final HeroSelectListener eventListener) { // 下拉框的宽高 this.setWidth(WindowManager.LayoutParams.MATCH_PARENT); this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); // 外边界面是否可以点击 this.setOutsideTouchable(true); View view = LayoutInflater.from(context).inflate(R.layout.my_select_layout, null); setContentView(view); heroAdapter = new MySelectAdapter(context, list); heroAdapter.setListener(new MySelectAdapter.ItemListener() { @Override public void itemListener(int position) { // 回调 eventListener.click(position); heroAdapter.setClickitem(position); } }); GridView selectGrid = (GridView) view.findViewById(R.id.hero_select_grid); selectGrid.setAdapter(heroAdapter); if(list != null && list.size() > 0){ heroAdapter.setClickitem(0); } // 外边界面点击后关闭下拉框 View overlayView = view.findViewById(R.id.hero_select_overlay); overlayView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } public interface HeroSelectListener { void click(int i); }}
MySelectAdapter
package com.example.administrator.textexample;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import java.util.List;import java.util.Map;public class MySelectAdapter extends BaseAdapter { private Context context; private List<Map<String, String>> mlist; private int clickitem = -1; public MySelectAdapter(Context context, List<Map<String, String>> list) { this.context = context; this.mlist = list; } @Override public int getCount() { return mlist.size(); } public void setClickitem(int position) { this.clickitem = position; } @Override public Object getItem(int position) { return mlist.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { viewHolder vh; if (null == convertView) { vh = new viewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.my_select_item, parent, false); vh.tv = (TextView) convertView.findViewById(R.id.heroselect_tv); convertView.setTag(vh); } else { vh = (viewHolder) convertView.getTag(); } String name = mlist.get(position).get("name"); vh.tv.setText(name); final int pos = position; if (listener != null) { vh.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 回调 listener.itemListener(pos); } }); } return convertView; } public class viewHolder { TextView tv; } public interface ItemListener { void itemListener(int position); } public ItemListener listener; public void setListener(ItemListener listener) { this.listener = listener; }}
xml
activity_main
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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" tools:context="com.example.administrator.textexample.MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="下拉窗口" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30dp" android:layout_above="@+id/showText" android:layout_marginBottom="20dp" android:text="下拉框点击的内容"/> <TextView android:id="@+id/showText" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30dp" android:layout_centerInParent="true"/></RelativeLayout>
my_select_layout
<?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"> <GridView android:id="@+id/hero_select_grid" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="9dp" android:numColumns="4" android:paddingBottom="10dp" android:paddingLeft="9dp" android:paddingRight="9dp" android:paddingTop="10dp" android:verticalSpacing="9dp"> </GridView> <View android:id="@+id/hero_select_overlay" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
my_select_item
<?xml version="1.0" encoding="utf-8"?><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"> <TextView android:id="@+id/heroselect_tv" android:gravity = "center" android:textSize = "15dp" android:layout_width="76dp" android:layout_height="31dp" tools:text="射手" /></LinearLayout>
0 0
- PopupWindow学习
- PopupWindow学习
- PopupWindow学习
- PopupWindow学习总结
- 学习笔记 popupwindow
- PopupWindow学习总结
- PopupWindow学习大全
- android学习之popupWindow
- Android学习之PopupWindow
- PopupWindow学习整理
- PopupWindow学习笔记
- Android学习之PopupWindow
- popupwindow的学习
- Android学习——PopupWindow
- 【学习笔记】Android PopupWindow使用
- Android UI:PopupWindow基本学习
- android基础学习之popupwindow
- popupwindow
- 跟我一起写 Makefile(十)
- 设计模式C++实现(3)——适配器模式
- Vue最早版本迁移到2.0
- 【C#】使用TcpListener和TcpClient实现简单通信
- Listview中的分割线、分割线的颜色以及分割线高度的改变 Button或者ImageButton的背景设为透明或者半透明
- PopupWindow学习
- sqli-labs学习教程(一)
- Manacher马拉车算法总结
- synchronized关键字(四)
- Python 之 self
- 跟我一起写 Makefile(十一)
- Ubuntu14.04 OpenCV编译安装与测试
- Linux架站-DNS
- Mybatis与spring项目中遇到的奇怪的问题:ClassNotFoundException: com.mysql.jdbc.Driver