Android Layout View. measure () dynamically calculates the height and sets the layout -- (for example, dynamically calculates the comment height and displays it ),

Source: Internet
Author: User

Android Layout View. measure () dynamically calculates the height and sets the layout -- (for example, dynamically calculates the comment height and displays it ),
The requirement is as follows:

For details about the application, there is a comment section. A comment on this page can display a maximum of five items, and each comment can contain a maximum of 140 characters. Each comment may be different in words, the height is also different, for example, 1 line, 2 or 3 lines, and comments can be turned over.

The image effect is as follows:




How can this problem be solved?

The first thing you must know is that the comment control should not be fixed, but must be dynamically calculated and dynamically added to the display panel.

The following is an example.

1. Define the Layout

When defining the layout, you can use AbsoluteLayout. Because the height is dynamic, the specific coordinates can be obtained. Refer to the following:

 <AbsoluteLayout        android:layout_width="1386px"        android:layout_height="wrap_content"        android:layout_marginTop="60px"        android:id="@+id/comment_content">         </AbsoluteLayout>
The layout file width is not changed, and the height is filled according to the content. You only need to place the layout in the area where you want to display the comment content.

2. Display Data


To display data, you need to calculate the height based on the data. First, set the data to the control, and then use View. measure (int widthMeasureSpec, int heightMeasureSpec) calculates the height and sets coordinates for the View. Reference code:

<Span style = "font-size: 14px;">/*** initialization, dynamic computing height */public void initCommentView () {record_temp = new CommentRecord (); mPositionRecord = new ArrayList <Integer> (); mHeightRecord = new ArrayList <Integer> (); int currentHeight = 0; int maxNum = cachedComments. size (); int sum = 0; for (int I = comment_begin_index; I <maxNum; I ++) {if (null! = MCommentCache &&! MCommentCache. empty () {comment_temp = mCommentCache. pop ();} else {comment_temp = new CommentSimpleView (mContext);} mCommentUI. add (comment_temp); comment_temp.setData (cachedComments. get (I); comment_temp.measure (width, height); if (MAX_COMMENT_HEIGHT> currentHeight) {comment_content.addView (comment_temp, new AbsoluteLayout. layoutParams (1386, comment_temp.getMeasuredHeight (), 0, FIRST_COMMENT_INTERVAL + currentHeight); mPositionRecord. add (FIRST_COMMENT_INTERVAL + currentHeight); mHeightRecord. add (comment_temp.getMeasuredHeight (); currentHeight = currentHeight + comment_temp.getMeasuredHeight (); comment_end_index ++; sum ++; if (sum <5) {limit ();} else if (sum = 5) {comment_temp.hide_Divider () ;}} if (MAX_COMMENT_HEIGHT <currentHeight) {compareHeight = 1; isEnd = false; RelativeLayout. layoutParams rl = (LayoutParams) comment_content.getLayoutParams (); rl. setMargins (0, 60, 0, 0); rl. width = 1386; rl. height = MAX_COMMENT_HEIGHT + 20; histogram (rl); break;} if (MAX_COMMENT_HEIGHT = currentHeight) {compareHeight = 0; if (maxNum = comment_end_index) {isEnd = true; else (false) ;}else {isEnd = false ;}} else (mHeightRecord); then (mPositionRecord); then (comment_begin_index); record_temp.setEnd_index (comment_end_index ); if (MAX_COMMENT_HEIGHT> currentHeight) {isEnd = true; comment_pagedown.setFocusStatus (false) ;}</span>
The global width and height are defined as follows:

Int width = MeasureSpec. makeMeasureSpec (1386, MeasureSpec. EXACTLY );
Int height = MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED );

3. dynamic page flip computing implementation

 

/*** When you click the next page, determine whether there is space remaining. If there is space remaining, supplement the data on the previous page. false: no space remaining * @ return */private boolean hasExtraSpace () {if (null! = CachedComments & cachedComments. size ()-comment_end_index> = 5) {// The remaining comments are greater than 5 return false;} int beginIndex = 0; if (1 = compareHeight) {beginIndex = comment_end_index ;} else if (0 = compareHeight) {beginIndex = comment_end_index + 1;} int maxSize = cachedComments. size (); int HeightSum = 0; for (int I = beginIndex; I <maxSize; I ++) {comment_temp = new CommentSimpleView (mContext); comment_temp.setData (cachedComments. Get (I); comment_temp.measure (width, height); HeightSum + = comment_temp.getMeasuredHeight (); if (MAX_COMMENT_HEIGHT <= HeightSum) {return false ;}} lastPageHeight = HeightSum; return true;}/*** the last page is less than one screen, which is the last page and arranged in reverse order from the last one. */public void showLastPage () {int lastCommentNum = cachedComments. size ()-comment_end_index; int copy_last_index = comment_end_index; while (128 <= MAX_COMMENT_HEIGHT-lastPageHeight) {las TCommentNum ++; comment_temp = new CommentSimpleView (mContext); comment_temp.setData (cachedComments. get (-- copy_last_index); trim (width, height); lastPageHeight + = trim ();} if (MAX_COMMENT_HEIGHT <lastPageHeight) {lastCommentNum --; lastPageHeight-= trim (); copy_last_index ++;} int sum = cachedComments. size (); int current_H = FIRST_COMMENT _ INTERVAL + MAX_COMMENT_HEIGHT-lastPageHeight; for (int I = copy_last_index; I <sum; I ++) {if (null! = MCommentCache &&! MCommentCache. empty () {comment_temp = mCommentCache. pop ();} else {comment_temp = new CommentSimpleView (mContext);} mCommentUI. add (comment_temp); comment_temp.setData (cachedComments. get (I); comment_temp.measure (width, height); comment_content.addView (comment_temp, new AbsoluteLayout. layoutParams (1386, comment_temp.getMeasuredHeight (), 0, current_H); current_H = current_H + comment_temp.getMeasuredHeight (); If (I = sum-1) {comment_temp.hide_Divider ();} else {comment_temp.show_Divider () ;}} isEnd = true; comment_pagedown.setFocusStatus (false );} /*** click the previous page to exit the stack, recover data, and display */public void showPageUp () {if (mCommentRecord. empty () {Toast. makeText (mContext, "already the first page", Toast. LENGTH_SHORT ). show (); return;} record_temp = mCommentRecord. pop (); int begin = record_temp.getBegin_index (); int end = record_temp.getEnd_index (); List <I Nteger> position = record_temp.getPositionRecord (); List <Integer> height = record_temp.getHeightRecord (); if (null = position | null = height) {return;} int m = 0; for (int I = begin; I <= end; I ++) {if (null! = MCommentCache &&! MCommentCache. empty () {comment_temp = mCommentCache. pop ();} else {comment_temp = new CommentSimpleView (mContext);} mCommentUI. add (comment_temp); comment_temp.setData (cachedComments. get (I); comment_content.addView (comment_temp, new AbsoluteLayout. layoutParams (1386, height. get (m), 0, position. get (m); m ++; if (5 = m) {comment_temp.hide_Divider () ;}else {comment_temp.show_Divider () ;}} isEnd = false; comment_begin_index = begin; comment_end_index = end ;}

4. Click the previous page and next page for event handling

 

Public class comment_pageup_click implements OkButtonViewClick {@ Overridepublic void onOkButtonViewClick () {if (1 = currentPage) {// Toast. makeText (mContext, "already the first page", Toast. LENGTH_SHORT ). show (); comment_pageup.setFocusStatus (false); return;} else {currentPage = currentPage-1; comment_content.removeAllViews (); while (! MCommentUI. isEmpty () {mCommentCache. push (mCommentUI. remove (0) ;}showpageup (); sums (true) ;}} class comment_pagedown_click implements OkButtonViewClick {@ Overridepublic void onOkButtonViewClick () {if (isEnd) {// Toast. makeText (mContext, "is the last page", Toast. LENGTH_SHORT ). show (); comment_pagedown.setFocusStatus (false); return;} else if (! IsEnd) {// if (! HasExtraSpace () {// there is no space left on the next page, that is, this page is not the penultimate page of mCommentRecord. push (record_temp); currentPage = currentPage + 1; if (1 = compareHeight) {comment_begin_index = comment_end_index; comment_end_index --;} else {comment = comment_end_index + 1 ;} if (currentPage % 4 = 0) {// pre-load data ParserHelper. getParserHelper (). requestComment ("1", "30", "9", callback);} comment_content.removeAllViews (); while (! MCommentUI. isEmpty () {mCommentCache. push (mCommentUI. remove (0);} initCommentView ();} else {// available space on the next page, that is, this page is the penultimate page of mCommentRecord. push (record_temp); currentPage = currentPage + 1; comment_content.removeAllViews (); while (! MCommentUI. isEmpty () {mCommentCache. push (mCommentUI. remove (0) ;}showlastpage () ;}comment_pageup.setfocusstatus (true );}}}

5. Save the status when you click on the next page for restoration. (Save with Stack and import to stack)
package com.helios.module.commentData;import java.util.List;public class CommentRecord {int begin_index;int end_index;List <Integer> positionRecord;List <Integer> heightRecord;public CommentRecord() {super();}public int getBegin_index() {return begin_index;}public void setBegin_index(int begin_index) {this.begin_index = begin_index;}public int getEnd_index() {return end_index;}public void setEnd_index(int end_index) {this.end_index = end_index;}public List<Integer> getPositionRecord() {return positionRecord;}public void setPositionRecord(List<Integer> positionRecord) {this.positionRecord = positionRecord;}public List<Integer> getHeightRecord() {return heightRecord;}public void setHeightRecord(List<Integer> heightRecord) {this.heightRecord = heightRecord;}   }


6. Dynamic Calculation of the summary language. It is quite common to dynamically set the layout. The key is to use the measure () method. You can set the data to measure (). After measuring the height, you can set it to the display panel, that is, call ViewGroup. addView (View child, LayoutParams params) writes the dynamic layout for the first time and does not write well. I hope you do not need to spray it. If you have any questions, please leave a message to discuss it. You can learn from each other and make progress slowly.

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.