@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// 彈窗
// contentView-顯示的View, width, height-寬高
/* TextView contentView = new TextView(mContext);
contentView.setText("彈窗");
contentView.setTextSize(20);
contentView.setBackgroundColor(Color.RED);*/
View contentView = View.inflate(mContext, R.layout.pop_window_layout, null);
final PopupWindow window = new PopupWindow(contentView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//設(shè)置默認(rèn)只顯示一個(gè)彈窗
window.setBackgroundDrawable(new ColorDrawable());
window.setFocusable(true);
window.setOutsideTouchable(true);//設(shè)置外圍點(diǎn)擊
//設(shè)置動(dòng)畫(huà)樣式
window.setAnimationStyle(R.style.pop_anim);
//顯示
//window.showAsDropDown(view);//在哪個(gè)控件下面顯示
//window.showAsDropDown(view, 80, -60);
AppBean bean = mListDatas.get(position);
final String packageName = bean.packageName;
//parent是popupWindow要在哪個(gè)夫容器里面展示
window.showAtLocation(parent, Gravity.CENTER, 0, 0);//顯示在指定位置
TextView tvUninstall = (TextView) contentView.findViewById(R.id.tv_uninstall);
TextView tvOpen = (TextView) contentView.findViewById(R.id.tv_open);
TextView tvShare = (TextView) contentView.findViewById(R.id.tv_share);
TextView tvInfo = (TextView) contentView.findViewById(R.id.tv_info);
//判斷顯示或者隱藏
tvUninstall.setVisibility(bean.isSystem ? View.GONE : View.VISIBLE);
PackageManager pkgMgr = getPackageManager();
final Intent intent = pkgMgr.getLaunchIntentForPackage(packageName);
tvOpen.setVisibility(intent == null ? View.GONE : View.VISIBLE);
tvUninstall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//實(shí)現(xiàn)卸載功能
/* <intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.DELETE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="package" />
</intent-filter>*/
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setAction("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
//startActivityForResult(intent, requestCode)不能實(shí)現(xiàn)監(jiān)聽(tīng)卸載完成
window.dismiss();
}
});
window.dismiss();
public class AppBean {
public Drawable icon;//圖標(biāo)
public String name;
public boolean isInstallSD;//是否安裝在SD卡
public String space;//應(yīng)用大小
public boolean isSystem;//是否為系統(tǒng)應(yīng)用
public String packageName;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pop_bg"
android:orientation="horizontal"
android:padding="12dp" >
<TextView
android:id="@+id/tv_uninstall"
style="@style/TextNormal"
android:drawablePadding="4dp"
android:drawableTop="@drawable/ic_uninstall"
android:text="卸載" />
<TextView
android:id="@+id/tv_open"
style="@style/TextNormal"
android:layout_marginLeft="4dp"
android:drawablePadding="4dp"
android:drawableTop="@drawable/ic_open"
android:text="打開(kāi)" />
<TextView
android:id="@+id/tv_share"
style="@style/TextNormal"
android:layout_marginLeft="4dp"
android:drawablePadding="4dp"
android:drawableTop="@drawable/ic_share"
android:text="分享" />
<TextView
android:id="@+id/tv_info"
style="@style/TextNormal"
android:layout_marginLeft="4dp"
android:drawablePadding="4dp"
android:drawableTop="@drawable/ic_info"
android:text="信息" />
</LinearLayout>
<style name="pop_anim" parent="@android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">@anim/pop_enter</item>
<item name="android:windowExitAnimation">@anim/pop_exit</item>
</style>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@interpolator/overshoot" >
<translate
android:duration="250"
android:fromXDelta="100%"
android:toXDelta="0" />
</set>
<!-- Animation for when a dock window at the bottom of the screen is entering. -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@interpolator/anticipate" >
<translate
android:duration="250"
android:fromXDelta="0"
android:toXDelta="100%" />
</set>
TWO
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.pop_login, null);
// View v = View.inflate(getContext(),R.layout.pop_login, null);
popupWindow = new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setFocusable(true);
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
popupWindow.setOutsideTouchable(true);
//設(shè)置漸入、漸出動(dòng)畫(huà)效果
popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popupWindow.dismiss();
return true;
}
return false;
}
});
// popupWindow.update();
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
// popupWindow.showAsDropDown(view,100,-150);
點(diǎn)擊事件中的view就是父容器
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.rl_touxiang_personalCenter:
showPopWindow(view);
break;
}
}