前几篇小结,基本都是一下基础的,进来给各位同学带点有进程一点的知识!接下来,我们将实现还有两级的PopupWindow浮动窗。
效果图:
PopupWindow的官方定义如下:
它是一个浮动在当前界面上方并且可以显示在任意位置的View,前面的章节我们学习了弹出框,各式各样的,那么PopupWindow应该有两点和弹出框不同,一是PopupWindow必须指定宽高属性,而弹出框则不是必须指定;二是PopupWindow必须指定其布局文件。
先给各位同学讲一讲,PopupWindow的基本方法:
1.PopupWindow(Context context) ——>构造方法(传入宽高属性)
2.PopupWindow(int width ,int height) ——>构造方法(传入宽高属性)
3.PopupWindow(View contentView,int width,int height) ——>构造方法(传入布局文件,宽高属性)
4.PopupWindow(View contentView,int width,int height,boolean focusable) ——>构造方法(传入布局文件,宽高属性和是否获取焦点)
5.dismiss ——>隐藏PopupWindow
6.setAnimationStyle(int animationStyle) ——>为popupwindow添加动画
7.setContextView(View contentView)——>为popupwindow设置布局
8.setBackgroundDrawable(Drawable background)——>为popupwindow设置背景
9.setFocusable(boolean focusable)——>是否获取焦点
10.setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) ——>设置popupwindow消失的监听
11.showAsDropDown(View anchor,int xoff,int yoff,int gravity)——>在某个控件下面弹出popupwindow
12.showAtLocation(View parent,int gravity,int x,int y)——>在父控件的什么位置弹出popupwindow
下面就带同学们进入正题,如何实现上面的效果图的效果:
layout_business_filter.xml
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_E3E3E3"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/left_recycler"
android:layout_width="0dp"
android:layout_height="220dp"
android:layout_weight="1"
android:background="@color/color_F5F5F5"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/right_recycler"
android:layout_width="0dp"
android:layout_height="220dp"
android:layout_weight="1"
android:background="@color/white"
android:paddingLeft="18dp"/>
</LinearLayout>
</LinearLayout>
SelectionAdapter.java 用于做下来的左边和右边recycleview的Adapter
public class SelectionAdapter extends BaseAdapter{
public static final intTYPE_LEFT=1;
public static final intTYPE_RIGHT=2;
private int mSelectionPosition;
private int mSelectedColor;
private int mUnSelectedColor;
private int padding;
private int mType;
public SelectionAdapter( List list,Context context, int type) {
super(list,context);
mSelectedColor= ContextCompat.getColor(mContext,R.color.yellow_click);
mUnSelectedColor= ContextCompat.getColor(mContext,R.color.world_color);
padding= DisplayUtils.dip2px(mContext,2);
this.mType= type;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, intviewType) {
return newTextViewHolder(createTextView());
}
@NonNull
protected TextView createTextView() {
TextView textView = new TextView(mContext);
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,RecyclerView.LayoutParams.WRAP_CONTENT);
if(mType == TYPE_LEFT) {
textView.setTextSize(16);
textView.setGravity(Gravity.CENTER);
}else{
textView.setTextSize(14);
textView.setGravity(Gravity.LEFT);
}
textView.setPadding(0,padding,0,padding);
textView.setLayoutParams(layoutParams);
return textView;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
TextViewHolder viewHolder = (TextViewHolder) holder;
TextView textView = (TextView) viewHolder.itemView;
textView.setText(mList.get(position));
if(position == mSelectionPosition) {
textView.setTextColor(mSelectedColor);
if(mType == TYPE_LEFT) {
textView.setBackgroundResource(R.drawable.blue_square);
}
}else{
textView.setTextColor(mUnSelectedColor);
if(mType==TYPE_LEFT) {
textView.setBackgroundResource(R.drawable.line4);
}else{
textView.setBackgroundResource(R.drawable.line5);
}
}
textView.setTag(position);
textView.setOnClickListener(mOnClickListener);
}
public int getSelectionPosition() {
return mSelectionPosition;
}
public void setSelectionPosition(intselectionPosition) {
mSelectionPosition= selectionPosition;
}
static class TextViewHolder extends RecyclerView.ViewHolder {
publicTextViewHolder(View itemView) {
super(itemView);
}
}
}
BusinessFilterPopupWindow.java 我们这里重写popupwindow
public class BusinessFilterPopupWindow extends PopupWindow{
private static final StringTAG="BusinessFilterPopupWindow.class";
private Selection Adapteradapter;
private Selection Adapteradapter2;
@BindView(R.id.left_recycler)
RecyclerView mLeftRecycler;
@BindView(R.id.right_recycler)
RecyclerView mRightRecycler;
private List mRightList = new ArrayList<>();
private List mGoodsTypes = new ArrayList<>();
private Listm ShopTypeList = new ArrayList<>();
List list = new ArrayList<>();
private int shop_type_id;
private PopupWindow CallBackmCallBack;
public BusinessFilterPopupWindow(Context context,List mShopTypes,List mGoodsTypes) {
super(context);
mShopTypeList.clear();
list.clear();
this.mShopTypeList= mShopTypes;
View contentView = LayoutInflater.from(context).inflate(R.layout.layout_business_filter, null);
ButterKnife.bind(this,contentView);
setContentView(contentView);
setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
setBackgroundDrawable(newColorDrawable(0x20000000));
setFocusable(true);
setOutsideTouchable(true);
mLeftRecycler.setLayoutManager(newLinearLayoutManager(context));
LogTrace.d(TAG,"mShopType size:",mShopTypeList.size()+"");
for(inti =0;i <mShopTypeList.size();i++) {
ShopType mShopType =mShopTypeList.get(i);
list.add(mShopType.getTitle());
}
adapter=newSelectionAdapter(list,context,SelectionAdapter.TYPE_LEFT);
adapter.setOnClickListener(mOnClickListener);
mLeftRecycler.setAdapter(adapter);
mRightRecycler.setLayoutManager(newLinearLayoutManager(context));
adapter2=new SelectionAdapter(mRightList,context,SelectionAdapter.TYPE_RIGHT);
adapter2.setOnClickListener(mOnClickListener);
mRightRecycler.setAdapter(adapter2);
}
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public voidonClick(View v) {
if(((ViewGroup) v.getParent()).getId() ==mLeftRecycler.getId()) {
inttag = (int) v.getTag();
adapter.setSelectionPosition(tag);
adapter.notifyDataSetChanged();
mRightList.clear();
for(inti =0;i<mShopTypeList.size();i++) {
if(tag == i){
mGoodsTypes=mTwoTypes.getChild();
for(intj =0;j<mGoodsTypes.size();j++) {
mRightList.add(mGoodsTypes.get(j).getTitle());
}
}
}
adapter2.notifyDataSetChanged();
}else{
adapter2.setSelectionPosition((int) v.getTag());
adapter2.notifyDataSetChanged();
String s =mRightList.get((Integer) v.getTag());
shop_type_id=mGoodsTypes.get((Integer)v.getTag()).getId();
LogTrace.d(getClass().getName(),"Onclick",s +shop_type_id);
mCallBack.getData(shop_type_id);
dismiss();
}
}
};
public void setCallBack(PopupWindowCallBack callBack) {
mCallBack= callBack;
}
}
PopupWindowCallBack.java 一个接口,用于数据的回调
public interface PopupWindowCallBack {
void getData(int id);
}
MainActivity.java 这里我们就贴出关键代码就行,其他请各位同学自行扩展.
public classMallActivityextendsBaseActivityimplementsPopupWindowCallBack {
protected Toolbar mToolbar;
private List mShopTypes = new ArrayList<>();
private List mTwoTypes = new ArrayList<>();
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mShopTypes.add("JAVA");
mShopTypes.add("PHP");
mShopTypes.add("Ruby");
mShopTypes.add("Python");
mTwoTypes.add("自助餐");
mTwoTypes.add("刀削面");
mTwoTypes.add("黄焖鸡");
mTwoTypes.add("勾魂面");
BusinessFilterPopupWindow popupWindow =new BusinessFilterPopupWindow(MallActivity.this,mShopTypes,mTwoTypes);
popupWindow.setCallBack(MallActivity.this);
popupWindow.showAsDropDown(mToolbar,0,DisplayUtils.dip2px(getBaseContext(),34);
}
/**
* popupWindow回调的id
*/
@Override
public voidgetData(intshop_type) {
showToast("点击了"+shop_type);
}
}
好了,我们大功告成!