PopupWindow的簡單使用(結合RecyclerView)

Android彈窗:java

在Android中彈出式菜單(如下稱彈窗)是使用十分普遍一種菜單呈現的方式,彈窗爲用戶交互提供了便利。關於彈窗的實現大體有如下兩種方式AlertDialogPopupWindowandroid

二者的區別:AlertDialog彈窗在位置顯示上是固定的,而PopupWindow則相對比較隨意,可以在主屏幕上的任意位置顯示; 
今天就簡單介紹一下,如何利用PopupWindow實現RecyclerView的自定義的彈窗佈局;markdown

使用步驟:app

1.建立兩個xml文件,一個mainactivity主佈局,一個是popupwindow佈局(由於我是在項目裏寫的,因此閒雜代碼可能比較多):ide

主佈局(在其寫一個Button按鈕,由於項目須要,我換成了ImageView):佈局

<LinearLayout android:id="@+id/score_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:background="#f0f0f0"> <ImageView android:id="@+id/bttest" android:layout_width="50dp" android:layout_height="match_parent" android:layout_gravity="center" android:src="@mipmap/selectteam" /> </LinearLayout>

 

popupwindow.xml佈局(裏面放一個RecyclerView):post

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/select" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|enterAlways|snap"> </android.support.v7.widget.RecyclerView> </LinearLayout>

2.在MainActivity中爲ImageView進行實例化,併爲其設立點擊事件:spa

bselect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showPopupWindow(); } });
private void showPopupWindow() {
       View view = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow,null); RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.select); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(linearLayoutManager); ScoreTeamAdapter scoreTeamAdapter = new ScoreTeamAdapter(yearList); recyclerView.setAdapter(scoreTeamAdapter); popupWindow = new PopupWindow(main_layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setContentView(view); popupWindow.setFocusable(true); popupWindow.showAsDropDown(bselect); }

showPopupWindow()方法:code

1.先將popupwindow.xml佈局加載成一個View,並經過該View將RecyclerView進行實例化,而後RecyclerView進行設置,在這設置成豎向排列的線性佈局,而後爲其設置一個Adapter;xml

2.隨後將popupWindow進行設置:

popupWindow = new PopupWindow(main_layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

main_layout是ImageView的父容器LinearLayout, 
第一個ViewGroup.LayoutParams.WRAP_CONTENT是設置popupwindow的寬度,第二個ViewGroup.LayoutParams.WRAP_CONTENT是設置popupwindow的高度;

popupWindow.setContentView(view);

設置popupwindow的佈局;

popupWindow.showAsDropDown(bselect);

調用PopupWindow的showAsDropDown(View view)將PopupWindow做爲View組件的下拉組件顯示出來;或調用PopupWindow的showAtLocation()方法將PopupWindow在指定位置顯示出來;

最後的效果如圖: 
這裏寫圖片描述

想了解更多PopupWindow的用法,請看下面的簡書: 
http://www.jianshu.com/p/825d1cc9fa79

相關文章
相關標籤/搜索