New things 003--android GridView control display height

Source: Internet
Author: User

One: Demand

Recently, the product decided to make big changes to the company's software, he wants to achieve netease Cloud Learning tab bar effect, such as (in a area need to have a series of tab bar (tab bar how much variable, but each row three, display if more than screen 2/3 will show the progress bar, that is, the maximum does not exceed the screen 2/3 display), b area is a ListView, in the middle is a few button, when the B area to slide up when the a area automatically closed, but at the top of the current selection of the label, click on the label pop-up Floating tab bar, when the B area to the top, the a area starts to show.)



There are two main problems in the above requirements.

1. The above series does not know how many are displayed with a button or another component. As a senior (hehe) developer, definitely do not want to use dynamic layout, a button to add, so I used the GridView, each inside a button

The 2.A area is present as a headview of the B region or a layout similar to the B zone.

At the beginning, I want to treat the a area as the headview of the ListView, so that a follows the B-area and slides up and down, which looks like a complete individual. But when implemented, there was a problem with the sliding pass of the ListView, Because the ListView needs to accept scrolling events, the above GridView may need to accept sliding events because the display height is ambiguous. In general, it can be used as a headview at a fixed height, but to achieve this random height may be compressed, is still not very good to achieve. So I gave up. I will have the GridView of area A as the peer view of the B zone, and in order to achieve the product demand, just add the animation when a expands.


GridView Control Display Height

I had expected the GridView to have Maxline or maxheight properties, so the problem was simple, but no. So it is thought that in the height property to do the processing, want to write in the layout file dead must not be, because the number of height is unknown, So the dynamic layout is used. In solving the problem of dynamic layout, it is the dynamic control height. What I do is divided into two steps

1. Get the display height of the screen

2. Calculate the height of the GridView after the data has been populated (it is 0 if the data is filled out, otherwise it is meaningless), if the height is greater than 2/3 of the screen, set the height of the GridView to 2/3 of the screen, if less, the value of heigt is not changed. (Part of the code below)

public void Setgridviewheight () {int needheight = ((((Labes.length + 2)/3 + 1) * DP2PX (this, 40.0f)); if (Needheight <= ((ScreenHeight * 4)/7)) {//indicates that the space display is sufficient and does not handle}else{linearlayout.layoutparams params = (android.widget.LinearLayout.LayoutParams) Gridview.getlayoutparams ();p arams.height = (screenheight * 4)/7-dp2px (this, 40.0f);p arams.width = Screenwidth;gridvie W.setlayoutparams (params);//a second gridviewrelativelayout.layoutparams PARAMS2 = ( Android.widget.RelativeLayout.LayoutParams) gridview2.getlayoutparams ();p arams2.height = params.height; Params2.width = Params.width;gridview2.setlayoutparams (PARAMS2);}}

3. Floating GridView Solution

is actually two GridView (floating one according to the logical display, can be hidden)


Summarize:

The function looks very flashy, but according to the written process analysis, writing down is still no difficulty. There will be time to sort out an example and upload it later.

New things 003--android GridView control display height

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.