The problem with the GridView nested in the Android ListView and how to fix it.

Source: Internet
Author: User

We are directly into the subject. For all examples of problems, please refer to

1, how to make the picture have a click event?

For:

Workaround:

In your baseadapter do not set the following three things, and then set the GridView Onitemclick.

Gridview.setclickable (FALSE);

Gridview.setpressed (FALSE);

Gridview.setenabled (FALSE);

2, why I have dealt with the ListView click event Blocking, while the picture has a click event, and then click on the picture with the height of the space, but unable to perform the ListView item click event, click Other non-high places, such as click on the text but can.

For:

Reason:

GridView Lattice view has a click event to prove that you did not block, the reason the click and the picture with the height of the space does not perform the ListView Click event is your GridView occupied the whole line, even if your lattice only one, verify I this sentence is to the GridView Set a background, if not so please leave a message, I help you fix.

Workaround:

Dynamic to your GridView set width, do not use wrap_parent and so on.

3. How to dynamically set the width and height of the GridView nested inside the listview?

For:

Workaround:

The GridView does not have setwidth () and so on, you must use Setlayoutparams () to fix it.

4, why did I give the GridView set Setlayoutparams () after being thrown a strong turn type failure exception, program crashes?


For:

Cause/Workaround:

If your GridView is a sub-view of a view, for example, if you use LinearLayout to wrap the GridView, then the Setlayoutparams () passed in by your GridView must be layoutparams if Lin Earlayout. That is

viewgroup.layoutparams linearparams =newlinearlayout.layoutparams (70,70);

Gridview.setlayoutparams (Linearparams); otherwise throws an exception.

5, why I dynamically set the width of the GridView, found that the lattice has dislocation, overlap, gap is too big, very ugly is not happy feeling phenomenon?

For:

Reason:

Width, gap is too large-your GridView if you use the default layout, that is, if you are not set Gridview.setstretchmode (Gridview.no_stretch); , then it is the average of the width of the display of each lattice, this will cause. The workaround is to set the Gridview.no_stretch and then the custom grid width and clearance. or set the width of the GridView.

Overlap, occlusion phenomenon-if you set the gridview.no_stretch, Then there may be overlapping, occlusion phenomenon, the solution is the dynamic measurement device width and then use the ratio to set the ideal grid width, not affect the grid click, but also the corresponding Click on the item of the ListView, for example:

int mywidth = 80; This value is the width of your display picture, for example my ImageView, I set it to 80DP wide, then it is 80

int distance = 3;//This is the gap between each lattice

int userphonewidth = width of the device;//self-use code acquisition

int myphonewith = 1080; This is the width of the device you're testing, mine is 1080.

Formula calculation, first declared, the above 4 values once determined to apply to all devices, because the use of equal-to-scale, but also tested.

(Ideal lattice width for other phones)/userphonewidth = mywidth/myphonewith, simplified after simplification:

Ideal lattice width = userphonewidth * Mywidth/myphonewith

Gridview.sethorizontalspacing ((userphonewidth+3) * Maximum number of squares * mywidth/myphonewith); Set spacing after using gridview.no_stretch

6, why do I click on the text below the section cannot perform the ListView item click event?

A: Typical event blocking, in response to this solution, Baidu above a lot of solutions. I'm not wordy here.

Finally, my project's ListView after nesting the GridView effect is satisfying all of the above desired effects.

The problem with the GridView nested in the Android ListView and how to fix it.

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.