implement actionbar search for HC/ICS devices

This commit is contained in:
Andrew Dolgov 2012-01-19 13:05:48 +03:00
parent 8624583764
commit 60128ffe2f
4 changed files with 85 additions and 6 deletions

View File

@ -4,11 +4,13 @@
<group android:id="@+id/menu_group_feeds" > <group android:id="@+id/menu_group_feeds" >
<!-- <item <!--
<item
android:id="@+id/back_to_categories" android:id="@+id/back_to_categories"
android:icon="@android:drawable/ic_menu_close_clear_cancel" android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:showAsAction="" android:showAsAction=""
android:title="@string/back_to_categories"/> --> android:title="@string/back_to_categories"/>
-->
<item <item
android:id="@+id/show_feeds" android:id="@+id/show_feeds"
@ -42,6 +44,13 @@
android:icon="@drawable/ic_menu_cloud" android:icon="@drawable/ic_menu_cloud"
android:title="@string/go_offline"/> android:title="@string/go_offline"/>
<item
android:id="@+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:title="@string/search"/>
<item <item
android:id="@+id/headlines_mark_as_read" android:id="@+id/headlines_mark_as_read"
android:icon="@drawable/ic_menu_tick" android:icon="@drawable/ic_menu_tick"
@ -117,7 +126,6 @@
android:id="@+id/set_labels" android:id="@+id/set_labels"
android:icon="@drawable/ic_menu_marked" android:icon="@drawable/ic_menu_marked"
android:title="@string/article_set_labels"/> android:title="@string/article_set_labels"/>
</group> </group>
</group> </group>

View File

@ -115,4 +115,5 @@
<string name="offline_image_cache_enabled_summary">Download images to sdcard. This might significantly increase time it takes to go offline.</string> <string name="offline_image_cache_enabled_summary">Download images to sdcard. This might significantly increase time it takes to go offline.</string>
<string name="notify_downloading_images">Downloading images (%1$d)...</string> <string name="notify_downloading_images">Downloading images (%1$d)...</string>
<string name="article_set_labels">Set labels</string> <string name="article_set_labels">Set labels</string>
<string name="search">Search</string>
</resources> </resources>

View File

@ -58,6 +58,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
private boolean m_refreshInProgress = false; private boolean m_refreshInProgress = false;
private boolean m_canLoadMore = false; private boolean m_canLoadMore = false;
private boolean m_combinedMode = true; private boolean m_combinedMode = true;
private String m_searchQuery = "";
private SharedPreferences m_prefs; private SharedPreferences m_prefs;
@ -110,6 +111,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
m_selectedArticles = savedInstanceState.getParcelable("selectedArticles"); m_selectedArticles = savedInstanceState.getParcelable("selectedArticles");
m_canLoadMore = savedInstanceState.getBoolean("canLoadMore"); m_canLoadMore = savedInstanceState.getBoolean("canLoadMore");
m_combinedMode = savedInstanceState.getBoolean("combinedMode"); m_combinedMode = savedInstanceState.getBoolean("combinedMode");
m_searchQuery = savedInstanceState.getString("searchQuery", "");
} }
View view = inflater.inflate(R.layout.headlines_fragment, container, false); View view = inflater.inflate(R.layout.headlines_fragment, container, false);
@ -201,6 +203,12 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
put("skip", String.valueOf(fskip)); put("skip", String.valueOf(fskip));
if (isCat) put("is_cat", "true"); if (isCat) put("is_cat", "true");
if (m_searchQuery.length() != 0) {
put("search", m_searchQuery);
put("search_mode", "");
put("match_on", "both");
}
} }
}; };
@ -217,6 +225,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
out.putParcelable("selectedArticles", m_selectedArticles); out.putParcelable("selectedArticles", m_selectedArticles);
out.putBoolean("canLoadMore", m_canLoadMore); out.putBoolean("canLoadMore", m_canLoadMore);
out.putBoolean("combinedMode", m_combinedMode); out.putBoolean("combinedMode", m_combinedMode);
out.putString("searchQuery", m_searchQuery);
} }
public void setLoadingStatus(int status, boolean showProgress) { public void setLoadingStatus(int status, boolean showProgress) {
@ -562,5 +571,12 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
return m_adapter.getPosition(article); return m_adapter.getPosition(article);
} }
public void setSearchQuery(String query) {
if (!m_searchQuery.equals(query)) {
m_searchQuery = query;
refresh(false);
}
}
} }

View File

@ -31,6 +31,7 @@ import android.preference.PreferenceManager;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SearchViewCompat;
import android.util.Log; import android.util.Log;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Display; import android.view.Display;
@ -43,6 +44,7 @@ import android.view.ViewGroup;
import android.view.animation.AnimationUtils; import android.view.animation.AnimationUtils;
import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -1201,8 +1203,52 @@ public class MainActivity extends FragmentActivity implements OnlineServices {
} else if (m_selectedArticle != null) { } else if (m_selectedArticle != null) {
m_menu.setGroupVisible(R.id.menu_group_article, true); m_menu.setGroupVisible(R.id.menu_group_article, true);
} else if (m_activeFeed != null || m_activeCategory != null) { } else if (m_activeFeed != null) {
m_menu.setGroupVisible(R.id.menu_group_headlines, true); m_menu.setGroupVisible(R.id.menu_group_headlines, true);
MenuItem search = m_menu.findItem(R.id.search);
if (search != null && !m_compatMode) {
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
private String query = "";
@Override
public boolean onQueryTextSubmit(String query) {
Log.d(TAG, "Search/onQueryTextSubmit");
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
.findFragmentById(R.id.headlines_fragment);
if (frag != null) {
frag.setSearchQuery(query);
this.query = query;
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
Log.d(TAG, "Search/onQueryTextChange: " + newText);
if (newText.equals("") && !newText.equals(this.query)) {
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
.findFragmentById(R.id.headlines_fragment);
if (frag != null) {
frag.setSearchQuery(newText);
this.query = newText;
}
}
return false;
}
});
}
} else { } else {
m_menu.setGroupVisible(R.id.menu_group_feeds, true); m_menu.setGroupVisible(R.id.menu_group_feeds, true);
} }
@ -1422,6 +1468,14 @@ public class MainActivity extends FragmentActivity implements OnlineServices {
} }
if (!append) { if (!append) {
MenuItem search = m_menu.findItem(R.id.search);
if (search != null && !m_compatMode) {
SearchView sv = (SearchView) search.getActionView();
sv.setQuery("", false);
}
HeadlinesFragment hf = new HeadlinesFragment(); HeadlinesFragment hf = new HeadlinesFragment();
FragmentTransaction ft = getSupportFragmentManager() FragmentTransaction ft = getSupportFragmentManager()