Skip to content

Commit

Permalink
Add a refresh button to the action bar.. and fix bad code
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekV committed Feb 2, 2014
1 parent d889da5 commit af0ff69
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
Binary file added res/drawable-hdpi/ic_action_refresh.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_action_refresh.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_action_refresh.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_action_refresh.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions res/menu/main_activity_actions.xml
Expand Up @@ -5,6 +5,10 @@
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item android:id="@+id/action_refresh"
android:title="@string/title_refresh"
android:icon="@drawable/ic_action_refresh"
android:showAsAction="ifRoom"/>
<item android:id="@+id/action_settings"
android:title="@string/button_settings_text" />
</menu>
1 change: 1 addition & 0 deletions res/values/strings.xml
Expand Up @@ -49,5 +49,6 @@
<string name="field_new_bookmark_tags_hint">Tags</string>
<string name="error_access_denied">access denied</string>
<string name="message_user_not_logged_in">Please enter your username and API key in the Settings</string>
<string name="title_refresh">refresh</string>

</resources>
30 changes: 25 additions & 5 deletions src/main/java/us/bmark/android/BookmarkListsActivity.java
Expand Up @@ -46,6 +46,7 @@ public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
};
private MultiRefreshStateObserver observer;
private BookmarkListsActivity.BookiePagerAdapter tabsPagerAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -114,9 +115,19 @@ public boolean onQueryTextChange(String newText) {
}
});

MenuItem refreshButton = menu.findItem(R.id.action_refresh);
refreshButton.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
refreshActiveFragment();
return true;
}
});

return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch( item.getItemId() ) {
Expand All @@ -139,12 +150,12 @@ private class MultiRefreshStateObserver

void add(BookmarkListFragment fragment) {
RefreshStateObservable refreshState = fragment.getRefreshState();
refreshState.addObserver(this);
observables.add(refreshState);
}

void activateObservation() {
for(RefreshStateObservable observable : observables) {
observables.add(observable);
observable.addObserver(this);
}
updateProgressBar();
}
Expand All @@ -168,19 +179,22 @@ public void onChanged() {
}

private void updateProgressBar() {
setProgressBarVisibility(isAnyInProgress());
setProgressBarIndeterminateVisibility(isAnyInProgress());
}
}



private class BookiePagerAdapter extends FragmentPagerAdapter {
final Fragment[] fragments = {mineFragment,allFragment,searchFragment};

private BookiePagerAdapter() {
super(getSupportFragmentManager());
}

public Fragment[] getFragments() {
return fragments;
}

@Override
public int getCount() {
return fragments.length;
Expand Down Expand Up @@ -210,7 +224,8 @@ private void createFragments() {
observer.add(allFragment);
observer.add(searchFragment);

pager.setAdapter(new BookiePagerAdapter());
tabsPagerAdapter = new BookiePagerAdapter();
pager.setAdapter(tabsPagerAdapter);

ActionBar actionBar = getActionBar();
for(int i = 0; i < titleIds.length; i++) {
Expand All @@ -220,6 +235,11 @@ private void createFragments() {
tab.setTabListener(tabListener);
actionBar.addTab(tab);
}
}

private void refreshActiveFragment() {
BookmarkListFragment activeFragment =
(BookmarkListFragment) tabsPagerAdapter.getFragments()[pager.getCurrentItem()];
activeFragment.refresh();
}
}

0 comments on commit af0ff69

Please sign in to comment.