如何用list popupwindow替换我的popupmenu代码?

时间:2017-04-24 16:13:10

标签: android popup popupwindow popupmenu android-popupwindow

目前,我正在以编程方式创建一个弹出菜单,其中显示了一个楼层列表和一个标题。但是,更改标题的背景颜色并为标题添加关闭按钮变成了一场噩梦。

我想用列表弹出窗口替换这个弹出菜单,这样我就可以添加一个带有背景属性的XML文件,其中黑色作为背景,右侧的关闭按钮和菜单中项目的白色背景。有没有办法用列表弹出窗口实现这个目的?这是我的代码:

private void floorMenu(ImageView btnFloorMenu){
    MapData data = new MapDao(MyPlugin.mapId);
    final List<Floor> flList = dao.getFloors();
    // set popupMenu
    final PopupMenu floorsPm = new PopupMenu(MapViewActivity.this,btnFloorMenu);

    MenuItem titleItem =  floorsPm.getMenu().add(Menu.NONE, Menu.NONE, Menu.NONE, "Floors");

    int i = 1;
    for(Floor fl : flList)
    {
        floorsPm.getMenu().add(Menu.NONE, i,i, fl.getName());
        if(i>3)
            break;
        i++;
    }


    // add popup listener
    floorsPm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        //    onClick
        @Override
        public boolean onMenuItemClick(MenuItem item){
            //       get floorname

            int flOrder = item.getOrder();
            if(flOrder == Menu.NONE )
                return true;
            flOrder--;

            final String floorId = flList.get(flOrder).getMapId();

            //       set camera to floor

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    floorsPm.dismiss();
                    mapFragment.getMapManager().setCameraLayer(floorId, false);
                     Log.d(TAG, "post cameraLayer set");
                    changedSteps = true;
                    pauseNav();

                }
            });




            return true;
        }

    });
    floorsPm.show();


}

2 个答案:

答案 0 :(得分:2)

以下是创建展示ListPopupWindow

的示例

首先,为item_list_popup_window的每个项目创建布局ListPopupWindow

<?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:background="#e4e4e4"
    android:paddingTop="1dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete" />

</LinearLayout>

其次,为您的Adapter创建ListPopupWindow

public class ListPopupWindowAdapter extends BaseAdapter{
    private Activity mActivity;
    private List<String> mDataSource = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private OnClickDeleteButtonListener clickDeleteButtonListener;

    ListPopupWindowAdapter(Activity activity, List<String> dataSource, @NonNull OnClickDeleteButtonListener clickDeleteButtonListener){
        this.mActivity = activity;
        this.mDataSource = dataSource;
        layoutInflater = mActivity.getLayoutInflater();
        this.clickDeleteButtonListener = clickDeleteButtonListener;
    }

    @Override
    public int getCount() {
        return mDataSource.size();
    }

    @Override
    public String getItem(int position) {
        return mDataSource.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_list_popup_window, null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.text_title);
            holder.btnDelete = (Button) convertView.findViewById(R.id.button_delete);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        // bind data
        holder.tvTitle.setText(getItem(position));
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickDeleteButtonListener.onClickDeleteButton(position);
            }
        });
        return convertView;
    }

    public class ViewHolder{
        private TextView tvTitle;
        private Button btnDelete;
    }

    // interface to return callback to activity
    public interface OnClickDeleteButtonListener{
        void onClickDeleteButton(int position);
    }
}

第三,您为创建和展示ListPopupWindow

创建了一个功能
private void showListPopupWindow(View anchorView) {
    final ListPopupWindow listPopupWindow = new ListPopupWindow(this);
    listPopupWindow.setWidth(600);
    List<String> sampleData = new ArrayList<>();
    sampleData.add("A");
    sampleData.add("B");
    sampleData.add("CCCCCCCCCCCCCC");
    sampleData.add("D");
    sampleData.add("EEEEEEEEE");

    listPopupWindow.setAnchorView(anchorView);
    ListPopupWindowAdapter listPopupWindowAdapter = new ListPopupWindowAdapter(this, sampleData, new ListPopupWindowAdapter.OnClickDeleteButtonListener() {
        @Override
        public void onClickDeleteButton(int position) {
            Toast.makeText(MainActivity.this, "Click delete " + position, Toast.LENGTH_SHORT).show();
            listPopupWindow.dismiss();
        }
    });
    listPopupWindow.setAdapter(listPopupWindowAdapter);
    listPopupWindow.show();
}

最后,您可以通过

显示ListPopupWindow
showListPopupWindow(v);

例如,如果要在单击按钮

时显示它
anyButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showListPopupWindow(v);
    }
});

enter image description here

Full Demo is here

答案 1 :(得分:0)

请尝试使用此代码,也许您不会喜欢这个

private void floorMenu(ImageView btnFloorMenu){
final Dialog customDialog = new Dialog(this);
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.item_dialog_coustom_design);

TextView clickItem = (TextView)customDialog.findViewById(R.id.item_click);
TextView clickItem1 = (TextView)customDialog.findViewById(R.id.item_click1);
TextView clickItem2 = (TextView)customDialog.findViewById(R.id.item_click2);
Button btnClose = (Button)customDialog.findViewById(R.id.btn_close);

clickItem.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
        // wright your Button Action
    }
});

clickItem1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
        // wright your Button Action
    }
});

clickItem2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
        // wright your Button Action
    }
});

btnClose.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        customDialog.dismiss();
    }
});
customDialog.show();

}

创建Linearlayout layout_width =“280dp”layout_height =“wrap_content”         android:orientation =“vertical”文件名item_dialog_coustom_design.xml然后把这段代码

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Your Title"
        android:background="#000"
        android:textColor="#fff"
        android:padding="12dp"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="#fff"
        android:padding="10dp"
        android:text="Your Item"
        android:id="@+id/item_click"
        android:textSize="16sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="#fff"
        android:padding="10dp"
        android:text="Your Item"
        android:id="@+id/item_click1"
        android:textSize="16sp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="#fff"
        android:padding="10dp"
        android:text="Your Item"
        android:id="@+id/item_click2"
        android:textSize="16sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:background="#fff"
        android:gravity="right">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btn_close"
                android:text="Close"/>
    </LinearLayout>