Popuwindow点击外部控件切换popuwindow——解决点击外部事件不响应问题。

问题描述:

popuwindow的原理这里就不赘述了。使用过的人都会发现一个问题。如图当你需要写一个通用控件,而不希望收到父布局限制的时候,你可能就要用到popuwindow,但是popuwindow有个问题,就是你在点击如图的三个tab的时候,你会发现,永远都是上一个popuwindow先消失,然后你要再点一下,新的popuwindow才会出现。

很多人想过通过释放touch事件把点击事件交给父布局处理。但是问题是,popuwindow在new的时候你就会发现,貌似不是从父布局来的,那么事件就无法交给父布局。

找了一些资料,都没有现成的接口或者事件处理能够很好的解决这个问题,很多人说可以换成dialog,我去,这真但想多了,用dialog问题会跟多。


所以还是自己想办法解决。


解决方案:

pupuwindow展示的时候,监听touchevent的坐标。然后判断该坐标是否在tab可点击范围以内,然后强行出发tab点击。

原理我就不再赘述了,代码贴的很明白,也没有特别复杂的逻辑。关键看下checkPointInCtrlBtn这个函数。


/**
 * Created by shenjianlin on 15/11/5.
 */
public class FilterView extends LinearLayout{

    private Activity mContext;
    private LinearLayout cart,sort,filter;
    private TextView cart_text,sort_text,filter_text;
    private ImageView cart_img,sort_img,filter_img;
    private LinearLayout rootLayout;

    private PopupWindow sortPopWindow,cartPopWindow,filterPopWindow;
    public FilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = (Activity)context;
        rootLayout = (LinearLayout)LinearLayout.inflate(context, R.layout.filter_headview,this);
        initView();
        initPopuWindow();
    }

    public FilterView(Context context){
        super(context);
        this.mContext = (Activity)context;
        rootLayout = (LinearLayout)LinearLayout.inflate(context, R.layout.filter_headview,this);
        initView();
        initPopuWindow();
    }

    private void initView(){
        cart = (LinearLayout)rootLayout.findViewById(R.id.cart);
        sort = (LinearLayout)rootLayout.findViewById(R.id.sort);
        filter = (LinearLayout)rootLayout.findViewById(R.id.filter);
        cart_text = (TextView)rootLayout.findViewById(R.id.cart_text);
        sort_text = (TextView)rootLayout.findViewById(R.id.sort_text);
        filter_text = (TextView)rootLayout.findViewById(R.id.filter_text);
        cart_img = (ImageView)rootLayout.findViewById(R.id.cart_img);
        sort_img = (ImageView)rootLayout.findViewById(R.id.sort_img);
        filter_img = (ImageView)rootLayout.findViewById(R.id.filter_img);

        cart.setOnClickListener(onClickListener);
        sort.setOnClickListener(onClickListener);
        filter.setOnClickListener(onClickListener);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            setTitleClick(v.getId());
        }
    };
    private void setTitleClick(int titleId){
        switch (titleId){
            case R.id.cart:
                cart_text.setTextColor(mContext.getResources().getColor(R.color.qtTextBlue));
                sort_text.setTextColor(mContext.getResources().getColor(R.color.unusual_balck));
                filter_text.setTextColor(mContext.getResources().getColor(R.color.unusual_balck));
                cartPopWindow.showAsDropDown(rootLayout);

                break;
            case R.id.sort:
                cart_text.setTextColor(mContext.getResources().getColor(R.color.unusual_balck));
                sort_text.setTextColor(mContext.getResources().getColor(R.color.qtTextBlue));
                filter_text.setTextColor(mContext.getResources().getColor(R.color.unusual_balck));
                sortPopWindow.showAsDropDown(rootLayout);

                break;
            case R.id.filter:
                cart_text.setTextColor(mContext.getResources().getColor(R.color.unusual_balck));
                sort_text.setTextColor(mContext.getResources().getColor(R.color.unusual_balck));
                filter_text.setTextColor(mContext.getResources().getColor(R.color.qtTextBlue));
                filterPopWindow.showAsDropDown(rootLayout);
                break;
            default:
                break;
        }
    }

    private void checkPointInCtrlBtn(MotionEvent event){

        int[] cartLocation = new int[2];
        int[] sortLocation = new int[2];
        int[] filterLocation = new int[2];
        cart.getLocationOnScreen(cartLocation);
        sort.getLocationOnScreen(sortLocation);
        filter.getLocationOnScreen(filterLocation);

        Logger.i("checkPointInCtrlBtn", "checkPointInCtrlBtn" +
                ";" + event.getRawX() + ";" + event.getRawY() + ";;cart:x=" + cart.getX() + ";cartY=" + cart.getY() +
                ";sortX=" + sort.getX() + ";sortY=" + sort.getY() + ";filterX" + filter.getX() + ";filterY=" + filter.getY());
        Logger.i("checkPointInCtrlBtn","checkPointInCtrlBtn cart:"+
        cartLocation.toString()+";sort:"+sortLocation.toString()+";filter:"+filterLocation.toString());
        if (event.getRawX()>cartLocation[0]
                &&event.getRawX()<(cartLocation[0]+cart.getMeasuredWidth())
                &&event.getRawY()>cartLocation[1]
                &&event.getRawY()<(cartLocation[1]+cart.getMeasuredHeight())){
            cart.performClick();
        }else if (event.getRawX()>sortLocation[0]
                &&event.getRawX()<(sortLocation[0]+sort.getMeasuredWidth())
                &&event.getRawY()>sortLocation[1]
                &&event.getRawY()<(sortLocation[1]+sort.getMeasuredHeight())){
            sort.performClick();
        }else if (event.getRawX()>filterLocation[0]
                &&event.getRawX()<(filterLocation[0]+filter.getMeasuredWidth())
                &&event.getRawY()>filterLocation[1]
                &&event.getRawY()<(filterLocation[1]+filter.getMeasuredHeight())){
            filter.performClick();
        }


    }
    private void initPopuWindow() {

        // 一个自定义的布局,作为显示的内容
        View sortView = LayoutInflater.from(mContext).inflate(
                R.layout.sort_pop_view, null);
        View cartView = LayoutInflater.from(mContext).inflate(
                R.layout.cart_pop_view, null);
        View filterView = LayoutInflater.from(mContext).inflate(
                R.layout.filter_pop_view, null);
        // 设置按钮的点击事件

        sortPopWindow = new PopupWindow(sortView,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
        cartPopWindow = new PopupWindow(cartView,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
        filterPopWindow = new PopupWindow(filterView,
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);

        cartPopWindow.setOutsideTouchable(true);
        sortPopWindow.setOutsideTouchable(true);
        filterPopWindow.setOutsideTouchable(true);

        sortPopWindow.setTouchable(true);
        cartPopWindow.setTouchable(true);
        filterPopWindow.setTouchable(true);

        sortPopWindow.setTouchInterceptor(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                ToastUtil.makeText(event.getRawX()+";"+event.getRawY());
                checkPointInCtrlBtn(event);
                return false;
            }
        });
        cartPopWindow.setTouchInterceptor(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                ToastUtil.makeText(event.getRawX()+";"+event.getRawY());

                checkPointInCtrlBtn(event);
                return false;

            }
        });
        filterPopWindow.setTouchInterceptor(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                checkPointInCtrlBtn(event);
                return false;

            }
        });

        sortPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.layer_list_downline));
        cartPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.layer_list_downline));
        filterPopWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.layer_list_downline));


    }
}

猜你喜欢

转载自blog.csdn.net/shenjinalin123/article/details/49680137