复用代码,接口扩展(自定义poupwindow)

来源:互联网 发布:js修改字体颜色 编辑:程序博客网 时间:2024/06/05 21:49

1:效果图


2:代码复用部分,以及用来耦合的抽象接口

抽象接口:


publicinterface IFillView //接口,暴露的抽象规则;

{

 

    View  getView();

    void setBroadCast(IBroadcast iBroadcast);

}

public interface IBroadcast//回调接口,用于类之间的异步交互
{
   
voidsetTag(Object type);

   
void close();

}

 

复用代码


public abstractclass PoupFillViewextendsPopupWindowimplementsIBroadcast
{
   
privateActivityact;
   
private intwidth,height;
   
privateIFillViewiFillView;

   
publicPoupFillView(Activityact,IFillView iFillView)
    {
       
super(act);
       
this.act=act;
       
this.iFillView=iFillView;
       
height=act.getWindowManager().getDefaultDisplay().getHeight();
       
width=act.getWindowManager().getDefaultDisplay().getWidth();
    }

   
public voidinitPoupView()
    {
        setContentView(
iFillView.getView());
       
iFillView.setBroadCast(this);

        setFocusable(
true);
        setTouchable(
true);
        setHeight(
height);
        setWidth(
width);
        setAnimationStyle(R.style.
PopumAnimation);

    }


   
@Override
   
public voidclose()
    {
      dismiss();
    }


}

3:扩展部分(只需实现IFillView接口即可)
 

public classPoupWebView implementsIFillView
{
   
privateContext context;

   
public PoupWebView(Context context)
    {
       
this.context=context;
        initView();
        initInfo();
        initBinder();
    }

   
privateView contentView;

   
privateTextView tvOk;
   
privateTextView tvNo;
   
privateWebView webView;

   
private voidinitView()
    {
       
contentView= LayoutInflater.from(context).inflate(R.layout.poup_webview,null);
       
tvOk= (TextView)contentView.findViewById(R.id.poup_ok);
       
tvNo= (TextView)contentView.findViewById(R.id.poup_no);
       
webView= (WebView)contentView.findViewById(R.id.poup_web);


    }

   
privateString url;
    
private voidinitInfo()
    {
       
url="http://mt.sohu.com/20150923/n421957484.shtml";
    }

   
private voidinitBinder()
    {
       
webView.loadUrl(url);

       
tvOk.setOnClickListener(newView.OnClickListener() {
           
@Override
            
public voidonClick(View v) {
               
iBroadcast.setTag(false);
               
iBroadcast.close();
            }
        });
       
tvNo.setOnClickListener(newView.OnClickListener() {
           
@Override
           
public voidonClick(View v) {
               
iBroadcast.setTag(true);
               
iBroadcast.close();
            }
        });


    }


   
@Override
   
public View getView() {
       
returncontentView;
    }

   
privateIBroadcast iBroadcast;

   
@Override
   
public voidsetBroadCast(IBroadcast iBroadcast)
    {
      
this.iBroadcast=iBroadcast;

    }
4
,调用使用
final
PoupFillView poupFillView=newPoupFillView(getActivity(),newPoupWebView(getActivity())) {
   
@Override
   
public voidsetTag(Object type) {
        Boolean logic= (Boolean) type;
       
if(!logic)
        {
            getActivity().finish();
        }

    }
};

poupFillView.showAtLocation(convertView, Gravity.CENTER,0,0);

}
0 0
原创粉丝点击