PopupWindow使用總結(jié)

自從開學以來遇西,有一段時間沒有更新博客了疟丙,所以這兩天打算把一些東西重拾一下然后做一下總結(jié)涌矢。

PopupWindow

這是一個一旦使用就會喜歡上的一個控件雪猪,使用官方的話先來介紹一下這個東西膨更,他是一個彈出窗口“該類可以用于顯示任意視圖的彈出窗口厨喂。彈出窗口是一個浮動容器贷笛,顯示在當前活動頂部”靶庙,但是這個控件相對于dialog的好處是可以自定義其中的內(nèi)容叮叹,沒有標題艾栋,不是給其他布局添加蒙層效果,而且它默認不會響應(yīng)返回鍵蛉顽,最重要的是可以自定義他的位置蝗砾,而且十分的靈活。
先給出:官方文檔

首先需要創(chuàng)建

//參數(shù)列表有多種携冤,可以傳入自定義的contentView悼粮,設(shè)置寬高,是否可點擊等
View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop,null);
PopupWindwo popWnd = PopupWindow (context);
popWnd.setContentView(contentView);
popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)曾棕;

設(shè)置顯示的方法

//顯示窗口(相對于參照物控件扣猫,下方自定義位置,xy分貝表兩軸的偏移量)
showAsDropDown(View anchor,int xoff,int yoff)
//顯示在正下方
showAsDropDown(View anchor)
//相對于參照物的位置
showAtLocation(View parent, int gravity, int x, int )

常用的公用方法

.dismiss()//關(guān)閉彈窗
.setFocusable()//設(shè)置是否可以獲取焦點
.setTouchable()//是否可以點擊
.setContentView()//設(shè)置內(nèi)容
....
還有一些獲取寬高翘地,更新視圖的一些方法可以參照官方文檔申尤。

這兩天發(fā)現(xiàn)一個比較好用的開源控件,設(shè)置起來更是十分方便衙耕。

結(jié)合RecyclerView的使用

由于項目需要在一個Recyclerview列表中實現(xiàn)一個類似qq聊天長按刪除的一個效果昧穿,并且有一些總結(jié)。

//聲明接口臭杰,接口方式耦合度低
    /**item上控件點擊監(jiān)聽*/
    public interface ItemEditTextClickListener{
        void onWordListEdtClick(int position);
    }

適配器中

//聲明接口
private ItemEditTextClickListener mEditTextClickListener;

//onbindViewHolder中綁定監(jiān)聽事件
 //詢問線索情況氣泡彈出的點擊事件
        holder.wcEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditTextClickListener.onWordListEdtClick(position);
            }
        });

//并在適配器中提供對外的調(diào)用
    /**點擊Edt監(jiān)聽對外調(diào)用*/
    public void setEditTextClickListener(GameWord.ItemEditTextClickListener itemEditTextClickListener){
        mEditTextClickListener=itemEditTextClickListener;
    }

Activity中

private CustomPopWindow mCustomPopWindow;//本次使用的開源框架

//onCreate()中
//設(shè)置Edt的監(jiān)聽
        adapter.setEditTextClickListener(this);

/**
     * 點擊之后彈出popupwindow
     * */
    @Override
    public void onWordListEdtClick(int position) {
        Log.d("MakeGameActivity","On Edt click");
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_menu,null);
        //找到當前position對應(yīng)的item
        View view=mlayoutManager.findViewByPosition(position);
        //構(gòu)造popwindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
                .setFocusable(true)
                .setView(contentView)
                .setOutsideTouchable(true)
                .create()
                .showAsDropDown(view,500,-580);
        //添加彈框的點擊事件
        popupClick(contentView,position);
    }

/**氣泡中的item點擊事件*/
    private void popupClick(View contentView,final int position){

        final GameWord gameWord=gameWordList.get(position);
        //獲取到EditText
        View layout=(View)recyclerView.getChildAt(position);
        final AutoCompleteTextView autoCompleteTextView=layout.findViewById(R.id.word_clue_edit_view);
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCustomPopWindow!=null){
                    mCustomPopWindow.dissmiss();
                }
                String showContent="";
                switch (v.getId()){
                    case R.id.first_menu:
                        autoCompleteTextView.setText("1");
                        break;
                    case R.id.second_menu:
                        autoCompleteTextView.setText("2");
                        break;
                    case R.id.third_menu:
                        showContent="點擊菜單三";
                        break;
                    case R.id.forth_menu:
                        showContent="點擊菜單四";
                        break;
                        default:break;
                }              //Toast.makeText(MakeGameActivity.this,showContent,Toast.LENGTH_SHORT).show();
            }
        };
        //為四個litm綁定監(jiān)聽
        contentView.findViewById(R.id.first_menu).setOnClickListener(listener);
        contentView.findViewById(R.id.second_menu).setOnClickListener(listener);
        contentView.findViewById(R.id.third_menu).setOnClickListener(listener);
        contentView.findViewById(R.id.forth_menu).setOnClickListener(listener);
    }

最后是popupWindow的布局可以自定義設(shè)計粤咪。

<?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="match_parent"
    android:background="@color/colorFabIcon"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/first_menu"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:padding="5dp"
            android:src="@drawable/clues" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:paddingRight="40dp"
            android:text="添加中文" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/second_menu"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:padding="5dp"
            android:src="@drawable/bd_ocr_light_off" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:paddingRight="40dp"
            android:text="添加英文" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/third_menu"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:padding="5dp"
            android:src="@drawable/xiansuo" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:paddingRight="40dp"
            android:text="手動輸入" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/forth_menu"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:padding="5dp"
            android:src="@drawable/notebook" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:paddingRight="40dp"
            android:text="取消" />
    </LinearLayout>
</LinearLayout>

此外還有一線需要總結(jié)一下,那就是在外部如何獲根據(jù)position取到Recyclerview中item的子控件渴杆。思路大概就是,首先根據(jù)position獲取對應(yīng)的childview宪塔。之后通過對應(yīng)的childView根據(jù)id獲取到子控件磁奖。

//獲取到EditText
        View layout=(View)recyclerView.getChildAt(position);
        final AutoCompleteTextView autoCompleteTextView=layout.findViewById(R.id.word_clue_edit_view);

盡量不要給EditText添加點擊事件,因為EditText首先會獲取到焦點某筐,之后再點擊才會觸發(fā)點擊事件比搭。也就是在沒有獲取到焦點的時候雙擊才會觸發(fā)點擊事件。一些點擊之間的沖突還有待在深入研究一下南誊。還有在設(shè)計用戶交互的時候盡量不要做一些超越一般用戶認知的交互身诺。點擊事件盡量使用按鈕的形式蜜托,不要使用一些其他亂七八糟的東西,比如文本霉赡,圖片之類的橄务。
還有進一步的用戶體驗,比如在輸入完文本自用隱藏軟鍵盤穴亏,當需要編輯的時候使用代碼為用戶自動彈出軟鍵盤蜂挪。

//強制關(guān)閉鍵盤
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
//強制顯示
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(activity.getWindow().getDecorView(),InputMethodManager.SHOW_FORCED);

開學是比較忙,一些東西落下了嗓化,所以之后會繼續(xù)堅持做總結(jié)棠涮。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市刺覆,隨后出現(xiàn)的幾起案子严肪,更是在濱河造成了極大的恐慌,老刑警劉巖谦屑,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驳糯,死亡現(xiàn)場離奇詭異,居然都是意外死亡伦仍,警方通過查閱死者的電腦和手機结窘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來充蓝,“玉大人隧枫,你說我怎么就攤上這事∥焦叮” “怎么了官脓?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長涝焙。 經(jīng)常有香客問我卑笨,道長,這世上最難降的妖魔是什么仑撞? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任赤兴,我火速辦了婚禮,結(jié)果婚禮上隧哮,老公的妹妹穿的比我還像新娘桶良。我一直安慰自己,他們只是感情好沮翔,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布陨帆。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪疲牵。 梳的紋絲不亂的頭發(fā)上承二,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音纲爸,去河邊找鬼亥鸠。 笑死,一個胖子當著我的面吹牛缩焦,可吹牛的內(nèi)容都是我干的读虏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼袁滥,長吁一口氣:“原來是場噩夢啊……” “哼盖桥!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起题翻,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤揩徊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后嵌赠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體塑荒,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年姜挺,在試婚紗的時候發(fā)現(xiàn)自己被綠了齿税。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡炊豪,死狀恐怖凌箕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情词渤,我是刑警寧澤牵舱,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站缺虐,受9級特大地震影響芜壁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜高氮,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一慧妄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧剪芍,春花似錦腰涧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至坊谁,卻和暖如春费彼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背口芍。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工箍铲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鬓椭。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓颠猴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親小染。 傳聞我的和親對象是個殘疾皇子翘瓮,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

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