Android has fun with the GridView

Source: Internet
Author: User

Working for so long has been aimed at addressing demand. Gradually found this way of learning is not good, the knowledge learned can immediately solve the problem, but not through a comb induction. Therefore wants the system to summarize some interesting knowledge points. In this blog, I want to use an example system to explain the various aspects of the GridView control, such as monitoring, background map settings.

1. Description of control properties

android:listselector="@drawable/bg"

This property is important, and if not set, the GridView control will bring its own selected style (yellow border):

This is not usually required for projects. So the solution is to set the GridView Android:listselector property to be the same as the activity background.
android:verticalspacing= "10DP"//Line width
android:horizontalspacing= "10dip"//Column width
android:numcolumns= "3"//Number of columns

Android:stretchmode= "ColumnWidth"//Column expandable
What if you want to set the number of columns to 4 in high resolution to 3?

First, you need to set the column as Extensible android:stretchmode= "ColumnWidth" in the property. Then make a judgment in the code:

if (metrics.heightpixels = = && Metrics.widthpixels = = 800) {
Gridview.setnumcolumns (4);
This way, the number of columns in the GridView can be more set up with different resolutions.

2. Example one: set a border, and set a checkbox in the upper right corner to beautify the selected state (left image → right).

To

in the XML design of this item, I implemented it with two ImageView controls. The first ImageView to set a property: Android:layout_margin="4dip". Left and right to set aside 4dip of concrete to show the effect of the border. And in the corresponding adapter to make a judgment, select the color of the background image is #87cefa, when not selected is 3FFFFFF:if(bselect) {//bselect Indicates whether to selectArg1.setbackgroundcolor (Color.parsecolor ("#87CEFA")); }Else{Arg1.setbackgroundcolor (Color.parsecolor ("#FFFFFF")); The top right corner looks like a check box, but I also use the ImageView control to implement it. The interface design is very important, the distance to be good. The control I set is a distance of 2dip from the top and right margins, respectively.  The same can only be done by modifying the image that the ImageView is bound to. if(bselect) {viewclass.checkboxImage.setImageBitmap (Bmpwater_sel);//Bmpwater_sel represents the picture in the upper-right corner of the left image}Else{viewclass.checkboxImage.setImageBitmap (Bmpwater_unsel);//Bmpwater_unsel represents the picture in the upper right corner of the right image}

3. Example two: when you open the activity of a GridView control, if you open too many pictures, how to make the picture load asynchronously . :

To

First in the interface definition to define a full-screen display of the load ring effect, called mloading. When the program just starts, it shows that the picture is loaded at the end of the call function mloading.setvisibility (view.gone); I use a function to invoke it, so that the better embodiment of encapsulation, as follows:

private void Showloading (Boolean bShow) {
if (mloading = = null) return;
Mloading.setvisibility (bShow? View.VISIBLE:View.GONE);
}

The Load box in the XML is designed as follows:

<span style="font-size:13px;"><relativelayout android:id="@+id/video_tip_layout"Android:layout_width="fill_parent"android:layout_height="fill_parent"><progressbar style="? Android:attr/progressbarstylelarge"Android:layout_width="wrap_content"Android:id="@+id/video_tip_progressbar"Android:layout_height="wrap_content"Android:layout_centervertical="true"Android:layout_centerhorizontal="true"/><textview android:textappearance="? Android:attr/textappearancemedium"Android:id="@+id/video_tip_text"android:text="Loading in ..."Android:layout_width="wrap_content"android:layout_height="wrap_content"Android:layout_below="@+id/video_tip_progressbar"Android:textcolor="#808080"Android:layout_centerhorizontal="true"></TextView></span>

① in order to not the main thread of the card, to achieve this effect is to open a more thread. In this thread called the function initlist (), in the function to load the picture into the ArrayList (most of the time is the processing of many pictures, this is placed in the sub-thread can not card the main thread, if directly in the main thread, will make the screen appear 2-3 seconds of black screen pause).

Like this, open a sub-thread in the OnCreate method:

New Thread (New Runnable () {
@Override
public void Run () {
Initlist ();
}
}). Start ();

There are two things to do in ② method Initlist (). One is to load the picture, and the second is to notify handler that the picture is loaded (because the child thread cannot manipulate the UI control, only by handler).

private void Initlist ()
{

XXXX; Load Picture Code
Mmainhandler.sendmessage (Mainhandler.msg_finish); Send Message
}

③ The mloading mentioned above is not displayed when you receive a message sent by Initlist. At the same time the GridView binding adapter code is also written in this. Because the picture is loaded and then bound adapter, so that the picture can be displayed all, or only part of the display.

Case Msg_finish:
Showloading (FALSE); Let the load box no longer appear
Gridview.setadapter (IA); Binding Data
Break
Default
Break

Provide a few of our professional designers do a beautiful picture, you can download the material to try, do a demo. Address: http://www.kuaipan.cn/file/id_37676161790126914.html

  

Android has fun with the GridView

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.