"> ">
Download presentation
Presentation is loading. Please wait.
1
Reactive Android Development
CS T & CS T Summer 2016 ListView, Adapter, and Custom View
2
ListView I'm simply using the default ListView layout entry (like last time) <ListView android:layout_width="wrap_content" android:layout_height="wrap_content"/>
3
Array Adapter As we saw last time, I'm modifying an array adapter.
By default, array adapters expect each element to implement toString and the default layout must be a text view.
4
Array Adapter As we saw last time, I'm modifying an array adapter.
By default, array adapters expect each element to implement toString and the default layout must be a text view. However, by overriding only one method (getView), I can change this to any other view or layout
5
Array Adapter As we saw last time, I'm modifying an array adapter.
By default, array adapters expect each element to implement toString and the default layout must be a text view. However, by overriding only one method (getView), I can change this to any other view or layout Including my own custom View
6
Custom View Your custom view can
Define attributes to be configured in the layout XML
7
Custom View Your custom view can
Define attributes to be configured in the layout XML Issue drawing commands to display the content
8
Custom View Your custom view can
Define attributes to be configured in the layout XML Issue drawing commands to display the content Programmatically adjust to changes in the size of the view
9
Custom View: Downsides
You are responsible for handling all of the layout You have to be sure that it provides an intuitive interface Information display Response to user input
10
Defining attributes Some of the layout calculations can be avoided by setting dimensions in XML <resources> <declare-styleable name = "MessageView"> <attr name="textSize" format="dimension"> <attr name="barColor" format="color"> </declare-styleable> </resources> Some attributes (like padding) appear to be available automatically.
11
Reading Attributes The attributes are passed to the View's constructor
They are accessed with TypedArray a = context.getTheme().obtainStyledAttributes( attrs, // the AttributeSet argument R.styleable.MessageView, // the description of the attributes 0, 0); // defaults (optional) barColor = a.getColor(R.styleable.MessageView_barColor, Color.blue); textSize = a.getDimension(R.styleable.MessageView_textSize, 24);
12
Drawing Two classes Canvas Paint Commands for what to draw
drawRect (rectangle) drawText Paint Properties for how to draw it setColor setStyle SetTextSize
13
Layout: onSizeChanged
Provides the current (and previous) width and height. Allows you to avoid recomputing dimensions if nothing has changed. Store any necessary calculations so that onDraw can be as fast as possible.
14
Layout: onMeasure Provides a chance to set the desired dimensions.
The height could be changed to accommodate different messages.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.