欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PopupWindow简单使用

程序员文章站 2022-05-31 10:55:08
...

在Android开发中PopupWindow是经常被使用到的,下面我们来实现以下PopupWindow,当然了,在开发中也可以封装,这样可以多次用到,小伙伴们加油。

1、效果如下:

PopupWindow简单使用
PopupWindow简单使用

2、代码如下:

public class InfoActivity extends AppCompatActivity{
      private PopupWindow popWindow;
      private CommonAdapter<String> authAdapter;
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_info);
      }
      @OnClick({ R.id.ll_user_auth, R.id.ll_user_flag})
      public void onViewClicked(View view) {
          case R.id.ll_user_auth:
            showAuthPopWin(view);
            break;
        case R.id.ll_user_flag:
            //showPopuWindowAlter(1, view);
            break;
      }
    private void showAuthPopWin(View v) {

      try {
        View view = LayoutInflater.from(this).inflate(R.layout.ly_type, null, false);
        RecyclerView recyclerPool = (RecyclerView) view.findViewById(R.id.recyclerType);
        LinearLayout hot_layout = (LinearLayout) view.findViewById(R.id.type_layout);
        recyclerPool.setLayoutManager(new LinearLayoutManager(this));

        List<String> stringList = new ArrayList<>();
        stringList.add("admin");
        stringList.add("user");
        stringList.add("manager");
        authAdapter = new CommonAdapter<String>(this, R.layout.item_rv_popu_layout,         stringList) {
            @Override
            protected void convert(ViewHolder holder, String string, int position) {

                holder.setText(R.id.tv_name, string);
            }
        };
        recyclerPool.setAdapter(authAdapter);
        if (authAdapter.getItemCount() > 5) {
            hot_layout.setLayoutParams(new  LinearLayout.LayoutParams(ConvertUtils.dip2px(this, 150), ConvertUtils.dip2px(getContext(), 100)));
        }
        UIUtils.post(() -> authAdapter.notifyDataSetChanged());

        popWindow = new PopupWindow(view, ConvertUtils.dip2px(this, 202), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setAnimationStyle(R.anim.anim_pop);
        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor((v2, event) -> {
            return false;
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popWindow.showAsDropDown(v, 0, 0);
        onSelectItemPopup(popWindow);
    } catch (Exception ex) {
        XLog.e("initPossiblePopWin:Exception Occurs " + ex.getMessage());
    }
}
} 
private void onSelectItemPopup(PopupWindow popWindow) {

    authAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            if (authAdapter.getDatas() == null) {
                if (popWindow != null) popWindow.dismiss();
                return;
            }
            tvUserAuth.setText(authAdapter.getDatas().get(position));
            if (popWindow != null) popWindow.dismiss();
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position){
            return false;
        }
    });
}

R.layout.ly_type 布局文件

  <?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="wrap_content"
          android:orientation="vertical">

  <LinearLayout
    android:id="@+id/type_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/poppup_style"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerType"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</LinearLayout>
</LinearLayout>

drawable/poppup_style文件

  <?xml version="1.0" encoding="UTF-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fff" />
    <stroke
      android:width="1dp"
      android:color="#BDC7D8" />
    <corners
      android:bottomLeftRadius="2dp"
      android:bottomRightRadius="2dp"
      android:topLeftRadius="2dp"
      android:topRightRadius="2dp" />
</shape>






      ![Screenshot_20171121-102834.png](http://upload-images.jianshu.io/upload_images/3012005-532c28352f7d291a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)