snapshot on the way to making studylists editable: add checkbox to xwlistitem. It's to be an alterative to long tap to select. Implement selectAll and deselectAll.

This commit is contained in:
Eric House 2014-03-19 20:42:29 -07:00
parent e7708ef439
commit aa6fbf8605
7 changed files with 167 additions and 29 deletions

View file

@ -13,6 +13,12 @@
android:background="@android:drawable/list_selector_background"
>
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<TextView android:id="@+id/text_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View file

@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/select_all"
android:title="@string/slmenu_select_all"
/>
<item android:id="@+id/deselect_all"
android:title="@string/slmenu_deselect_all"
/>
<item android:id="@+id/copy_all"
android:title="@string/slmenu_copy_all"
android:icon="@drawable/content_copy__gen"

View file

@ -2227,4 +2227,9 @@
app that supports pasting text, e.g. an email app.</string>
<string name="lookup_title">Word lookup</string>
<string name="slmenu_select_all">Select all</string>
<string name="slmenu_deselect_all">Deselect all</string>
<string name="sel_wordsf">Words: %d</string>
</resources>

View file

@ -83,7 +83,7 @@ public class DictsActivity extends XWExpandableListActivity
private static HashMap<MenuItem, DictAndLoc> s_itemData;
private String[] m_langs;
private String m_download;
private String m_downloadStr;
private ExpandableListView m_expView;
private String[] m_locNames;
private DictListAdapter m_adapter;
@ -129,9 +129,8 @@ public class DictsActivity extends XWExpandableListActivity
}
if ( null == view ) {
view = (XWListItem)
m_factory.inflate( R.layout.list_item, null );
view.setSelCB( DictsActivity.this );
view = XWListItem.inflate( DictsActivity.this,
DictsActivity.this );
int lang = (int)getGroupId( groupPosition );
DictAndLoc[] dals = DictLangCache.getDALsHaveLang( m_context,
@ -146,7 +145,7 @@ public class DictsActivity extends XWExpandableListActivity
view.setComment( m_locNames[loc.ordinal()] );
view.cache( loc );
} else {
view.setText( m_download );
view.setText( m_downloadStr );
}
addToCache( groupPosition, childPosition, view );
@ -392,7 +391,7 @@ public class DictsActivity extends XWExpandableListActivity
m_factory = LayoutInflater.from( this );
m_download = getString( R.string.download_dicts );
m_downloadStr = getString( R.string.download_dicts );
setContentView( R.layout.dict_browse );
m_expView = getExpandableListView();

View file

@ -697,8 +697,7 @@ public class GameConfig extends XWActivity
boolean localGame = localOnlyGame();
for ( int ii = 0; ii < names.length; ++ii ) {
final XWListItem view
= (XWListItem)factory.inflate( R.layout.list_item, null );
final XWListItem view = XWListItem.inflate( this, null );
view.setPosition( ii );
view.setText( names[ii] );
if ( localGame && m_gi.players[ii].isLocal ) {
@ -837,8 +836,7 @@ public class GameConfig extends XWActivity
LinearLayout phoneList =
(LinearLayout)findViewById(R.id.sms_phones);
for ( CommsAddrRec addr : m_remoteAddrs ) {
XWListItem item =
(XWListItem)Utils.inflate( this, R.layout.list_item );
XWListItem item = XWListItem.inflate( this, null );
item.setText( addr.sms_phone );
String name = Utils.phoneToContact( this, addr.sms_phone,
false );

View file

@ -19,6 +19,7 @@
package org.eehouse.android.xw4;
import android.view.ViewGroup;
import android.widget.ListView;
import android.app.AlertDialog;
import android.content.Context;
@ -34,13 +35,16 @@ import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.Arrays;
import java.util.HashSet;
import junit.framework.Assert;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.jni.GameSummary;
public class StudyList extends XWListActivity
implements OnItemSelectedListener {
implements OnItemSelectedListener, SelectableItem {
public static final int NO_LANG = -1;
@ -50,7 +54,11 @@ public class StudyList extends XWListActivity
private View m_pickView; // LinearLayout, actually
private int[] m_langCodes;
private String[] m_words;
private int m_position;
private HashSet<Integer> m_checkeds;
private int m_langPosition;
private SLWordsAdapter<String> m_adapter;
private ListView m_list;
private CharSequence m_origTitle;
@Override
protected void onCreate( Bundle savedInstanceState )
@ -58,9 +66,11 @@ public class StudyList extends XWListActivity
super.onCreate( savedInstanceState );
setContentView( R.layout.studylist );
m_list = (ListView)findViewById( android.R.id.list );
m_spinner = (Spinner)findViewById( R.id.pick_lang_spinner );
m_pickView = findViewById( R.id.pick_lang );
initOrFinish( getIntent() );
}
@ -90,6 +100,20 @@ public class StudyList extends XWListActivity
showConfirmThen( R.string.confirm_studylist_clear,
Action.SL_CLEAR_ACTION );
break;
case R.id.select_all:
for ( int ii = 0; ii < m_words.length; ++ii ) {
m_checkeds.add( ii );
}
makeAdapter();
setTitleBar();
break;
case R.id.deselect_all:
m_checkeds.clear();
makeAdapter();
setTitleBar();
break;
default:
handled = false;
}
@ -105,7 +129,7 @@ public class StudyList extends XWListActivity
if ( AlertDialog.BUTTON_POSITIVE == which ) {
switch ( action ) {
case SL_CLEAR_ACTION:
DBUtils.studyListClear( this, m_langCodes[m_position] );
DBUtils.studyListClear( this, m_langCodes[m_langPosition] );
initOrFinish( null );
break;
case SL_COPY_ACTION:
@ -127,7 +151,7 @@ public class StudyList extends XWListActivity
public void onListItemClick( ListView lv, View view, int position, long id )
{
String[] words = { m_words[position] };
launchLookup( words, m_langCodes[m_position], true );
launchLookup( words, m_langCodes[m_langPosition], true );
}
//////////////////////////////////////////////////
@ -136,30 +160,60 @@ public class StudyList extends XWListActivity
public void onItemSelected( AdapterView<?> parent, View view,
int position, long id )
{
m_position = position;
loadList();
m_langPosition = position;
loadList(); // because language has changed
}
public void onNothingSelected( AdapterView<?> parent )
{
}
//////////////////////////////////////////////////
// SelectableItem interface
//////////////////////////////////////////////////
public void itemClicked( SelectableItem.LongClickHandler clicked,
GameSummary summary )
{
Assert.assertTrue( clicked instanceof XWListItem );
m_checkeds.add( ((XWListItem)clicked).getPosition() );
}
public void itemToggled( SelectableItem.LongClickHandler toggled,
boolean selected )
{
Assert.assertTrue( toggled instanceof XWListItem );
int position = ((XWListItem)toggled).getPosition();
if ( selected ) {
m_checkeds.add( position );
} else {
m_checkeds.remove( position );
}
setTitleBar();
}
public boolean getSelected( SelectableItem.LongClickHandler obj )
{
Assert.assertTrue( obj instanceof XWListItem );
return m_checkeds.contains( ((XWListItem)obj).getPosition() );
}
private void loadList()
{
int lang = m_langCodes[m_position];
int lang = m_langCodes[m_langPosition];
m_words = DBUtils.studyListWords( this, lang );
ArrayAdapter<String> adapter = new ArrayAdapter<String>
( this, android.R.layout.simple_list_item_1 );
for ( String word : m_words ) {
adapter.add( word );
}
// adapter.sort();
m_checkeds = new HashSet<Integer>();
setListAdapter( adapter );
makeAdapter();
String langName = DictLangCache.getLangNames( this )[lang];
String title = getString( R.string.studylist_titlef, langName );
setTitle( title );
m_origTitle = getString( R.string.studylist_titlef, langName );
setTitleBar();
}
private void makeAdapter()
{
m_adapter = new SLWordsAdapter<String>( this, 0, m_words );
setListAdapter( m_adapter );
}
private void initOrFinish( Intent startIntent )
@ -169,7 +223,7 @@ public class StudyList extends XWListActivity
finish();
} else if ( 1 == m_langCodes.length ) {
m_pickView.setVisibility( View.GONE );
m_position = 0;
m_langPosition = 0;
loadList();
} else {
int startLang = NO_LANG;
@ -202,6 +256,19 @@ public class StudyList extends XWListActivity
}
}
private void setTitleBar()
{
CharSequence newTitle;
int nSels = m_checkeds.size();
DbgUtils.logf( "setTitleBar: nSels=%d", nSels );
if ( 0 == nSels ) {
newTitle = m_origTitle;
} else {
newTitle = getString( R.string.sel_wordsf, nSels );
}
setTitle( newTitle );
}
public static void launchOrAlert( Context context, int lang,
DlgDelegate.HasDlgDelegate dlg )
{
@ -225,4 +292,29 @@ public class StudyList extends XWListActivity
}
}
private class SLWordsAdapter<String> extends ArrayAdapter<String> {
// public SLWordsAdapter()
// {
// super( m_words.length );
// }
public SLWordsAdapter( Context context, int ignored, String[] strings) {
super( context, ignored, strings );
}
public SLWordsAdapter( Context context, int resource ) {
super( context, resource );
}
public View getView( int position, View convertView,
ViewGroup parent ) {
XWListItem item = XWListItem.inflate( StudyList.this, StudyList.this );
item.setPosition( position );
item.setText( m_words[position] );
item.setSelected( m_checkeds.contains(position) );
DbgUtils.logf( "getView(position=%d) => %H", position, item );
return item;
}
}
}

View file

@ -28,10 +28,12 @@ import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CheckBox;
import junit.framework.Assert;
public class XWListItem extends LinearLayout
implements SelectableItem.LongClickHandler {
implements SelectableItem.LongClickHandler, View.OnClickListener {
private int m_position;
private Context m_context;
private Object m_cached;
@ -39,6 +41,7 @@ public class XWListItem extends LinearLayout
private Drawable m_origDrawable;
private boolean m_selected = false;
private SelectableItem m_selCb;
private CheckBox m_checkbox;
public interface DeleteCallback {
void deleteCalled( XWListItem item );
@ -49,6 +52,15 @@ public class XWListItem extends LinearLayout
m_context = cx;
}
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
m_checkbox = (CheckBox)findViewById( R.id.checkbox );
Assert.assertNotNull( m_checkbox );
m_checkbox.setOnClickListener( this );
}
public int getPosition() { return m_position; }
public void setPosition( int indx ) { m_position = indx; }
@ -86,9 +98,10 @@ public class XWListItem extends LinearLayout
button.setVisibility( View.VISIBLE );
}
public void setSelCB( SelectableItem selCB )
private void setSelCB( SelectableItem selCB )
{
m_selCb = selCB;
m_checkbox.setVisibility( null == selCB ? View.GONE : View.VISIBLE );
}
public void setSelected( boolean selected )
@ -129,6 +142,14 @@ public class XWListItem extends LinearLayout
toggleSelected();
}
// View.OnClickListener interface
public void onClick( View view )
{
if ( m_checkbox == view ) {
setSelected( m_checkbox.isChecked() );
}
}
private void toggleSelected()
{
m_selected = !m_selected;
@ -138,6 +159,15 @@ public class XWListItem extends LinearLayout
} else {
setBackgroundDrawable( m_origDrawable );
}
m_checkbox.setChecked( m_selected );
m_selCb.itemToggled( this, m_selected );
}
public static XWListItem inflate( Context context, SelectableItem selCB )
{
XWListItem item = (XWListItem)Utils.inflate( context, R.layout.list_item );
item.setSelCB( selCB );
return item;
}
}