今天终于认真的弄了一回自定义的弹出框,但是踩了两个坑,一个是自己粗心,一个是自己认识不够深。
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);
}
..........