前幾篇小結(jié),基本都是一下基礎(chǔ)的,進來給各位同學(xué)帶點有進程一點的知識!接下來,我們將實現(xiàn)還有兩級的PopupWindow浮動窗。
效果圖:
PopupWindow的官方定義如下:
它是一個浮動在當(dāng)前界面上方并且可以顯示在任意位置的View,前面的章節(jié)我們學(xué)習(xí)了彈出框,各式各樣的,那么PopupWindow應(yīng)該有兩點和彈出框不同,一是PopupWindow必須指定寬高屬性,而彈出框則不是必須指定;二是PopupWindow必須指定其布局文件。
先給各位同學(xué)講一講,PopupWindow的基本方法:
? ? ? ? ? ?1.PopupWindow(Context context) ?——>構(gòu)造方法(傳入寬高屬性)
? ? ? ? ? ?2.PopupWindow(int width ,int height)? ——>構(gòu)造方法(傳入寬高屬性)
? ? ? ? ? ?3.PopupWindow(View contentView,int width,int height)? ——>構(gòu)造方法(傳入布局文件,寬高屬性)
? ? ? ? ? 4.PopupWindow(View contentView,int width,int height,boolean focusable)? ——>構(gòu)造方法(傳入布局文件,寬高屬性和是否獲取焦點)
? ? ? ? ? 5.dismiss ——>隱藏PopupWindow
? ? ? ? ? 6.setAnimationStyle(int animationStyle) ——>為popupwindow添加動畫
? ? ? ? ? 7.setContextView(View contentView)——>為popupwindow設(shè)置布局
? ? ? ? ? 8.setBackgroundDrawable(Drawable background)——>為popupwindow設(shè)置背景
? ? ? ? ? 9.setFocusable(boolean focusable)——>是否獲取焦點
? ? ? ? ? 10.setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) ——>設(shè)置popupwindow消失的監(jiān)聽
? ? ? ? ? 11.showAsDropDown(View anchor,int xoff,int yoff,int gravity)——>在某個控件下面彈出popupwindow
? ? ? ? ? 12.showAtLocation(View parent,int gravity,int x,int y)——>在父控件的什么位置彈出popupwindow
下面就帶同學(xué)們進入正題,如何實現(xiàn)上面的效果圖的效果:
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 一個接口,用于數(shù)據(jù)的回調(diào)
public interface PopupWindowCallBack {
void getData(int id);
}
MainActivity.java 這里我們就貼出關(guān)鍵代碼就行,其他請各位同學(xué)自行擴展.
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回調(diào)的id
*/
? ? @Override
? ?public voidgetData(intshop_type) {
? ? ? ? ?showToast("點擊了"+shop_type);
? }
}
好了,我們大功告成!