Android 封装popwindow类,实现所有activity共用

popwindow是一个弹出window,在此对其进行封装,使这个popwindow可以在所有页面都显示。

在CSDN上有类似的封装类供下载,可以去自行下载然后做修改。附上地址:http://download.csdn.net/detail/jdsjlzx/9583823

这样一来就会出现一个问题,定义popwindow上的item的点击事件做相应的动作的时候,就会提示空指针异常,从而强制退出程序。如下将分析如何解决。

定义公用类 以便调用所有activity调用。

package com.ytumobilelibrary.common;

import com.ytumobilelibrary.readerservice.LoginActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CommonPopWindowActivity extends Activity {

    private PopMenu popMenu; // 此popmenu是2中定义的popwindow生成类
    private Context context;
    private Context current_activity;
    private static CommonPopWindowActivity instance;

    public synchronized static CommonPopWindowActivity getInstance() {
        if (null == instance) {
            instance = new CommonPopWindowActivity();
        }
        return instance;

    }

    public void buildPopWindow(Activity activity, ImageButton popWind) {

        this.context = activity;
        popMenu = new PopMenu(context);

        popMenu.addItems(new String[] { "登录", "注销", "使用说明", "关于软件" });
        // 菜单项点击监听器
        popMenu.setOnItemClickListener(popmenuItemClickListener);

        popWind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                popMenu.showAsDropDown(v);
            }
        });
    }

    // 弹出菜单监听器
    OnItemClickListener popmenuItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position,
        long id) {
        current_activity = view.getContext();
        // current_activity 的定义是为了取得调用此方Activity
        //的本身实例,这样才不会报空指针异常。
        System.out.println("下拉菜单点击" + position);
        // 取得item上的值
        // HashMap item = (HashMap)
        // adapterView
        // .getItemAtPosition(position);
        Intent intent = new Intent();
        intent.putExtra("Flag", RS_FLAG);
        intent.setClass(current_activity, LoginActivity.class);
        ((Activity) current_activity).startActivityForResult(
        intent, 5);

        popMenu.dismiss();

        };
}

定义pop类

package com.ytumobilelibrary.common;

import java.util.ArrayList;
import com.ytumobilelibrary.R;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PopMenu {
    private ArrayList itemList;
    private Context context;
    private PopupWindow popupWindow;
    private ListView listView;

    // private OnItemClickListener listener;
    public PopMenu(Context context) {

        this.context = context;
        itemList = new ArrayList(5);
        View view = LayoutInflater.from(context).inflate(R.layout.popmenu, null);
        // 设置 listview
        listView = (ListView) view.findViewById(R.id.popwin_listView);
        listView.setAdapter(new PopAdapter());
        listView.setFocusableInTouchMode(true);
        listView.setFocusable(true);
        // popupWindow = new PopupWindow(view, 100, LayoutParams.WRAP_CONTENT);
        popupWindow = new PopupWindow(view, context.getResources().getDimensionPixelSize(R.dimen.popmenu_width),
                context.getResources().getDimensionPixelSize(R.dimen.popmenu_height));
        // context.getResources()
        // .getDimensionPixelSize(R.dimen.popmenu_width)
        // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的)
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    // 设置菜单项点击监听器
    public void setOnItemClickListener(OnItemClickListener listener) {
        // this.listener = listener;
        listView.setOnItemClickListener(listener);
    }

    // 批量添加菜单项
    public void addItems(String[] items) {
        for (String s : items)
            itemList.add(s);
    }

    // 单个添加菜单项
    public void addItem(String item) {
        itemList.add(item);
    }

    // 下拉式 弹出 pop菜单 parent 右下角
    public void showAsDropDown(View parent) {
        popupWindow.showAsDropDown(parent, 10,
                // 保证尺寸是根据屏幕像素密度来的
                context.getResources().getDimensionPixelSize(R.dimen.popmenu_yoff));
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // // 刷新状态
        popupWindow.update();
    }

    // 隐藏菜单
    public void dismiss() {
        popupWindow.dismiss();
    }

    // 适配器
    private final class PopAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return itemList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.pomenu_item, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
                holder.groupItem = (TextView) convertView.findViewById(R.id.popwin_textView);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.groupItem.setText(itemList.get(position));
            return convertView;
        }

        private final class ViewHolder {
            TextView groupItem;
        }
    }
}

pomenu_item.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120.0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:background="@drawable/bg_pop_item" > 
    <TextView
    android:id="@+id/popwin_textView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10.0dip"
    android:gravity="center"
    android:text="测试"
    android:textColor="#FFFFFF"
    android:textSize="17.0dip" />
LinearLayout>

pomenu.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_view_cont"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
    <ListView
    android:id="@+id/popwin_listView"
    android:layout_width="fill_parent"
    android:layout_height="0.0dip"
    android:layout_weight="1.0"
    android:background="@drawable/map_tips" 
    android:cacheColorHint="#00000000"
    android:divider="@drawable/line2"
    android:dividerHeight="4.0dip"
    android:focusable="true"
    android:scrollbars="none" >
    ListView>
LinearLayout>

bg_pop_item.xml


<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/home_btn_bg_s"
    android:state_pressed="true"/>
    <item android:drawable="@drawable/home_btn_bg_s"
    android:state_enabled="true"
    android:state_focused="true"/>
    <item android:drawable="@drawable/home_btn_bg_s"
    android:state_checked="true"
    android:state_enabled="true"
    >item>
selector>

在所有Activity中调用公用类CommonPopWindowActivity 的方法如下:

//ImageButton 是所有activity中定义的ImageButton,点击此bt可以弹出popwindow
ImageButton popWind = (ImageButton) findViewById(R.id.radio_button4);
//加载popwindow this就是该activity的实例,这样在CommonPopWindowActivity类中跳转页面或者弹出toast才不会报空指针异常
CommonPopWindowActivity.getInstance().buildPopWindow(this,popWind);

你可能感兴趣的:(Android开发)