ListPopupWindow使用完整示例(二)——自定义ListPopupWindow
发布日期:2021-06-30 11:14:53
浏览次数:2
分类:技术文章
本文共 3730 字,大约阅读时间需要 12 分钟。
MainActivity如下:
package cc.wy;import java.util.ArrayList;import android.app.Activity;import android.app.ActionBar.LayoutParams;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.Button;import android.widget.ListPopupWindow;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;/** * Demo描述: * ListPopupWindow使用完整示例(二)——自定义ListPopupWindow * * 参考资料: * 1 http://blog.csdn.net/rambomatrix/article/details/23525379 * 2 http://blog.csdn.net/jsnrwzm/article/details/14408835 * Thank you very much * */public class MainActivity extends Activity { private Context mContext; private Button mButton; private ArrayListListPopupWindowAdapter如下:mArrayList; private ListPopupWindow mListPopupWindow; private ListPopupWindowAdapter mListPopupWindowAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mContext=this; mArrayList=new ArrayList (); mArrayList.add("第一个子项"); mArrayList.add("第二个子项"); mArrayList.add("第三个子项"); mListPopupWindow=new ListPopupWindow(mContext); //自定义Adapter mListPopupWindowAdapter=new ListPopupWindowAdapter(mArrayList, mContext); mListPopupWindow.setAdapter(mListPopupWindowAdapter); //mListPopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher)); mListPopupWindow.setWidth(200); mListPopupWindow.setHeight(LayoutParams.WRAP_CONTENT); mListPopupWindow.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int position,long arg3) { Toast.makeText(mContext, "点击了"+mArrayList.get(position), Toast.LENGTH_SHORT).show(); } }); mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //指定anchor mListPopupWindow.setAnchorView(v); mListPopupWindow.show(); } }); }}
package cc.wy;import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class ListPopupWindowAdapter extends BaseAdapter { private ArrayListmain.xml如下:mArrayList; private Context mContext; public ListPopupWindowAdapter(ArrayList list, Context context) { super(); this.mArrayList = list; this.mContext = context; } @Override public int getCount() { if (mArrayList == null) { return 0; } else { return this.mArrayList.size(); } } @Override public Object getItem(int position) { if (mArrayList == null) { return null; } else { return this.mArrayList.get(position); } } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null, false); holder.itemTextView = (TextView) convertView.findViewById(R.id.itemTextView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (this.mArrayList != null) { final String itemName = this.mArrayList.get(position); if (holder.itemTextView != null) { holder.itemTextView.setText(itemName); } } return convertView; } private class ViewHolder { TextView itemTextView; }}
item.xml如下:
转载地址:https://it9527.blog.csdn.net/article/details/23662215 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月27日 14时45分24秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ACM路上的一大失误
2021-07-03
CodeForces 628A Tennis Tournament
2021-07-03
HDOJ2049 不容易系列之(4)——考新郎
2021-07-03
CodeForces 248B - Chilly Willy - 找规律
2021-07-03
C与C++中的运算符和常用函数的使用
2021-07-03
POJ 3421 X-factor Chains(数论)(筛法)()
2021-07-03
UVA 1339 - Ancient Cipher(题意)(排序)
2021-07-03
HDOJ 1081 To The Max(dp)(状态压缩)未解决
2021-07-03
UVA 10375 - Choose and divide(数论)(组合数学)
2021-07-03
Codeforces 比赛代码记录及心得
2021-07-03
POJ初级分类 枚举专题 poj1753 poj2965
2021-07-03
暴力求解法 算法竞赛入门经典2读书笔记 (附POJ 1321)
2021-07-03
重积分和线面积分总结
2021-07-03
FZU 2147(找规律)
2021-07-03
POJ 2231 Moo Volume(规律)
2021-07-03
2016ACM省赛总结
2021-07-03