可扩展的popUpwindow

可扩展的popUpwindow

原创  2014年10月23日 16:44:30

  通常我们使用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 用法如下:

扫描二维码关注公众号,回复: 653107 查看本文章
 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);
}


  • 本文已收录于以下专栏:

PopUpWindow使用详解(一)--基本使用

前言:不要嫌前进的慢,只要一直在前进就好。有同学讲到想要知道PopUpWindow的知识,这里就给大家讲一讲PopUpWindow的基本用法和原理吧。这段时间博客可能会更新比较慢,因为你懂的 !!-_...

简单定制Android控件(3) - 打造通用的PopupWindow(一)

国际惯例,先上地址 https://github.com/razerdp/BasePopup 通常情况下,面对各种浮动窗口,选择窗口什么的,我们通常都是使用popupWindow,但是很多时候我...
  • mkfrank
  • mkfrank
  • 2016年01月15日 11:33
  • 3723

十九、使用PopupWindow

一、PopupWindow与AlertDialog的区别 最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)...

自定义PopupWindow+BaseAdapter+Anim

activity_main.xml

Android开发之旅一PopupWindow写的一个基类和PopupWindow中EditText输入法的问题

呵呵,几乎看遍了,才找到解决的方法,看看下面的代码 import android.app.Activity; import android.graphics.BitmapFactory;...
  • qqqGL
  • qqqGL
  • 2014年02月27日 19:14
  • 1802

自定义PopupWindow--可扩展操作

现在很多人喜欢自定义控件,这样可以满足各种各样的操作,这段时间我也试着弄了一个自定义的popupwindow仅供参考。 1.动画效果:这里使用从下往上弹出的效果,可以自己定义喜欢的效果 2.把...
  • nzzl54
  • nzzl54
  • 2016年08月01日 16:32
  • 630

从零撸一个优雅的Android Http网络框架(一)框架搭建

从零撸一个优雅的Android Http网络框架系之框架搭建 此系列文章基于Java原生URLConnection打造一款优雅的HTTP网络框架,惊喜多多,学点多多 Github地址:https:...

【Android进阶】多抽屉效果 (类似最早QQ使用的效果)

 也不知道该怎么取名,暂且就叫他多抽屉效果吧~~  最早QQ就是这样的效果,点一下,还有声音,呵呵。 一晃,都过去那么多年了...  废话不多说了,看下效果:   这个就是类似抽屉的效果,这边做了三个...
文章来自:http://c.360webcache.com/c?m=43c1eb76da027a9980ddc64860aeeb4f&q=public+BasePopupWindow%28View+contentView%2C+int+width%2C+int+height%29+%7B&u=http%3A%2F%2Fblog.csdn.net%2Fyaya_soft%2Farticle%2Fdetails%2F40399727

  通常我们使用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);
}


猜你喜欢

转载自blog.csdn.net/li_yu_csdn/article/details/79135278
今日推荐