现在的位置: 首页 > 综合 > 正文

可扩展的popUpwindow

2018年02月16日 ⁄ 综合 ⁄ 共 6158字 ⁄ 字号 评论关闭

  通常我们使用popUpwindow的时候例如都是写一个布局文件然后设置到popUpwindow的自定义布局当中,但有时候我们可能有这样的需求,底部的取消按钮是一定的,但是上面的选项不是写死的两个或者三个而是更加我们的需求动态来决定的,这个时候我们不可能去写死一个布局了,那么就需要我们自定义一个pop,动态添加同时要可以监听各项的点击事件。。。

   先说一下思路,我们先定义一个basepopwindow,来抽象几个公用方法,然后通过子类来实现

  具体代码:

A:抽象基类

      

public abstract class BasePopupWindow extends PopupWindow {

	protected View popRootView;
	
	public BasePopupWindow() {
		super();
	}

	public BasePopupWindow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public BasePopupWindow(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public BasePopupWindow(Context context) {
		super(context);
	}

	public BasePopupWindow(int width, int height) {
		super(width, height);
	}

	public BasePopupWindow(View contentView, int width, int height,
			boolean focusable) {
		super(contentView, width, height, focusable);
	}

	public BasePopupWindow(View contentView) {
		super(contentView);
	}
	
	public BasePopupWindow(View contentView, int width, int height){
		super(contentView, width, height,true);
		this.popRootView = contentView;
		setFocusable(true);
	    setOutsideTouchable(true);
	    setTouchable(true);
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		this.setBackgroundDrawable(dw);
        setAnimationStyle(R.style.AnimBottom);
        initViews();
        initEvents();
        init();
		
	}
	public abstract void initViews();

	public abstract void initEvents();

	public abstract void init();

	public View findViewById(int id) {
		return popRootView.findViewById(id);
	}

B: 实现子类:

   里面加入我的一些说明:

public class CommBottomPopWindow extends BasePopupWindow implements
        OnClickListener {

    private Button cancleBtn;

    private PopWindowListener listener;

    private LinearLayout mLayout;

    private Context mContext;

    private boolean isHasSubTitle = false;

    private LayoutInflater inflater;

    /**
     * 功能描述: 设置点击事件<br>
     * 〈功能详细描述〉
     *  点击的自定义回调接口
     */
    public void setPopListener(PopWindowListener listener) {
        this.listener = listener;
    }

    public CommBottomPopWindow(Context context) {
       //布局填充
        super((LayoutInflater.from(context).inflate(
                R.layout.comm_bottom_popwindow, null)),
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mContext = context;
    }

    /**
     * 功能描述:初始化小标题 <br>
       顶部是否需要提示的小标题
     */
    public void initPopSubTitle(String notiTxt) {
        mLayout.addView(createItem(notiTxt, true));
    }

    /**
     * 功能描述: 初始化item<br>
     * 〈功能详细描述〉
        动态添加的条目
     */
    public void initPopItem(List<String> list) {

        if (list == null || list.size() == 0) {
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            String title = list.get(i);
            mLayout.addView(createItem(title, i, list.size()));
        }
    }

    private View createItem(String itemTxt, boolean isSubTitle) {
        return createItem(itemTxt, -1, -1, isSubTitle);
    }

    private View createItem(String itemTxt, final int index, int total) {
        return createItem(itemTxt, index, total, false);
    }

    /**
     * 功能描述: 创建item<br>
     * 〈功能详细描述〉
     * 
       创建具体的条目
     */
    private View createItem(String itemTxt, final int index, int total,
            boolean isSubTitle) {
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.comm_bottom_popwindow_item, null);
        LinearLayout layout = (LinearLayout) view
                .findViewById(R.id.comm_popwindow_item_layout);

        TextView textView = (TextView) view
                .findViewById(R.id.comm_popwindow_item_txt);

        textView.setText(itemTxt);

        if (isSubTitle) {
            isHasSubTitle = true;
            layout.setBackgroundResource(R.drawable.selectpopwin_up);
            textView.setTextColor(ResUtil.getColor(R.color.color_999999));
        } else if (index == 0 && !isHasSubTitle) {
            layout.setBackgroundResource(R.drawable.btn_selectpopwin_up);
        } else if (index == total - 1) {
            layout.setBackgroundResource(R.drawable.btn_selectpopwin_down);
        } else {
            layout.setBackgroundResource(R.drawable.btn_camp_selpopwin_center);
        }

        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (index == -1) {
                    return;
                }
                if (listener != null) {
                    listener.onPopSelected(index);
                }
            }
        });
        return view;
    }

    @Override
    public void initViews() {
        mLayout = (LinearLayout) findViewById(R.id.comm_bottom_popwindow_layout);
        cancleBtn = (Button) findViewById(R.id.camp_pop_cancle);
        isHasSubTitle = false;
    }

    @Override
    public void initEvents() {
        cancleBtn.setOnClickListener(this);
        popRootView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void init() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.camp_pop_cancle:
                dismiss();
                break;
            default:
                break;
        }

    }

    /**
     * 功能描述: 显示pop window<br>
     * 〈功能详细描述〉
     */
    public void show(View view) {
        showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }
    //回调接口定义
    public interface PopWindowListener {
        public void onPopSelected(int which);
    }

C:贴下布局

    comm_bottom_popwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingTop="20dp" >

        <LinearLayout
            android:id="@+id/comm_bottom_popwindow_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>

        <Button
            android:id="@+id/camp_pop_cancle"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/comm_padding_size_9"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/btn_selectpopwin_cancel"
            android:text="@string/camp_cancle"
            android:textColor="@color/text_blue"
            android:textSize="17sp" />
    </LinearLayout>

</LinearLayout>

comm_bottom_popwindow_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/comm_popwindow_item_layout"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/comm_padding_size_9"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/comm_popwindow_item_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/camp_balance_query"
        android:gravity="center"
        android:textColor="@color/text_blue"
        android:textSize="17sp" />

</LinearLayout>

  其实就是定义一个linearlayout,根据具体的需求添加里面的item即可,同时通过一个自定义的接口监听item的点击就ok了

  ps 用法如下:

  private void initPopWindow() {
        if (mPopWindow == null) {
            mPopWindow = new CommBottomPopWindow(getActivity());
            List<String> list = new ArrayList<>();
            list.add("test1");
            list.add("test2");
            // 带显示小标题,可加可不加
            mPopWindow.initPopSubTitle("sub title context");
            mPopWindow.initPopItem(list);
            mPopWindow.setPopListener(mPopListener);
        }
        // 显示pop window
        mPopWindow.show(view);
    }

抱歉!评论已关闭.