用PopupWindow实现下拉列表,并再次熟悉一些其他基础知识点。
来源:互联网 发布:网络写手平台 编辑:程序博客网 时间:2024/05/21 06:18
本文仅供菜鸟交流观看,大神请绕路。
这里首先要明确一下需求,并不单单是为了用一个PopupWindow来实现一个下拉列表
需求1:PopupWindow实现下来列表
需求2:点击选中列表中item中时,item中显示字体,再次点击字体隐藏。
需求3:所有选中的item,不论是什么顺序选择的,最后都按position(用的listview)的大小,从小到大排列然后显示在textview里。
其实需求写明白后,就知道如何去实现这些功能了。 话不多说 贴出代码.
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView textView; private ListView listView; private PopupWindow popupWindow; private MyAdapter myAdapter; private List<BeanVo> data; // private List<String> textList = new ArrayList<String>(); private Map<Integer, String> map = new HashMap<Integer, String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); } private void initUI() { textView = (TextView) findViewById(R.id.tv); textView.setOnClickListener(this); } private void initSeletPopup() { listView = new ListView(this); testData(); myAdapter = new MyAdapter(this, data); listView.setAdapter(myAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { BeanVo beanVo = data.get(position); if (beanVo.isShow()) { map.remove(position); beanVo.setShow(false); } else { map.put(position, data.get(position).getName()); beanVo.setShow(true); } myAdapter.notifyDataSetChanged(); } }); popupWindow = new PopupWindow(listView, textView.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true); Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg_corner); popupWindow.setBackgroundDrawable(drawable); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { if (map.size() > 0) { String tmp = ""; int[] ints = sortPosition(); for (int i = 0; i < map.size(); i++) { tmp = tmp + map.get(ints[i]); } textView.setText(tmp); } popupWindow.dismiss(); } }); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.tv: map.clear(); initSeletPopup(); if (popupWindow != null && !popupWindow.isShowing()) { popupWindow.showAsDropDown(textView, 0, 10); } break; } } private void testData() { data = new ArrayList<>(); for (int i = 0; i < 20; i++) { BeanVo beanVo = new BeanVo(); beanVo.setName("数据" + i); beanVo.setShow(false); data.add(beanVo); } } private int[] sortPosition() { Set<Integer> str = map.keySet(); Object[] tt = str.toArray(); int N = map.size(); int[] arr = new int[N]; int a = 0; for (Object oo : tt) { if (oo instanceof Integer) { arr[a] = (Integer) oo; } a++; }for (int i = arr.length - 1; i > 0; i--) { for (int j = 1; j <= i; j++) { if (arr[j - 1] > arr[j]) { int tmp = arr[j - 1]; arr[j - 1] = arr[j]; arr[j] = tmp; } } } return arr; }}public class MyAdapter extends BaseAdapter { private List<BeanVo> mData; private LayoutInflater layoutInflater; private Context mContext; HashMap<Integer, View> hashMap = new HashMap<Integer, View>(); public MyAdapter(Context context, List data) { this.mContext = context; this.mData = data; this.layoutInflater = LayoutInflater.from(context); } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup viewGroup) { ViewHolder viewHolder = null;// if (hashMap.get(position)==null) {//如果变化的对象没有绑定position,该方法可以防止复用 if (convertView == null) { viewHolder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.popup_text_item, null); viewHolder.tv_data = convertView.findViewById(R.id.tv_data); viewHolder.select_tv = convertView.findViewById(R.id.select_tv); convertView.setTag(viewHolder);// hashMap.put(position,convertView); } else {// convertView=hashMap.get(position); viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.tv_data.setText(mData.get(position).getName()); if (mData.get(position).isShow()) { viewHolder.select_tv.setVisibility(View.VISIBLE); } else { viewHolder.select_tv.setVisibility(View.GONE); } return convertView; } public class ViewHolder { public TextView tv_data; public TextView select_tv; }}public class BeanVo { public String name; public boolean show; public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isShow() { return show; } public void setShow(boolean show) { this.show = show; }}由于代码比较简单,里面就没有什么备注了,一看就懂了。
阅读全文
0 0
- 用PopupWindow实现下拉列表,并再次熟悉一些其他基础知识点。
- 用PopupWindow实现下拉列表
- 使用PopupWindow实现的自定义下拉列表
- Android 使用PopupWindow实现下拉列表
- 自定义View,实现PopupWindow下拉列表
- RecyclerView+PopupWindow实现下拉列表选择效果
- 使用PopupWindow实现Spinner的下拉列表
- 用PopupWindow实现下拉菜单
- Android 下拉列表Popupwindow
- PopupWindow下拉列表
- 关于下拉列表的实现(spinner和PopupWindow+listview)
- Popupwindow边缘半透明的实现及一些其他解析
- Android 下拉列表之PopupWindow
- Android自定义下拉列表PopupWindow
- PopupWindow制作Spinner下拉列表
- Android 下拉列表之PopupWindow
- 安卓下拉列表popupWindow
- Popupwindow实现下拉菜单
- POJ 3264 Balanced Lineup
- ros如何及时清除障碍物层或者超声波层的的消息 clear_costmap_recovery 代码详解
- httpclient与springmvc整合
- 嵌入式应用之-初始化部分
- servlet之文件的上传下载(一)
- 用PopupWindow实现下拉列表,并再次熟悉一些其他基础知识点。
- 最小公倍数和最大公约数
- 螺旋矩阵 II -LintCode
- QString::arg()//用字符串变量参数依次替代字符串中最小数值
- 内容提供者——查询联系人
- 驱寒药不妨试试这几种食物
- 链表中p->next=head->next;语句解析
- Discuz!X 前台任意文件删除漏洞复现测试
- 设计心理学学习