Custom Action View can't be clicked

后端 未结 3 1038
广开言路
广开言路 2020-12-25 13:49

I am trying to add a custom ActionView to my ActionBar.

I am trying to add the common refresh button. (ImageButton, Prog

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-25 14:40

    I have found a working solution for me and I want to share it with you. It's based on the first approach of (@Macarse), with some important changes.

    Important: adapt the initView method accordingly

    1. Set an onClickListener to the ImageView (mButton)

    2. Set loading to true & inform the Activity (MyActivity) about the click

      private void initView(final Context context) {
          final LayoutInflater inflator = LayoutInflater.from(context);
      
          final View actionView = inflator.inflate(
                  R.layout.action_refresh_progress, this);
          mProgressBar = (ProgressBar) actionView
                  .findViewById(R.id.action_refresh_progress);
      
          mButton = (ImageView) actionView
                  .findViewById(R.id.action_refresh_button);
      
          mButton.setOnClickListener(new OnClickListener() {
      
              @Override
              public void onClick(final View view) {
                  setLoading(true);
                  ((MyActivity) context).handleRefreshButtonClick();
              }
          });
      }
      
    3. React accordingly to the click in the Activity (MyActivity)

      public void handleRefreshButtonClick() {
          // Start refreshing...
      }
      

    I hope that my approach could save you some time finding a working solution!

提交回复
热议问题