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

本文介绍了一种基于抽象接口的弹窗组件设计方法,通过定义IFillView接口和IBroadcast接口实现弹窗的显示与交互。具体实现包括PoupFillView基类及PoupWebView扩展类,并展示了如何在实际应用中进行调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:效果图


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

抽象接口:


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

{

 

    View  getView();

    void setBroadCast(IBroadcast iBroadcast);

}

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

   
void close();

}

 

复用代码


public abstractclass  PoupFillView extendsPopupWindow implements IBroadcast
{
   
private Activityact;
   
private int width,height;
   
private IFillViewiFillView;

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

   
public void initPoupView()
    {
        setContentView(
iFillView.getView());
       
iFillView.setBroadCast(this);

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

    }


   
@Override
   
public void close()
    {
      dismiss();
    }


}

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

public class PoupWebView implements IFillView
{
   
private Context context ;

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

   
private View contentView ;

   
private TextView tvOk ;
   
private TextView tvNo ;
   
private WebView webView ;

   
private void initView()
    {
       
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 );


    }

   
private String url ;
    
private void initInfo()
    {
       
url = "http://mt.sohu.com/20150923/n421957484.shtml" ;
    }

   
private void initBinder()
    {
       
webView .loadUrl( url );

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


    }


   
@Override
   
public View getView() {
       
return contentView ;
    }

   
private IBroadcast iBroadcast ;

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

    }
4
,调用使用
final
PoupFillView poupFillView= new PoupFillView(getActivity(), new PoupWebView(getActivity())) {
   
@Override
   
public void setTag(Object type) {
        Boolean logic= (Boolean) type;
       
if (!logic)
        {
            getActivity().finish();
        }

    }
};

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值