如何点击片段中的后退按钮关闭警报对话框?

时间:2016-09-14 09:27:06

标签: android listview adapter alertdialog

我有一个警报对话框。我点击列表项目显示警报对话。工作正常。但是当我按下后退按钮时,警报对话框不会被解雇并显示在活动中。所以当我点击时如何解除警报对话框手机的后退按钮。

这是我的适配器类: -

public class PgListAdapter extends RecyclerView.Adapter<PgListAdapter.FaqViewHolder> {

    private Context mContext;
    private List<Faq> faqList1;
    private ValueAnimator mAnimator;
    private int updatePosition = -1;
    private int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    private int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    private Faq ci;
    private PopupWindow popupWindow;


    public PgListAdapter(Context context, List<Faq> faqList) {
        mContext = context;
        this.faqList1 = faqList;



    }

 @Override
    public FaqViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_pglist, parent, false);
        FaqViewHolder vh = new FaqViewHolder(v);

        return vh;


    }

    @Override
    public void onBindViewHolder(final FaqViewHolder holder, int position) {
        ci = faqList1.get(position);
        holder.questionTv.setText(ci.getQuestion());
        holder.answerTextView.setText(ci.getAnswer());
        holder.Phone.setText(ci.getPhone());
        holder.pgAddress.setText(ci.getAddress());


        holder.ll_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



          showFilter();


            }
        });


        holder.Phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + holder.Phone.getText()));
                mContext.startActivity(intent);

            }
        });


        if (ci.isPGFull()) {
            holder.pgstatus.setVisibility(View.VISIBLE);
        } else {
            holder.pgstatus.setVisibility(View.INVISIBLE);
        }
    }


    @Override
    public int getItemCount() {
        return faqList1 == null ? 0 : faqList1.size();
    }





    class FaqViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout faqItemView;
        private RelativeLayout extendedView;
        private TextView questionTv, Phone;
        private TextView answerTextView, pgstatus, pgAddress;
        private LinearLayout ll_top;
        private LinearLayout ll_layout1;
        private ImageView arrowImgView;
        private int holderPosition;

        public FaqViewHolder(View itemView) {
            super(itemView);
            Phone = (TextView) itemView.findViewById(R.id.pgPhoneNo1);
            questionTv = (TextView) itemView.findViewById(R.id.pgOwner);
            answerTextView = (TextView) itemView.findViewById(R.id.pgPhoneNo);
            pgstatus = (TextView) itemView.findViewById(R.id.pgstatus);
            pgAddress = (TextView) itemView.findViewById(R.id.pgAddress);
            ll_top = (LinearLayout) itemView.findViewById(R.id.ll_top);
            ll_layout1 = (LinearLayout) itemView.findViewById(R.id.ll_layout1);




        }
    }


 public void showFilter() {
        View popupView = LayoutInflater.from(mContext).inflate(R.layout.pglist_popup, null);
        popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);


        final TextView tv_phone = (TextView) popupView.findViewById(R.id.tv_phone);
        final LinearLayout ll_layout = (LinearLayout) popupView.findViewById(R.id.ll_layout);
        final LinearLayout ll_top = (LinearLayout) popupView.findViewById(R.id.ll_top);
        TextView tv_address = (TextView) popupView.findViewById(R.id.tv_address);
        final TextView btn_ok = (TextView) popupView.findViewById(R.id.btn_ok);

        // popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(false);


        tv_phone.setText(ci.getPhone());
        tv_address.setText(ci.getAddress());
        tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + tv_phone.getText()));
                mContext.startActivity(intent);


            }
        });


        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                popupWindow = null;

            }
        });




        popupWindow.showAsDropDown(popupView, 0, 0);




    }

}

这是我的PgList类: -

public class PgList extends Fragment implements WebServiceListener {


    private String responseCodeCameFromServer = "null";
    private String responseCameFromServer, responseMessageCameFromServer;
    private RecyclerView rvFaqs;
    private List<Faq> faqList = new ArrayList<>();
    private List<NameValuePair> basicNameValuePair;
    private GeneralUtilities generalUtilities;
    private SharedPreferencesUtilities sharedPreferencesUtilities;
    PgListAdapter pgAdapter;
    private View rootView;
    private TextView empty_view;
    ProgressDialog br;
    private PgList pgList;


    List<String> typelist = new ArrayList<String>();
    List<String> citylist = new ArrayList<String>();
    List<CityAreaDetails> temp = new ArrayList<>();

    Spinner type;
    WebServiceHandler webServiceHandler;


    public PgList() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        if (rootView == null) {
            rootView = inflater.inflate(R.layout.pglist, container, false);

            generalUtilities = new GeneralUtilities(getActivity());
            sharedPreferencesUtilities = new SharedPreferencesUtilities(getActivity());
            br = new ProgressDialog(getContext());
            br.setTitle("Loading...");
            br.setCancelable(false);


            empty_view = (TextView) rootView.findViewById(R.id.tv_empty_view);

            empty_view.setText("Sorry for the Inconvience! We will update the list very soon for this Area. ");
            getActivity().setTitle("PG/Room List");
            typelist.add("Boys");
            typelist.add("Girls");
            citylist.clear();
            temp.clear();


            rvFaqs = (RecyclerView) rootView.findViewById(R.id.overviewRv);
            rvFaqs.setHasFixedSize(true);
            rvFaqs.addItemDecoration(new SpacesItemDecoration(20, 20, 12));
            rvFaqs.setLayoutManager(new LinearLayoutManager(getActivity()));

             rvFaqs.setAdapter(new PgListAdapter(getActivity(), faqList));
            webServiceHandler = new WebServiceHandler(getActivity());
            webServiceHandler.webServiceListener = PgList.this;
            HashMap<String, String> formData = new HashMap<>();
            formData.put("Type", "Boys");
            formData.put("CityAreaDetailsID", "1");
            if (br != null) {
                br.show();
            }
            //Checking internet connectivity and then requesting to the server
            Log.e("", "Data :" + formData);
            if (generalUtilities.isConnected()) {
                webServiceHandler.requestToServer((getResources().getString(R.string.api_end_point)) + "CityDetails",
                        WebService.ORDER, formData, true);
            } else {
                if (br != null) {
                    br.dismiss();
                }
                generalUtilities.showAlertDialog("Error", getResources().getString(R.string.internet_error), "OK");
            }

        }




        return rootView;
    }

我的问题是,当我点击alertdialogue框的ok按钮时,它会关闭alertdialogue框。但是当我没有点击确定按钮(正面按钮)并按下后退按钮时,警告对话框显示在我正在后退按钮上的活动中。

2 个答案:

答案 0 :(得分:0)

getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {    
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){

                    popupWindow.dismiss();

                    return true;    
                }    
                return false;
            }
        });
        super.onResume();

您需要将此用于Fragment

中的膨胀列表项视图
for(...){
     View itemView.findViewHolderForAdapterPosition(index/position) 
     itemView.findViewById(R.id.ll_top).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       popupWindows.dismiss() 
                    }
                });
}

答案 1 :(得分:0)

如果你想在点击后退按钮时关闭弹出窗口,只需将此代码添加到你的片段类中(虽然不同的东西适用于不同的人):

var _isFunction2 = require('lodash/isFunction');

var _isFunction3 = _interopRequireDefault(_isFunction2);

var _isPlainObject2 = require('lodash/isPlainObject');

var _isPlainObject3 = _interopRequireDefault(_isPlainObject2);

var _assignIn2 = require('lodash/assignIn');

var _assignIn3 = _interopRequireDefault(_assignIn2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
...

如果您要关闭警报对话框,请执行以下操作:

popUp.setBackgroundDrawable(new BitmapDrawable());// it is most important piece of code

// For Key Listeners

View v = popUp.getContentView();

//Here assigning the key Listners
v.setOnKeyListener(this);

@Override   
public boolean onKey(View v, int keyCode, KeyEvent event) {

    if(keyCode == KeyEvent.KEYCODE_BACK) popUp.dismiss();

    return false;

}//Implements the KeyListener
 //and be careful we should implement "OnKeyListener"`