Surubelnita
Surubelnita

Reputation: 107

Custom view with scroll in Xamarin.Android

I have to display a large diagram in a Xamarin.Android application and the user must be able to scroll it horizontally and/or vertically.
A. My first thought was to create a custom view (inheriting from View) that handles the drawing in OnDraw and then place the custom view in a ScrollView. But in this situation I do not know:

  1. how to tell the ScrollView how much content it has to scroll;
  2. how do I know inside my custom view where the scroll is so that I can draw the corresponding part of the diagram (I want to draw only what is visible).

Just for testing I tried the layout below but I cannot get it to scroll:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <Button
    android:id="@+id/btnLoadDiagram"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Load Diagram" />
  <ScrollView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:fillViewport="true"
     android:scrollbars = "vertical"
     android:scrollbarStyle="insideInset" >
    <my.namespace.DiagramView
      android:layout_width="2000dp"
      android:layout_height="2000dp"
      android:id="@+id/diagramView" />
  </ScrollView>
</LinearLayout>

B. The second thought was to inherit from ScrollView but I need to scroll both on horizontal and on vertical.

What is the recommended approach here?

Upvotes: 0

Views: 426

Answers (1)

Surubelnita
Surubelnita

Reputation: 107

In the end I implemented a solution based on option A.
I implemented a custom view that inherits from Android.Views.View. The view implements the GestureDetector.IOnGestureListener and ScaleGestureDetector.IOnScaleGestureListener interfaces for scrolling and zooming. Internally I keep track of current content position and draw only the content that fits inside the view area.

Upvotes: 1

Related Questions