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 ArrayList
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(); } }); }}
ListPopupWindowAdapter如下:
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 ArrayList
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; }}
main.xml如下:

item.xml如下:

转载地址:https://it9527.blog.csdn.net/article/details/23662215 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ListPopupWindow使用完整示例(一)——系统自带ListPopupWindow
下一篇:Android文本输入框(EditText)切换密码的显示与隐藏

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月27日 14时45分24秒