完美 定义popwindow
来源:互联网 发布:手机刷机数据恢复 编辑:程序博客网 时间:2024/06/07 06:55
1.上类图
2.上效果图
3.实现思路
为了,便于调试、解耦、代码结构清晰。首先先到的就是进行封装处理。同时便于日后其他项目复用。不进行重复造轮子。
4.功能介绍
4.1 根据popwindow的显示隐藏进行回调小三角的style(向上,向下)。
4.2 监听item点击,进行状态选择切换。
4.3 进行选中态默认,当前选择的是第几个item,popwindow弹出来。默认选中第几个 item。
4.4 根据屏幕的宽度,设置popwindow 位置居中向上偏移一些。
4.5 给popwindow弹出动画
5.核心代码分析
package com.nuoyuan.sp2p.widget.home;import android.content.Context;import android.graphics.drawable.BitmapDrawable;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.PopupWindow;import com.nuoyuan.sp2p.R;import com.nuoyuan.sp2p.activity.info.ProductionListActivity;import com.nuoyuan.sp2p.util.LogUtil;import java.util.ArrayList;/** * Created by weichyang on 2016/6/15. */public class VPopWindow extends PopupWindow { private Context context; /** * 展示列表 */ private ArrayList<String> titles; /** * 依附于view */ private View anchorView; private PopUpwindowLayout popUpwindowLayout; private View contentView; /** * 构造方式 * * @param context * @param anchorView */ public VPopWindow(Context context, View anchorView, ArrayList<String> titles) { super(context, null); this.titles = titles; this.context = context; this.anchorView = anchorView; initPopWindowParameter(); } private void initPopWindowParameter() { contentView = LayoutInflater.from(context).inflate(R.layout.layout_popupwindow, null); this.setContentView(contentView); this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT); this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT); this.setFocusable(true); popUpwindowLayout = (PopUpwindowLayout) contentView.findViewById(R.id.llayout_popupwindow); popUpwindowLayout.initViews(context, titles); contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); // 允许点击外部消失 this.setBackgroundDrawable(new BitmapDrawable()); this.setOutsideTouchable(true); this.setAnimationStyle(R.style.popwindow_anim_style); } /** * 设置显示位置 * * @param anchorView */ public void showVpAsDropDown(View anchorView) { this.showAsDropDown(anchorView); } /** * 显示偏移量 * * @param anchorView * @param x * @param y */ public void showVpAsDropDown(View anchorView, int x, int y) { int[] location = new int[2]; anchorView.getLocationOnScreen(location); this.showAsDropDown(anchorView, x, y); } /** * 设置点击监听 * * @param onItemClick */ public void setOnItemClick(PopUpwindowLayout.OnClickCallback onItemClick) { if (popUpwindowLayout == null) { popUpwindowLayout = (PopUpwindowLayout) contentView.findViewById(R.id.llayout_popupwindow); } popUpwindowLayout.setClickListener(onItemClick); } /** * 设置默认选中项 * * @param index */ public void itemSelected(int index) { if (popUpwindowLayout != null) { popUpwindowLayout.setSelectedIndex(index); } else { LogUtil.e("popUpWindowLayout is null"); } }}
// item 类。主要对的item的点击事件进行设置。提供外部访问的接口,选中状态设置的。这里使用代码,没有使用xml shape文件形式。更灵活
package com.nuoyuan.sp2p.widget.home;import android.content.Context;import android.content.res.ColorStateList;import android.text.TextUtils;import android.util.AttributeSet;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TextView;import com.nuoyuan.sp2p.R;import com.nuoyuan.sp2p.util.DensityUtil;import com.nuoyuan.sp2p.util.DeviceUtil;import com.nuoyuan.sp2p.util.StringUtil;import java.util.ArrayList;import java.util.List;/** * Created by weichyang on 2016/6/14. * 弹窗view */public class PopUpwindowLayout extends LinearLayout { private Context mContext; private OnClickCallback mCallback; private List<TextView> txtViewList; public PopUpwindowLayout(Context context) { this(context, null); } public PopUpwindowLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PopUpwindowLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setOrientation(VERTICAL); setBackgroundResource(R.drawable.bmap_bg); init(); } private void init() { txtViewList = new ArrayList<>(); } /** * 设置标题内容 * * @param context * @param titles 标题文字内容 */ public void initViews(Context context, List<String> titles) { initViews(context, titles, false); } /** * 设置标题内容 * * @param context * @param titles 标题文字内容 * @param hasDraw 是否带右侧向下箭头 */ public void initViews(Context context, List<String> titles, boolean hasDraw) { this.mContext = context; setLayoutContent(mContext, titles, hasDraw); } /** * 设置条目点击监听 */ public void setClickListener(OnClickCallback callback) { this.mCallback = callback; } /** * 设置内容 * * @param context * @param titles * @param hasDraw */ private void setLayoutContent(Context context, final List<String> titles, boolean hasDraw) { removeAllViews(); if (titles != null && titles.size() > 0) { // 不带箭头 if (!hasDraw) { for (int i = 0; i < titles.size(); i++) { final int index = i; final TextView textView = new TextView(context); // 文本 textView.setText(titles.get(i)); // 颜色 float titleSize = getContext().getResources().getDimension(R.dimen.sp_18); // 字体 textView.setTextSize(DensityUtil.px2dip(context, titleSize)); textView.setGravity(Gravity.CENTER); textView.setPadding(20, 0, 20, 0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, DeviceUtil.dip2px(context, 35), Gravity.CENTER); params.gravity = Gravity.CENTER; textView.setLayoutParams(params); textView.setTextColor(createColorStateList(0xffffffff, 0xffC0A78A, 0xffffffff, 0xffffffff)); textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.investment_pop_selector)); addView(textView); // addDividingLine(context, titles, i); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mCallback != null) { mCallback.onItemClick(PopUpwindowLayout.this, titles.size(), index); } } }); // TODO: 2016/6/22 避免重复添加 if (i == 0) { txtViewList.clear(); txtViewList.add(textView); } else { txtViewList.add(textView); } } } } else { throw new RuntimeException("title counts is less than 0"); } } /** * 增加中间分割线 * * @param context * @param titles * @param i */ private void addDividingLine(Context context, List<String> titles, int i) { if (i < titles.size() - 1) { LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1); layoutParams.gravity = Gravity.CENTER_VERTICAL; View view = new View(context); view.setLayoutParams(layoutParams); view.setBackgroundResource(R.color.gray_9); addView(view); } } /** * http://blog.csdn.net/abc6368765/article/details/51482317 * * @param normal * @param pressed * @param focused * @param unable * @return */ private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) { int[] colors = new int[]{pressed, focused, normal, focused, unable, normal}; int[][] states = new int[6][]; states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}; states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}; states[2] = new int[]{android.R.attr.state_enabled}; states[3] = new int[]{android.R.attr.state_focused}; states[4] = new int[]{android.R.attr.state_window_focused}; states[5] = new int[]{}; ColorStateList colorList = new ColorStateList(states, colors); return colorList; } /** * 点击事件接口 */ public interface OnClickCallback { /** * 点击子视图时调用 * * @param parentView 当前操作的View视图 * @param size 当前视图中子视图数量 * @param index 当前点击子视图索引 */ void onItemClick(LinearLayout parentView, int size, int index); } /** * 选中 * * @param index */ public void setSelectedIndex(int index) { if (!txtViewList.isEmpty()) for (int i = 0; i < txtViewList.size(); i++) { if (i == index) { txtViewList.get(i).setPressed(true); } else { txtViewList.get(i).setPressed(false); } } }}
转载请注明:
0 0
- 完美 定义popwindow
- POPwindow
- popwindow
- popwindow
- PopWindow
- popwindow
- popwindow
- PopWindow
- popwindow
- popwindow
- popwindow
- popwindow 大全
- PopWindow用法
- Popwindow封装
- 30-popwindow
- 自定义 Popwindow
- 自定义popWindow
- android PopWindow
- 单元格的表示方式
- div.2/Bellovin<最长上升子序列>
- java中equals与==的区别
- 电脑的运行、控制面板、命令提示符被禁用的解决方案
- 如何在Android Studio使用Espresso框架自动化测试
- 完美 定义popwindow
- 调整数组顺序使奇数位于偶数前面
- 双向链表
- 夜间模式
- 四、封装OkHttp
- poj3628
- 安卓 toast
- Servlet(1)
- 传智播客黑马程序员骗局真相揭秘