I have a main activity that has a ViewPager in it. The ViewPager uses a FragmentPagerAdapter to put the Fragments into the ViewPager. All of the Fragments are a PdfAssetFragment that contains a PDFView (Joan Zapata) and custom view that I wrote called DrawingView (which allows a user to draw on the screen). When I make calls to change the properties of the DrawingView that is currently in the page (every single Fragment in the ViewPager has it's own PDFView and DrawingView), it appears that nothing has changed.
Here is the abstract class that PdfAssetFragment inherits from (this is here so I can easily extend the class to allow for different media types to be put inside the ViewPager):
public abstract class AnnotationFragment : Fragment
{
protected View _view;
protected DrawingView _drawingView;
/// <summary>
/// ID of the fragment layout to inflate
/// </summary>
protected abstract int ViewId { get; }
/// <summary>
/// Sets up the view after it has been inflated
/// </summary>
protected abstract void SetupView();
/// <summary>
/// Creates and instantiates the view
/// </summary>
/// <param name="inflater">Inflate to inflate the view</param>
/// <param name="container">Container to inflate the view into</param>
/// <param name="savedInstanceState">Data passed from the caller</param>
/// <returns>Inflated view</returns>
public override View OnCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
_view = inflater.Inflate(ViewId, container, false);
_drawingView = _view.FindViewById<DrawingView>(Resource.Id.drawing_view);
SetupView();
return _view;
}
/// <summary>
/// Changes the type of annotation that will be drawn on the drawing view
/// </summary>
/// <param name="mode">New annotation mode</param>
public void OnAnnotationTypeChanged(DrawingView.Mode mode)
{
_drawingView.CurrentMode = mode;
_drawingView.TouchEventsEnabled = !mode.Equals(DrawingView.Mode.None);
}
}
Here is the code for the PdfAssetFragment:
public class PdfAssetFragment : AnnotationFragment, IOnPageChangeListener
{
/// <summary>
/// ID of the fragment layout to inflate
/// </summary>
protected override int ViewId { get { return Resource.Layout.PdfAssetFragment; } }
/// <summary>
/// Sets up the view after it has been inflated
/// </summary>
protected override void SetupView()
{
string t = Arguments.GetString("ASSET");
var pdfView = _view.FindViewById<PDFView>(Resource.Id.pdf_view);
pdfView.FromFile(new Java.IO.File(t))
.ShowMinimap(false)
.EnableSwipe(false)
.OnPageChange(this)
.Load();
}
}
AssetPagerAdapter:
/// <summary>
/// Handles the displaying of fragments in the asset view pager
/// </summary>
public class AssetPagerAdapter : FragmentPagerAdapter
{
private List<IMedia> _assets;
private Context _context;
/// <summary>
/// Instantiates this adapter with a list of assets
/// </summary>
/// <param name="manager">Manager of this object</param>
/// <param name="assets">List of assets to display</param>
public AssetPagerAdapter(Context context, FragmentManager manager, List<IMedia> assets) : base(manager)
{
_context = context;
_assets = assets;
}
/// <summary>
/// Gets the number items this adapter manages
/// </summary>
public override int Count { get { return _assets.Count; } }
/// <summary>
/// Gets the next fragment to display
/// </summary>
/// <param name="position">Current index</param>
/// <returns>A fragment to display in the view pager</returns>
public override Fragment GetItem(int position)
{
var bundle = new Bundle();
bundle.PutString("ASSET", _assets[position].Url);
return PdfAssetFragment.Instantiate(_context,
Java.Lang.Class.FromType(typeof(PdfAssetFragment)).Name,
bundle);
}
}
Layout for the PdfAssetFragment:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.joanzapata.pdfview.PDFView
android:id="@+id/pdf_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ra16.views.controls.DrawingView
android:id="@+id/drawing_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/asset_view_margin"
android:layout_weight="1"/>
</FrameLayout>
Here is how I'm handling the paging of views: (in MainActivity.OnCreate())
_pager.PageScrolled += (sender, e) =>
{
if (_currentFragment != null)
{
AnnotationTypeChanged -= _currentFragment.OnAnnotationTypeChanged;
}
_currentFragment = SupportFragmentManager.Fragments[e.Position]
as AnnotationFragment;
AnnotationTypeChanged += _currentFragment.OnAnnotationTypeChanged;
};
When I raise the "AnnotationTypeChanged" event, the base class AnnotationFragments OnAnnotationTypeChanged method is triggered and it sets the _drawingViews CurrentMode and TouchEventsEnabled properties.
The problem is, after that happens and a touch event occurs on the drawingView, TouchEventsEnabled is always false (which is default), which is telling me that it just isn't changing. It appears to the change inside the AnnotationFragment class because if I check it's property values after setting them in the event handler, they appear to be what I expect. It's almost like I'm not working with the same _drawingView anymore...
To add more info to all of this: I have checked and can confirm that ID's of every single _drawingView seems to be the same..
Any help or suggestions?