Android Basic Development ScrollView

Source: Internet
Author: User

ScrollView is a special display view provided by the Android system.

In fact, we have encountered ScrollView, such as the ListView.

Google's official documents also suggest, do not mix ScrollView & ListView, should for them have certain conflict.

This issue is analyzed and resolved later in this article.

1. Understanding ScrollView

Layout container forA view hierarchy that can is scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a framelayout, meaning you should place one child in it containing the entire contents to scroll; ThisChild may itself is a layout manager with a complex hierarchy of objects. A child this is often used are a linearlayout in a vertical orientation, presenting a vertical array of top-Level items, the user can scroll through. You should never use a ScrollView with a ListView, because the ListView takes care of its own vertical. Most importantly, doing ThisDefeats all of the important optimizations in ListView fordealing with large lists, since it effectively forces the ListView-to-display its entire list of items to fill up the Infinite container supplied by scrollview.the TextViewclassalso takes care of it own scrolling, so does isn't require a ScrollView, but using the-the-and-together is possible-ach Ieve the effect of a text view within a larger container. ScrollView only supports vertical scrolling. For horizontal scrolling, use Horizontalscrollview.

The above paragraph is the official description, a few questions.

    • ScrollView is essentially a framelayout, so ScrollView interior suggests only one view, such as Linerlayout.
    • Textview & ListView is born with scroll function, so it is not recommended to use with ScrollView.
    • Horizontal ScrollView, you can use Horizontalscrollview

2. Use ScrollView.

On official documents, the first line of properties is android:fillviewport visible This property is important to ScrollView!

This is what the property does:

When your ScrollView's sub-view, such as LinearLayout, wants to fill the screen with enough of the screen, but you find no matter how you set layout_height = "Match_parent".

It just doesn't keep the whole screen full. will only be displayed in the Wrap_content way. This is the special place of ScrollView. Use Android:fillviewport to solve this problem.

3.scrollview & ListView

In some special cases the Scroollview and the ListView need to be used simultaneously, the following is a demo of the example written on the Web:

Layout:

<?xml version= "1.0" encoding= "Utf-8"? ><scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/act_solution_1_sv"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#0dfeef" > <LinearLayout android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Vertical" > <TextView android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Data above \nlistview \ n"/> <Com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView Android:id= "@+id/act_solution_1_lv"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1" > </com.joyfulmath.sample.javanetwork.androidbase.scrollview.view.ScrollListView> <TextView android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Data below \nlistview \ n"/> </LinearLayout></ScrollView>

ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ListView;/** * @authordeman.lu Email [email protected]****.com *@versionOn 2016-03-10 11:12*/ Public classScrolllistviewextendsListView { PublicScrolllistview (Context context) {Super(context); }     PublicScrolllistview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicScrolllistview (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        intExpandspec = Measurespec.makemeasurespec (integer.max_value >> 2, Measurespec.at_most); Super. Onmeasure (Widthmeasurespec, Expandspec); }}

After this custom ListView, the ListView can be displayed in ScrollView.

Android Basic Development ScrollView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.