From 84698273a54424d37424a7d60a151a839eeda5c2 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 26 Mar 2014 21:03:51 -0700 Subject: [PATCH] fix selection of group item by adding a delegate all selectable items can call. --- .../XWords4/res/layout/game_list_group.xml | 39 +++++++++---- .../eehouse/android/xw4/DrawSelDelegate.java | 55 +++++++++++++++++++ .../eehouse/android/xw4/ExpiringDelegate.java | 11 +--- .../eehouse/android/xw4/ExpiringTextView.java | 7 --- .../eehouse/android/xw4/GameListGroup.java | 40 +++++++++++++- .../org/eehouse/android/xw4/GameListItem.java | 10 +--- .../org/eehouse/android/xw4/XWListItem.java | 13 ++--- 7 files changed, 130 insertions(+), 45 deletions(-) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/DrawSelDelegate.java diff --git a/xwords4/android/XWords4/res/layout/game_list_group.xml b/xwords4/android/XWords4/res/layout/game_list_group.xml index 7ab68cff1..41f6337a0 100644 --- a/xwords4/android/XWords4/res/layout/game_list_group.xml +++ b/xwords4/android/XWords4/res/layout/game_list_group.xml @@ -1,15 +1,30 @@ + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="horizontal" + android:background="#FF7F7F7F" + > + + + + + + diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DrawSelDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DrawSelDelegate.java new file mode 100644 index 000000000..6813497f7 --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DrawSelDelegate.java @@ -0,0 +1,55 @@ +/* -*- compile-command: "find-and-ant.sh debug install"; -*- */ +/* + * Copyright 2009-2014 by Eric House (xwords@eehouse.org). All + * rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.eehouse.android.xw4; + +import android.graphics.drawable.Drawable; +import android.view.View; + +public class DrawSelDelegate { + private View m_view; + private Drawable m_origDrawable; + + protected DrawSelDelegate( View view ) + { + m_view = view; + m_origDrawable = view.getBackground(); + } + + protected void showSelected( boolean selected, Drawable selDrawable ) + { + if ( selected ) { + m_origDrawable = m_view.getBackground(); + if ( null == selDrawable ) { + m_view.setBackgroundColor( XWApp.SEL_COLOR ); + } else { + m_view.setBackgroundDrawable( selDrawable ); + } + } else { + m_view.setBackgroundDrawable( m_origDrawable ); + } + } + + protected void showSelected( boolean selected ) + { + showSelected( selected, null ); + } + +} diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringDelegate.java index 430800771..afc61e6d7 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringDelegate.java @@ -52,12 +52,12 @@ public class ExpiringDelegate { private long m_startSecs; private Runnable m_runnable = null; private boolean m_selected; - private Drawable m_origDrawable; // these can be static as drawing's all in same thread. private static Rect s_rect; private static Paint s_paint; private static float[] s_points; private static Drawable s_selDrawable; + private DrawSelDelegate m_dsdel; static { s_rect = new Rect(); @@ -72,7 +72,7 @@ public class ExpiringDelegate { { m_context = context; m_view = view; - m_origDrawable = view.getBackground(); + m_dsdel = new DrawSelDelegate( view ); } public void setHandler( Handler handler ) @@ -101,12 +101,7 @@ public class ExpiringDelegate { public void setSelected( boolean selected ) { m_selected = selected; - if ( selected ) { - m_origDrawable = m_view.getBackground(); - m_view.setBackgroundDrawable( s_selDrawable ); - } else { - m_view.setBackgroundDrawable( m_origDrawable ); - } + m_dsdel.showSelected( m_selected, s_selDrawable ); } public void onDraw( Canvas canvas ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringTextView.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringTextView.java index 014e5af47..6c0ea2815 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringTextView.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ExpiringTextView.java @@ -28,7 +28,6 @@ import android.widget.TextView; class ExpiringTextView extends TextView { private ExpiringDelegate m_delegate = null; private Context m_context; - protected boolean m_selected = false; public ExpiringTextView( Context context, AttributeSet attrs ) { @@ -53,12 +52,6 @@ class ExpiringTextView extends TextView { } } - protected void toggleSelected() - { - m_selected = !m_selected; - getDelegate().setSelected( m_selected ); - } - @Override protected void onDraw( Canvas canvas ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListGroup.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListGroup.java index e630cdb62..65cf5947f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListGroup.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListGroup.java @@ -24,16 +24,21 @@ import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.View; +import android.widget.LinearLayout; import org.eehouse.android.xw4.DBUtils.GameGroupInfo; -public class GameListGroup extends ExpiringTextView - implements SelectableItem.LongClickHandler +public class GameListGroup extends LinearLayout + implements SelectableItem.LongClickHandler, + View.OnLongClickListener { private int m_groupPosition; private long m_groupID; private boolean m_expanded; private SelectableItem m_cb; + private ExpiringTextView m_etv; + private boolean m_selected; + private DrawSelDelegate m_dsdel; public static GameListGroup makeForPosition( Context context, int groupPosition, @@ -53,6 +58,15 @@ public class GameListGroup extends ExpiringTextView super( cx, as ); } + @Override + protected void onFinishInflate() + { + super.onFinishInflate(); + m_etv = (ExpiringTextView)findViewById( R.id.game_name ); + m_dsdel = new DrawSelDelegate( this ); + setOnLongClickListener( this ); + } + public void setGroupPosition( int groupPosition ) { m_groupPosition = groupPosition; @@ -76,6 +90,17 @@ public class GameListGroup extends ExpiringTextView } } + protected void setText( String text ) + { + m_etv.setText( text ); + } + + protected void setPct( boolean haveTurn, boolean haveTurnLocal, + long startSecs ) + { + m_etv.setPct( haveTurn, haveTurnLocal, startSecs ); + } + // GameListAdapter.ClickHandler interface public void longClicked() { @@ -84,8 +109,17 @@ public class GameListGroup extends ExpiringTextView protected void toggleSelected() { - super.toggleSelected(); + m_selected = !m_selected; + m_dsdel.showSelected( m_selected ); m_cb.itemToggled( this, m_selected ); } + ////////////////////////////////////////////////// + // View.OnLongClickListener interface + ////////////////////////////////////////////////// + public boolean onLongClick( View view ) + { + longClicked(); + return true; + } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java index e27712633..b4a37a78d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java @@ -66,8 +66,8 @@ public class GameListItem extends LinearLayout private int m_fieldID; private int m_loadingCount; private int m_groupPosition; - private Drawable m_origDrawable; private boolean m_selected = false; + private DrawSelDelegate m_dsdel; public GameListItem( Context cx, AttributeSet as ) { @@ -80,6 +80,7 @@ public class GameListItem extends LinearLayout m_rowid = DBUtils.ROWID_NOTFOUND; m_lastMoveTime = 0; m_loadingCount = 0; + m_dsdel = new DrawSelDelegate( this ); setOnClickListener( new View.OnClickListener() { @Override @@ -332,12 +333,7 @@ public class GameListItem extends LinearLayout private void toggleSelected() { m_selected = !m_selected; - if ( m_selected ) { - m_origDrawable = getBackground(); - setBackgroundColor( XWApp.SEL_COLOR ); - } else { - setBackgroundDrawable( m_origDrawable ); - } + m_dsdel.showSelected( m_selected ); m_cb.itemToggled( this, m_selected ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListItem.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListItem.java index 3e4273401..857086ce7 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListItem.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWListItem.java @@ -22,7 +22,6 @@ package org.eehouse.android.xw4; import android.content.Context; import android.graphics.Rect; -import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; import android.widget.ImageButton; @@ -38,10 +37,10 @@ public class XWListItem extends LinearLayout private Context m_context; private Object m_cached; private DeleteCallback m_delCb; - private Drawable m_origDrawable; private boolean m_selected = false; private SelectableItem m_selCb; private CheckBox m_checkbox; + private DrawSelDelegate m_dsdel; public interface DeleteCallback { void deleteCalled( XWListItem item ); @@ -50,6 +49,7 @@ public class XWListItem extends LinearLayout public XWListItem( Context cx, AttributeSet as ) { super( cx, as ); m_context = cx; + m_dsdel = new DrawSelDelegate( this ); } @Override @@ -152,12 +152,9 @@ public class XWListItem extends LinearLayout private void toggleSelected() { m_selected = !m_selected; - if ( m_selected ) { - m_origDrawable = getBackground(); - setBackgroundColor( XWApp.SEL_COLOR ); - } else { - setBackgroundDrawable( m_origDrawable ); - } + + m_dsdel.showSelected( m_selected ); + m_checkbox.setChecked( m_selected ); m_selCb.itemToggled( this, m_selected );