如何从弹出窗口

时间:2016-02-15 10:31:27

标签: android

我在每个recylcerview项目中都有popupwindow,但是我想根据recyclerview中的项目禁用popupwindow中的一些选项。 用户应该能够看到但无法点击。

     public void bind(Card addCard) {  // onBind method of recylerview
    popupWindow = getPopupWindow(addCard);
}

public void onClick(View v) {  // added onclick on image (item of recylcerview)

    if (popupWindow != null) {
        popupWindow.showAsDropDown(v, 0, 0);
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, -20, -10);
    }
}


public PopupWindow getPopupWindow(Card addCard) {

    String popUpContents[] = {"edit", "archive", "delete", "share"};

    final PopupWindow popupWindow = new PopupWindow(context);

    ListView listOptions = new ListView(context);
    listOptions.setDivider(null);
    listOptions.setDividerHeight(0);

    listOptions.setAdapter(listOptionsAdapter(popUpContents, addCard));

    listOptions.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Card card = (Card) view.getTag();

            switch (position) {
                case 0:
                    cardsFragment.editCard(card);
                    popupWindow.dismiss();
                    break;
                case 1:
                    cardsFragment.archiveCard(card);
                    popupWindow.dismiss();
                    break;
                case 2:
                    cardsFragment.deleteCard(card);
                    popupWindow.dismiss();
                    break;
                case 3:
                    cardsFragment.shareCard();
                    popupWindow.dismiss();
                    break;
            }
        }
    });

    // some other visual settings
    popupWindow.setFocusable(true);
    popupWindow.setWidth(250);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setOutsideTouchable(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        popupWindow.setElevation(10);
    }
    // set the list view as pop up window content
    popupWindow.setContentView(listOptions);
    Drawable d = new ColorDrawable(Color.WHITE);
    popupWindow.setBackgroundDrawable(d);
    return popupWindow;
}

private ArrayAdapter<String> listOptionsAdapter(final String listoptions[], final Card addCard) {

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listoptions) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            // setting the ID and text for every items in the list
            String item = getItem(position);

            // visual settings for the list item
            TextView listItem = new TextView(context);
            listItem.setTextColor(Color.BLACK);
            listItem.setText(item);
            listItem.setTag(addCard);
            listItem.setTextSize(20);
            if (position == 0) {
                listItem.setPadding(30, 20, 0, 20);
            } else {
                listItem.setPadding(30, 0, 0, 20);
            }
            if (addCard.isArchived) {
                if (position == 0 || position == 1 || position == 3) {
                    listItem.setEnabled(false);  // this one i have tried to disable click on certain items but not working
                }
            }

            return listItem;
        }

        @Override
        public int getCount() {
            return listoptions.length;
        }
    };

    return adapter;
}

对于Ex:如果存档卡我想要禁用&#34;编辑,存档,共享&#34;等选项以下代码我试过但没有工作:

if (addCard.isArchived) {
                if (position == 0 || position == 1 || position == 3) {
                    listItem.setEnabled(false);  // this one i have tried to disable click on certain items but not working
                }
            }

0 个答案:

没有答案