Your application can also listen for long click events on particular items

Your application can also listen for long click

This preview shows page 65 - 74 out of 78 pages.

Your application can also listen for long-click events on particular items. Additionally, your application can listen for selected items. Although the parameters are the same, your application receives a call as the highlighted item changes. This can be in response to the user scrolling with the arrow keys and not selecting an item for action.
Image of page 65

Subscribe to view the full document.

Using ListView with ListFragment The ListView control is commonly used for full- screen menus or lists of items from which a user selects. You might consider using ListFragment as the base class for such screens and adding the ListFragment to your View . Using the ListFragment can simplify these types of screens. Fragments are discussed in Chapter 9, “Partitioning with Fragments.”
Image of page 66
Using ListView with ListFragment (Cont’d) First, to handle item events, you now need to provide an implementation in your ListFragment . For instance, the equivalent of OnItemClickListener is to implement the onItemClick() method within your ListFragment that implements the AdapterView.OnItemClickListener interface.
Image of page 67

Subscribe to view the full document.

Using ListView with ListFragment (Cont’d) Second, to assign an Adapter , you need a call to the setListAdapter() method. You do this after the call to the setContentView() method of your Activity , in the ListFragment method named onActivityCreated() . However, this hints at some of the limitations of using ListFragment .
Image of page 68
Using ListView with ListFragment (Cont’d) To use ListFragment , the layout that is inflated inside your ViewFragment with the onCreateView() method of the ListFragment must contain a ListView with the identifier set to @android:id/list ; this cannot be changed. Second, you can also have a View with an identifier set to @android:id/empty to have a View display when no data is returned from the Adapter . Finally, this works only with ListView controls, so it has limited use. However, when it does work for your application, it can save some coding.
Image of page 69

Subscribe to view the full document.

Adding Scrolling Support One of the easiest ways to provide vertical scrolling for a screen is by using these controls: ScrollView (vertical scrolling) HorizontalScrollView (horizontal scrolling) Either control can be used as a wrapper container, causing all child View controls to have one continuous scroll bar. However, the ScrollView and HorizontalScrollView controls can have only one child, so it’s customary to have that child be a layout, such as a LinearLayout , which then contains all the “real” child controls to be scrolled through.
Image of page 70
Adding Scrolling Support (Cont’d)
Image of page 71

Subscribe to view the full document.

Exploring Other View Containers Many other user interface controls are available within the Android SDK. Some of these controls are listed here: Toolbar SwipeRefreshLayout RecyclerView CardView ViewPager DrawerLayout
Image of page 72
Exploring Other View Containers Toolbar A Toolbar can be used as your application’s ActionBar or may also be used anywhere else in your application’s view hierarchy. If your application has media controls, for example, you may want to embed your media controls inside a Toolbar , and place the Toolbar at the bottom of your application.
Image of page 73

Subscribe to view the full document.

Image of page 74
  • Summer '16

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern