popupwindow中动态添加adapter

主方法中显示pupubwindow和添加adapter:

 LayoutInflater inflater = LayoutInflater.from(MainActivity.this) ;
                    View view = inflater.inflate(R.layout.popupwindow_search_file, null);
                    SearchAdapter searchAdapter = new SearchAdapter(searchItems,MainActivity.this);
                    ListView mListView = (ListView) view.findViewById(R.id.lv_search_file);
                    mListView.setAdapter(searchAdapter);
                    final PopupWindow mPopupWindow = new PopupWindow(view,WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
                    mPopupWindow.setFocusable(true);
                    mPopupWindow.setBackgroundDrawable(new ColorDrawable(0));
                    mPopupWindow.setOutsideTouchable(true);
                    mPopupWindow.showAsDropDown(MainActivity.this.findViewById(R.id.sv_title),50, -28);
                    mPopupWindow.update();
                    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            if(mPopupWindow.isShowing()){
                                String searchPath = searchItems.get(position).getPath();
                                int countPage = 0;
                                for(int i = 0; i < searchPath.length();i++){
                                    if(searchPath.charAt(i) == '/'){
                                        pageIndex = ++countPage;
                                    }
                                }
                                currentpageUrl = searchPath;
                                requestPage(searchPath);
                                mPopupWindow.dismiss();
                            }
                        }
                    });
adapter中的代码:

public class SearchAdapter extends BaseAdapter {
    private List<SearchItem> items;
    private Context context;

    public SearchAdapter(List<SearchItem> items, Context context) {
        this.items = items;
        this.context = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.search_item, null);
            viewHolder.name = (TextView) convertView.findViewById(R.id.tv_search_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.name.setText(items.get(position).getPath() + "/" + items.get(position).getName());


        return convertView;


    }


    class ViewHolder {
        TextView name;
    }
}
adapter中R.layout.search_item中的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_search_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/dp8"
        android:paddingBottom="@dimen/dp8"
        android:paddingLeft="@dimen/dp15"
        android:text="name"/>

</LinearLayout>
popupwindow中的R.layout.popupwindow_search_file的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv_search_file"
        android:background="@color/white"
        android:layout_width="260dp"
        android:layout_height="160dp"></ListView>

</LinearLayout>

注释:

设置进场动画:
mPop.setAnimationStyle(R.style.AnimationPreview);//设置动画样式

mPop.setOutsideTouchable(true);//这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。当然这里很明显只能在Touchable下才能使用。

当有mPop.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。

mPop.setFocusable(true);
需要顺利让PopUpWindow dimiss(即点击PopuWindow之外的地方此或者back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在popuWindow.showAsDropDown(v);或者其它的显示PopuWindow方法之前设置它的背景不为空:

mPop.setBackgroundDrawable(new ColorDrawable(0));

mPop.showAsDropDown(anchor, 0, 0);//设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量

mPop.showAtLocation(findViewById(R.id.parent), Gravity.LEFT, 0, -90);(以某个View为参考),表示弹出窗口以parent组件为参考,位于左侧,偏移-90。

mPop.setOnDismissListenerd(new PopupWindow.OnDismissListener(){})//设置窗口消失事件



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PopupWindow是Android非常常见的一种弹窗控件,可以在屏幕上方或下方弹出一个窗口,常用于菜单、提示、选择等场景。 在Service使用PopupWindow需要注意以下几点: 1. 需要在Service创建一个WindowManager对象,用于在屏幕上添加PopupWindow。 2. 在创建PopupWindow时,需要传入一个View对象作为弹出窗口的内容,可以通过LayoutInflater.from(context).inflate()方法获取。 3. 创建PopupWindow时需要指定宽度、高度、动画等属性。 4. 在PopupWindow弹出之后,需要设置其背景色为透明,否则会遮挡住其他View。 以下是一个在Service使用PopupWindow的示例代码: ``` public class MyService extends Service { private WindowManager mWindowManager; private PopupWindow mPopupWindow; @Override public void onCreate() { super.onCreate(); mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); } @Override public int onStartCommand(Intent intent, int flags, int startId) { showPopupWindow(); return super.onStartCommand(intent, flags, startId); } private void showPopupWindow() { // 通过LayoutInflater获取PopupWindow的内容View View contentView = LayoutInflater.from(this).inflate(R.layout.popup_content, null); // 创建PopupWindow,指定宽度、高度、动画等属性 mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); mPopupWindow.setAnimationStyle(R.style.PopupWindowAnimation); mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); mPopupWindow.setFocusable(true); // 在屏幕上添加PopupWindow mWindowManager.addView(mPopupWindow.getContentView(), getLayoutParams()); // 设置PopupWindow的位置 int x = 0; int y = 0; mPopupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, x, y); } @Override public void onDestroy() { super.onDestroy(); if (mPopupWindow != null) { mWindowManager.removeView(mPopupWindow.getContentView()); } } private WindowManager.LayoutParams getLayoutParams() { WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); layoutParams.format = PixelFormat.TRANSLUCENT; layoutParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT; layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; return layoutParams; } } ``` 在上述代码,我们首先通过LayoutInflater获取PopupWindow的内容View,然后创建PopupWindow并设置其宽度、高度、动画等属性。接着,我们使用WindowManager将PopupWindow添加到屏幕上,并设置其位置为屏幕央。最后,在Service销毁时,需要手动移除PopupWindow

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值