move search view from activity to fragment by setting it as a list header, better reusability! Required for service activites

This commit is contained in:
Dominik Schürmann 2014-03-07 21:32:24 +01:00
parent 3572571391
commit a1be0d302b
4 changed files with 26 additions and 26 deletions

View File

@ -227,7 +227,7 @@ public class KeyListPublicFragment extends Fragment implements SearchView.OnQuer
// We have a menu item to show in action bar. // We have a menu item to show in action bar.
setHasOptionsMenu(true); setHasOptionsMenu(true);
// NOTE: Not supported by StickyListHeader, thus no indicator is shown while loading // NOTE: Not supported by StickyListHeader, but reimplemented here
// Start out with a progress indicator. // Start out with a progress indicator.
setListShown(false); setListShown(false);
@ -276,7 +276,7 @@ public class KeyListPublicFragment extends Fragment implements SearchView.OnQuer
mStickyList.setAdapter(mAdapter); mStickyList.setAdapter(mAdapter);
// NOTE: Not supported by StickyListHeader, thus no indicator is shown while loading // NOTE: Not supported by StickyListHeader, but reimplemented here
// The list should now be shown. // The list should now be shown.
if (isResumed()) { if (isResumed()) {
setListShown(true); setListShown(true);

View File

@ -30,7 +30,6 @@ import org.sufficientlysecure.keychain.provider.KeychainDatabase;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables; import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import org.sufficientlysecure.keychain.ui.adapter.SelectKeyCursorAdapter; import org.sufficientlysecure.keychain.ui.adapter.SelectKeyCursorAdapter;
import android.app.Activity;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseUtils; import android.database.DatabaseUtils;
import android.net.Uri; import android.net.Uri;
@ -41,6 +40,7 @@ import android.support.v4.content.Loader;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
@ -48,9 +48,7 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
LoaderManager.LoaderCallbacks<Cursor> { LoaderManager.LoaderCallbacks<Cursor> {
public static final String ARG_PRESELECTED_KEY_IDS = "preselected_key_ids"; public static final String ARG_PRESELECTED_KEY_IDS = "preselected_key_ids";
private Activity mActivity;
private SelectKeyCursorAdapter mAdapter; private SelectKeyCursorAdapter mAdapter;
private ListView mListView;
private EditText mSearchView; private EditText mSearchView;
private long mSelectedMasterKeyIds[]; private long mSelectedMasterKeyIds[];
private String mCurQuery; private String mCurQuery;
@ -72,8 +70,6 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mSearchView = (EditText) getActivity().findViewById(R.id.select_public_key_search);
mSearchView.addTextChangedListener(this);
mSelectedMasterKeyIds = getArguments().getLongArray(ARG_PRESELECTED_KEY_IDS); mSelectedMasterKeyIds = getArguments().getLongArray(ARG_PRESELECTED_KEY_IDS);
} }
@ -84,15 +80,20 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mActivity = getActivity(); getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView = getListView();
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// Give some text to display if there is no data. In a real // Give some text to display if there is no data. In a real
// application this would come from a resource. // application this would come from a resource.
setEmptyText(getString(R.string.list_empty)); setEmptyText(getString(R.string.list_empty));
mAdapter = new SelectKeyCursorAdapter(mActivity, null, 0, mListView, Id.type.public_key); // add header with search field
View headerView = getLayoutInflater(savedInstanceState)
.inflate(R.layout.select_public_key_fragment_header, null);
getListView().addHeaderView(headerView);
mSearchView = (EditText) getActivity().findViewById(R.id.select_public_key_search);
mSearchView.addTextChangedListener(this);
mAdapter = new SelectKeyCursorAdapter(getActivity(), null, 0, getListView(), Id.type.public_key);
setListAdapter(mAdapter); setListAdapter(mAdapter);
@ -111,11 +112,11 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
*/ */
private void preselectMasterKeyIds(long[] masterKeyIds) { private void preselectMasterKeyIds(long[] masterKeyIds) {
if (masterKeyIds != null) { if (masterKeyIds != null) {
for (int i = 0; i < mListView.getCount(); ++i) { for (int i = 0; i < getListView().getCount(); ++i) {
long keyId = mAdapter.getMasterKeyId(i); long keyId = mAdapter.getMasterKeyId(i);
for (int j = 0; j < masterKeyIds.length; ++j) { for (int j = 0; j < masterKeyIds.length; ++j) {
if (keyId == masterKeyIds[j]) { if (keyId == masterKeyIds[j]) {
mListView.setItemChecked(i, true); getListView().setItemChecked(i, true);
break; break;
} }
} }
@ -132,8 +133,8 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
// mListView.getCheckedItemIds() would give the row ids of the KeyRings not the master key // mListView.getCheckedItemIds() would give the row ids of the KeyRings not the master key
// ids! // ids!
Vector<Long> vector = new Vector<Long>(); Vector<Long> vector = new Vector<Long>();
for (int i = 0; i < mListView.getCount(); ++i) { for (int i = 0; i < getListView().getCount(); ++i) {
if (mListView.isItemChecked(i)) { if (getListView().isItemChecked(i)) {
vector.add(mAdapter.getMasterKeyId(i)); vector.add(mAdapter.getMasterKeyId(i));
} }
} }
@ -154,8 +155,8 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements T
*/ */
public String[] getSelectedUserIds() { public String[] getSelectedUserIds() {
Vector<String> userIds = new Vector<String>(); Vector<String> userIds = new Vector<String>();
for (int i = 0; i < mListView.getCount(); ++i) { for (int i = 0; i < getListView().getCount(); ++i) {
if (mListView.isItemChecked(i)) { if (getListView().isItemChecked(i)) {
userIds.add((String) mAdapter.getUserId(i)); userIds.add((String) mAdapter.getUserId(i));
} }
} }

View File

@ -4,16 +4,8 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_centerHorizontal="true" > android:layout_centerHorizontal="true" >
<EditText
android:id="@+id/select_public_key_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/menu_search"
android:drawableLeft="@drawable/ic_action_search"/>
<FrameLayout <FrameLayout
android:id="@+id/select_public_key_fragment_container" android:id="@+id/select_public_key_fragment_container"
android:layout_below="@id/select_public_key_search"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/select_public_key_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/menu_search"
android:drawableLeft="@drawable/ic_action_search" />