Android Custom ViewGroup Landscape Layout (1) _android

Source: Internet
Author: User

Recently learning to customize ViewGroup, my goal is to make a ListView that you want to scroll, use adapter to populate the data, and use Adapter.notifydatasetchanged () to update the data.

But eat not a fat one (I eat like this but a few years of accumulation of ~ ~ ~ ~ ~ ~ ~ ~ ~), we step by step, this note first write a horizontal layout.

Code:

Package com.example.libingyuan.horizontallistview.ScrollViewGroup;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.View;

Import Android.view.ViewGroup; /** * Custom ViewGroup * Very simple horizontal layout, all the child view are arranged horizontally, not scrollable/public class Scrollviewgroup extends viewgroup{public scrollv
  Iewgroup {This (context,null);
  Public Scrollviewgroup (context, AttributeSet attrs) {This (context, attrs,0); Scrollviewgroup (context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstyleatt
  R); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Reset width High this.setmeasured
  Dimension (Measurewidth (Widthmeasurespec,heightmeasurespec), Measureheight (Widthmeasurespec,heightmeasurespec)); /** * Measuring width */private int measurewidth (int widthmeasurespec, int heightmeasurespec) {//width int Sizew
   Idth = Measurespec.getsize (Widthmeasurespec); width of type int modewidth = Measurespec.getmode (Widthmeasurespec);
    The width of the parent control (wrap_content) int width = 0;

    Number of child view int childcount = Getchildcount ();
      Re-measure the width of the child view, as well as the maximum height for (int i = 0; i < ChildCount; i++) {//Get child view view children = Getchildat (i);
      Measure the view, no matter what mode, this sentence must have otherwise the interface does not show the view (a blank) measurechild (Child, Widthmeasurespec, Heightmeasurespec);
      Get the margin marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams () of the child view;
      Get the width int childwidth = child.getmeasuredwidth () + Lp.leftmargin + lp.rightmargin;
    width added = childwidth; //Back to width return modewidth = = measurespec.exactly?
  Sizewidth:width; /** * Measuring Height * * private int measureheight (int widthmeasurespec, int heightmeasurespec) {//height int Sizeh
    eight = Measurespec.getsize (HEIGHTMEASURESPEC);
    The height of the pattern int modeheight = Measurespec.getmode (Heightmeasurespec); The height of the parent control (wrap_content) int height =0;

    Number of child view int childcount = Getchildcount ();
      Re-measure the width of the child view, and the maximum height for (int i = 0; i < ChildCount; i++) {//Get the child view view children = Getchildat (i);
      Measure Measurechild (Child, Widthmeasurespec, Heightmeasurespec);
      Get margin Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
      Get a height of int childheight = Child.getmeasuredheight () + Lp.topmargin + lp.bottommargin;
    Cumulative height = childheight;
    //ask for average height = height/childcount; Back to height return modeheight = = measurespec.exactly?
  Sizeheight:height;
    @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {int childleft=0;//Zi view to the left of the distance
    int childwidth;//View width int height=getheight ();
    int Childcount=getchildcount ();
      for (int i = 0; i < ChildCount i++) {View child=getchildat (i);
      Marginlayoutparams lp= (Marginlayoutparams) child.getlayoutparams (); Childwidth=Child.getmeasuredwidth () +lp.leftmargin+lp.rightmargin;
      The most important word child.layout (childleft,0,childleft+childwidth,height);
    Childleft+=childwidth; } @Override Public Layoutparams generatelayoutparams (AttributeSet attrs) {return new Marginlayoutparams (getc
  Ontext (), attrs);

 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.