Skip to content

Commit

Permalink
Fixes the convertview, finally get it :)
Browse files Browse the repository at this point in the history
LinearDialogScrollView now also tries to use a convertview, which should improve performance.
Element.GetViewImpl should not do anything with a convertview, so I removed that attribute
  • Loading branch information
csteeg committed Jul 10, 2013
1 parent 9ea9698 commit 0a7c185
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 91 deletions.
2 changes: 1 addition & 1 deletion CrossUI/CrossUI.Droid/Dialog/DialogAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override int ViewTypeCount
get
{
// returning 1 here - I couldn't find any docs on what to return with Adapter.IgnoreItemViewType
return 1;
return Adapter.IgnoreItemViewType;
}
}

Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/AchievementElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ protected virtual void UpdateDescriptionDisplay(View cell)
}
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
View view = DroidResources.LoadAchievementsElementLayout(context, convertView, parent, LayoutName);
View view = DroidResources.LoadAchievementsElementLayout(context, parent, LayoutName);
if (view == null)
{
Android.Util.Log.Error("AchievementElement", "GetViewImpl failed to load template view");
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/BooleanElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public BooleanElement(string caption = null, bool value = false, string layoutNa
{
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
View view = DroidResources.LoadBooleanElementLayout(context, convertView, parent, LayoutName);
View view = DroidResources.LoadBooleanElementLayout(context, parent, LayoutName);
return view;
}

Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/ButtonElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ protected override void UpdateCaptionDisplay(View cell)
base.UpdateCaptionDisplay(cell);
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
var view = DroidResources.LoadButtonLayout(context, convertView, parent, LayoutName);
var view = DroidResources.LoadButtonLayout(context, parent, LayoutName);
if (view != null)
{
Button button;
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/CheckboxElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public CheckboxElement(string caption = null, bool value = false, string subCapt
SubCaption = subCaption;
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
View view = DroidResources.LoadBooleanElementLayout(context, convertView, parent, LayoutName);
View view = DroidResources.LoadBooleanElementLayout(context, parent, LayoutName);
return view;
}

Expand Down
28 changes: 5 additions & 23 deletions CrossUI/CrossUI.Droid/Dialog/Elements/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,29 +161,12 @@ public virtual string Summary()
public View GetView(Context context, View convertView, ViewGroup parent)
{
Context = context;

bool mustGetNewView = true;

if (CurrentAttachedCell != null)
if (CurrentAttachedCell == null || convertView != CurrentAttachedCell)
{
if (convertView != CurrentAttachedCell)
{
DialogTrace.WriteLine(@"We believe this situation should never happen.
If `convertView` is not null, then we expect it to match our CurrentAttachedCell because we have set
ViewTypeCount to Ignore - see comments in https://github.com/slodge/MvvmCross/pull/294");
mustGetNewView = true;
}
else
{
//if the CurrentAttachedCell matches the convertview and if the convertview's parent is null, then we can reuse the cell
//for more info on this, see https://github.com/slodge/MvvmCross/pull/294
mustGetNewView = (CurrentAttachedCell.Parent != null);
}
}

if (mustGetNewView)
{
CurrentAttachedCell = GetViewImpl(context, convertView, parent);
//we only get a new cell if convertview is not the currentattachecell.
//this for example is the case when you replace an element
CurrentAttachedCell = GetViewImpl(context, parent);
}

UpdateCellDisplay(CurrentAttachedCell);
Expand All @@ -194,10 +177,9 @@ public View GetView(Context context, View convertView, ViewGroup parent)
/// Overriden by most derived classes, creates a View with the contents for display
/// </summary>
/// <param name="context"></param>
/// <param name="convertView"></param>
/// <param name="parent"></param>
/// <returns></returns>
protected virtual View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected virtual View GetViewImpl(Context context, ViewGroup parent)
{
throw new NotImplementedException("GetViewImpl should be implemented in derived Element classes");
}
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/EntryElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ public Action Send
}
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
var view = DroidResources.LoadStringEntryLayout(context, convertView, parent, LayoutName);
var view = DroidResources.LoadStringEntryLayout(context, parent, LayoutName);
if (view != null)
{
view.FocusableInTouchMode = false;
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/FloatElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public FloatElement(string caption = null, Bitmap left = null, Bitmap right = nu
MaxValue = 1;
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
View view = DroidResources.LoadFloatElementLayout(context, convertView, parent, LayoutName);
View view = DroidResources.LoadFloatElementLayout(context, parent, LayoutName);

if (view != null)
{
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/HtmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ private void OpenUrl(Context context)
context.StartActivity(intent);
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
var view = base.GetViewImpl(context, convertView, parent);
var view = base.GetViewImpl(context, parent);
Click = (o, e) => OpenUrl(context);
return view;
}
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/ImageElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
if (scaled == null)
scaled = Scale(Value);

Click = delegate { SelectImage(); };

var view = convertView as RelativeLayout ?? new RelativeLayout(context);
var view = new RelativeLayout(context);

var parms = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent,
ViewGroup.LayoutParams.WrapContent);
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/RadioElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public RadioElement(string caption = null, string group = null)
Group = group;
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
if (!(((RootElement) Parent.Parent).Group is RadioGroup))
throw new Exception("The RootElement's Group is null or is not a RadioGroup");

return base.GetViewImpl(context, convertView, parent);
return base.GetViewImpl(context, parent);
}

public override string Summary()
Expand Down
4 changes: 2 additions & 2 deletions CrossUI/CrossUI.Droid/Dialog/Elements/RootElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ private void ParentAddedElements(NotifyCollectionChangedEventArgs args)
}
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
RefreshValue();
return base.GetViewImpl(context, convertView, parent);
return base.GetViewImpl(context, parent);
}

private void RefreshValue()
Expand Down
7 changes: 3 additions & 4 deletions CrossUI/CrossUI.Droid/Dialog/Elements/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,16 @@ public int ElementViewTypeCount
get { return ElementTypes.Count; }
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
if (HeaderView != null)
{
return (HeaderView).GetView(context, convertView, parent);
return (HeaderView).GetView(context, null, parent);
}

if (Caption != null)
{
var view = (convertView as TextView) ??
new TextView(context, null, Android.Resource.Attribute.ListSeparatorTextViewStyle);
var view = new TextView(context, null, Android.Resource.Attribute.ListSeparatorTextViewStyle);
if (Caption.Length >= 0)
{
view.Text = Caption;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ protected override void UpdateCellDisplay(View cell)

protected abstract string Format(T value);

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
var view = DroidResources.LoadStringElementLayout(context, convertView, parent, LayoutName);
var view = DroidResources.LoadStringElementLayout(context, parent, LayoutName);
if (view != null)
{
if (FontSize > 0)
Expand Down
2 changes: 1 addition & 1 deletion CrossUI/CrossUI.Droid/Dialog/Elements/ViewElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ViewElement(string layoutName = null): base(string.Empty, layoutName)
{
}

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
#warning convertView is junk here?
View view;
Expand Down
2 changes: 1 addition & 1 deletion CrossUI/CrossUI.Droid/Dialog/Elements/WebContentElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public WebContentElement(Android.Net.Uri uri)

public String WebContent { get; set; }

protected override View GetViewImpl(Context context, View convertView, ViewGroup parent)
protected override View GetViewImpl(Context context, ViewGroup parent)
{
var webView = new WebView(context);

Expand Down
38 changes: 25 additions & 13 deletions CrossUI/CrossUI.Droid/Dialog/LinearDialogScrollView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Cirrious.MvvmCross.Binding.Droid.ResourceHelpers;
using CrossUI.Core;
using CrossUI.Droid.Dialog.Elements;
using Java.Lang;
using Orientation = Android.Widget.Orientation;

namespace CrossUI.Droid.Dialog
Expand Down Expand Up @@ -86,6 +87,7 @@ protected void Init(IAttributeSet attrs, int defStyleRes)
_list = new DividerAwareLinearLayout(this.Context, null);
_list.LayoutParameters = @params;
_list.Orientation = Orientation.Vertical;
AddFocusable();
AddView(_list);

LinearDialogStyleableResource.Initialise();
Expand Down Expand Up @@ -115,11 +117,10 @@ public void AddViews()
if (_dialogAdapter == null)
return;

_list.RemoveAllViews();
AddFocusable();
for (var i = 0; i < _dialogAdapter.Count; i++)
{
var view = _dialogAdapter.GetView(i, null, _list);
var currentView = _list.ChildCount >= i + 1 ? _list.GetChildAt(i + 1) : null;
var view = _dialogAdapter.GetView(i, currentView, _list);
view.SetTag(_TAG_INDEX, i);
view.Click -= ListView_ItemClick;
view.LongClick -= ListView_ItemLongClick;
Expand All @@ -132,19 +133,30 @@ public void AddViews()
view.LongClickable = true;
view.SetBackgroundDrawable(ItemBackgroundDrawable
?? Resources.GetDrawable(Android.Resource.Drawable.ListSelectorBackground));
//view.SetBackgroundColor(Color.Transparent);
_list.AddView(view);
/*if ((view.Visibility == ViewStates.Visible) && (_divider != null))

if (currentView != null)
{
if (currentView != view) // we had a view in here, but it should be the new guy
{
currentView.Click -= ListView_ItemClick;
currentView.LongClick -= ListView_ItemLongClick;
currentView.Dispose();
_list.RemoveView(currentView);
_list.AddView(view, i + 1);
}
}
else
{
var dividerImage = new ImageView(this.Context);
dividerImage.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent);
dividerImage.SetPadding(5, 2, 5, 2);
dividerImage.SetScaleType(ImageView.ScaleType.FitXy);
dividerImage.SetImageDrawable(_divider);
_list.AddView(dividerImage);
}*/
_list.AddView(view);
}
}

//remove remaining
for (var i = _list.ChildCount; i > _dialogAdapter.Count + 1; i--)
{
_list.RemoveViewAt(i-1);
}

}

public virtual Drawable ItemBackgroundDrawable { get; set; }
Expand Down
Loading

0 comments on commit 0a7c185

Please sign in to comment.