PopWindow踩坑

今天終於認真的弄了一回自定義的彈出框,但是踩了兩個坑,一個是自己粗心,一個是自己認識不夠深。

1.第一坑

在我的popwindow的xml中,我加入了<view>弄了一條下劃線,哦,就是這樣,我把<View>寫成了<view>,

 mItemView = LayoutInflater.from(mContext).inflate(R.layout.item_list_music,viewGroup, false);
  contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_music_detail,null);

然後上面第二句老是報錯,說

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean 
java.lang.String.equals(java.lang.Object)' on a null object reference....
....
    at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418)

什麼玩意,這兩句話有啥大區別呀,害我以爲是我一個activity一個fragment共用這個adapter不合適

debug了好久,最後只能查代碼,最終在新增加的pop_music_detail.xml中發現了這個問題,下面的是改好了的QAQ

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:background="@drawable/popwindow_radius"<!--去掉這個佈局,加載relative那裏應該
也行-->
    android:padding="@dimen/marginSize"
    android:orientation="vertical">
<LinearLayout
    android:id="@+id/add_collection"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom|center_horizontal"

    android:orientation="horizontal">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/add_list_1"

        />
    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/marginTabSize"
        android:text="收藏到歌單"
        android:textSize="@dimen/titleSize"
        android:layout_weight="1"/>


</LinearLayout>
    <View style="@style/line2"/>
    <LinearLayout
        android:id="@+id/歌手"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|center_horizontal"
        android:orientation="horizontal">
        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/sort_sing"
            />
        <TextView
            android:paddingLeft="@dimen/marginTabSize"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="歌手:"
            android:textSize="@dimen/titleSize"
            android:layout_weight="1"/>
    </LinearLayout>
    <View
        style="@style/line2"/>
    </LinearLayout>
</RelativeLayout>

2.我想要彈出框框後後面的界面變暗一點,這樣子纔好看嘛是吧,於是抄起手來就是一頓敲,嗯,成功了!

但是四個尖角好醜啊,我想要變成圓角,弄了個drawable的xml設置radius,馬上安排,安排在RelativeLayout上,結果沒得作用……我懵了,爲啥人家成功了我沒有,沒道理,行,那我嵌套多一個LinearLayout那裏再設置RelativeLayout爲透明(傻子,在java裏已經設置了popWindow的background,但是那會我設置成了白色)然後我看到的都是白色!尖角!我的心拔涼拔涼的。後來跟別人吐槽的時候猛然回頭發現其實我java裏的popWindow是已經設置了的,因爲不設置的話,點擊框框外部,框框會一直在沒反應的,但是我當時採用的是第一種方法,直接把顏色寫成了白色,纔會釀成這樣的後果,不該呀!!其實感覺沒必要再嵌多一個LinearLayout,orientation="vertical"那個,直接還是按照原來的把drawable的xml設置在RelativeLayout就好

popupWindow.setBackgroundDrawable(contentView.getResources().getDrawable(android.R.color.white));×
popupWindow.setBackgroundDrawable(contentView.getResources().getDrawable(android.R.color.transparent));✔
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle" >
    <!--弧度-->
  <solid android:color="#ffffff"/>
    <corners
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp"
    android:bottomRightRadius="8dp"
    android:bottomLeftRadius="8dp"/>

</shape>

我的主要需求是在recyclerview中點擊某個item就彈框,所以寫在了adapter裏面,而且多個activity或者fragment複用

adapter的一部分代碼如下,不知道把popwindow寫成這樣子合不合理=。=

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder> {

    private Context mContext;
    private View mItemView;
    private  PopupWindow popupWindow;
    private RecyclerView mRv;
    private LinearLayout ivMore;
    private LinearLayout mAddCollectionList;
    private boolean isCalculationRecyclerView;
    private View contentView;
    public MusicListAdapter (Context context, RecyclerView recyclerView){

        mContext = context;
        mRv = recyclerView;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mItemView = LayoutInflater.from(mContext).inflate(R.layout.item_list_music,viewGroup, false);
         contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_music_detail,null);
//        LinearLayout add_collection = contentView.findViewById(R.id.add_collection);
        popupWindow = new PopupWindow(contentView, 
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
//        popupWindow.setBackgroundDrawable(contentView.getResources().getDrawable(android.R.color.transparent));
        popupWindow.setBackgroundDrawable(new BitmapDrawable(contentView.getResources(), (Bitmap) null));
//一定一定要設置背景,不然會出事,還有,如果嫌棄框框的四角有模有樣的想要磨成弧形,一定要設置爲以
//上兩個的一種,要麼是android的transparent,要麼是這個null!!!!!

        return new ViewHolder(mItemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int ViewType) {
        setRecyclerViewHeight();

//       TODO 多張圖片的加載
        
        /**
         * 給ivMore添加監聽事件,用戶點擊了,就彈出小頁面*/
        ivMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    setBackgroudAlpha(0.7f);
                    popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
                    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                    @Override
                    public void onDismiss() {
                        setBackgroudAlpha(1f);
                    }
                });
                    mAddCollectionList.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(mContext, "被點擊了", Toast.LENGTH_SHORT).show();
                        }
                    });
//                popupWindow.showAsDropDown(mRv);
//                Toast.makeText(mContext, "被點擊了", Toast.LENGTH_SHORT).show();
               
            }
        });

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 改爲點擊音樂
                 Intent intent = new Intent(mContext, PlayMusicActivity.class);
                 mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 6;
    }
    /**
      設置彈窗之後的背景變暗
         不要慌張,adapter裏面還是有辦法弄到getWindow()的
     */
     private void setBackgroudAlpha(float alpha){
         WindowManager.LayoutParams layoutParams = ((Activity)mContext).getWindow().getAttributes();
         layoutParams.alpha = alpha; //0.0-1.0
         ((Activity)mContext).getWindow().setAttributes(layoutParams);

     }
..........

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章