ProgressBar in an ActionBar, like GMail app with Refresh

后端 未结 4 1834
情书的邮戳
情书的邮戳 2021-01-30 14:18

I would like to do the same thing than the GMail application on Honeycomb tablets. When you click on the Refresh button, the icon is replaced by a ProgressBar. How can I do this

4条回答
  •  庸人自扰
    2021-01-30 15:01

    Ok, I tried what Cailean suggested but it didn't work for me. Every time I want to revert indeterminate progress to the original button it becomes unclickable, I used this layout for the progress

    (actionbar_refresh_progress.xml)

    
    
    

    and this one to revert to the button

    (actionbar_refresh_button.xml)

    
    

    my code was:

    private void setRefreshing(boolean refreshing) {
            this.refreshing = refreshing;
            if(refreshMenuItem == null) return;
            View refreshView;
            LayoutInflater inflater = (LayoutInflater)getActionBar().getThemedContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            if(refreshing)
                refreshView = inflater.inflate(R.layout.actionbar_refresh_progress, null);
            else
                refreshView = inflater.inflate(R.layout.actionbar_refresh_button, null);
    
            refreshMenuItem.setActionView(refreshView);
        }
    

    After browsing the source of the Google IO app, especially this file: http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/ui/HomeActivity.java i found another easier way.

    Now I need only the first layout with progress and the working method looks like this:

    private void setRefreshing(boolean refreshing) {
        this.refreshing = refreshing;
        if(refreshMenuItem == null) return;
    
        if(refreshing)
            refreshMenuItem.setActionView(R.layout.actionbar_refresh_progress);
        else
            refreshMenuItem.setActionView(null);
    }
    

    Menu item definition:

    
    

    I hope someone finds this useful.

提交回复
热议问题