The Android custom view constructor is described in detail _android

Source: Internet
Author: User

Android Custom View Constructor

Custom view is a common requirement in Android, and each custom view needs to implement three basic constructors, and these three constructors have two common ways of writing.

First Kind

Each constructor calls the constructor of the base class, and then calls a public initialization method for additional initialization.

public class MyView extends ListView {public
  myview (context) {
    super);
    Sharedconstructor ();
  }

  Public MyView (context, AttributeSet attrs) {
    Super (context, attrs);
    Sharedconstructor ();
  }

  Public MyView (context, AttributeSet attrs, int defstyleattr) {
    Super (context, attrs, defstyleattr);
    Sharedconstructor ();
  }
  
  private void Sharedconstructor () {
    //do some initialize work.
  }
}

Second Kind

Cascade Invocation, each constructor calls a constructor with one more parameter, the last constructor calls the constructor of the base class, and finally does some extra initialization work.

public class MyView extends ListView {public
  myview (context
    , null);

  Public MyView (context, AttributeSet attrs) {This
    (context, attrs, 0);
  }

  Public MyView (context, AttributeSet attrs, int defstyleattr) {
    Super (context, attrs, defstyleattr);
    
    Other initialize work.
  }


So the question is, which way should we use it?

The conclusion is: It's best to use the first one, because the second approach can be problematic in some cases, such as when your custom view inherits from ListView or TextView, ListView or TextView internal constructors have a default Defstyle, and the second method calls Defstyle passes in 0, which overrides the default Defstyle in the base class, resulting in a series of problems. Take ListView For example, and look at its constructor.

Public ListView {This
    (context, NULL);
  }

  Public ListView (context, AttributeSet attrs) {This
    (context, Attrs, Com.android.internal.r.attr.listviewstyle);
  }

  Public ListView (context, AttributeSet attrs, int defstyleattr) {This
    (context, Attrs, defstyleattr, 0);
  } Public

  ListView (context, AttributeSet attrs, int defstyleattr, int defstyleres) {
    Super (context, attrs , defstyleattr, defstyleres);
    Other works.
  }

You can see that a com.android.internal.r.attr.listviewstyle is passed in the second constructor code of ListView, and when we call with the second method (cascade), we pass in 0, which overrides this default value. But the first method calls super (context, attrs); This, in turn, invokes the base class of this (context, attrs, Com.android.internal.r.attr.listviewstyle);

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.