PopupWindow含二級目錄的浮動窗

前幾篇小結(jié),基本都是一下基礎(chǔ)的,進來給各位同學(xué)帶點有進程一點的知識!接下來,我們將實現(xiàn)還有兩級的PopupWindow浮動窗。

效果圖:


PopupWindow.jpg

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效果圖

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);

? }

}


好了,我們大功告成!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,412評論 6 532
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,514評論 3 416
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,373評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,975評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,743評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,199評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,262評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,414評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,951評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,780評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,527評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,218評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,649評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,889評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,673評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,967評論 2 374

推薦閱讀更多精彩內(nèi)容