The project has come to an end. Here is a summary-(to be continued)

Source: Internet
Author: User

I made an application similar to the electronic market and encountered many problems.

1. use the http package of Apache for network communication. If a message header ("Accept-encoding", "gzip, deflate") is added, no matter whether the data obtained from the server has passed gzip, will be decompressed, so if the data is not compressed and is decompressed, it will be garbled. Be sure to set this message header.

2. Use of include and viewstub labels

Let's talk about the include tag first. The main function of the include tag is to reuse the already written layout file, but there are limitations, because the include tag can only overwrite the width, high, margin of the referenced layout, for basic attributes such as weight, the attributes of the subview in the reference layout cannot be modified. It is a static reference, not to say that it cannot be modified.ProgramSo the include label can be used, but do not use it forcibly, which may cause unnecessary trouble.

Besides, the viewstub label

The advantage is that if the inflate method of this object is not called, the object will not be instantiated. When you need this layout, call its inflate method, this layout will be drawn to save memory.

Suspect: there is no difference between the view before inflate and the visible attribute set to gone?

The default attribute of viewstub is invisible, not gone. You can load this view by setting it to visible or calling its inflate method. It inherits from the view and can be loaded at runtime, in addition, unlike the view setting of invisible, after the view is set to invisible, the view will be inflate and occupy a certain amount of space, but viewstub will not.

Let's take a look at the gone attribute of the view. This view is invisible, and it doesn't take any space for layout.
Purposes, will the gone view be called by the inflate method? No, because it does not occupy space. So far, it seems that viewstub and gone have similar functions. Let's look at the section extracted from the viewstub class below.Code

 
Private VoidInitialize (context ){
Mcontext=Context;
Setvisibility (gone );
Setwillnotdraw (True);
}

First, set the visible attribute of this view to gone, and then set the class to not draw.

The principle of viewstub is that the attribute of viewstub is set to gone and will not be drawn.
Setwillnotdraw (True), His ondraw method will not be called, and in his onmeasure method, set his length and width to 0, so that this class will not occupy space, because viewstub contains other layout files, similar to include, viewstub is also a view and can be created by itself. However, the layout files contained by viewstub will not be loaded, that is, they will not be inflate, therefore, it will not consume too much memory. How to ensure that layout files contained in viewstub are not loaded:

Mainly by setting
Setwillnotdraw (True) Method.

Here is an article from a netizen.Article:

 I. Introduction:

I think you have also encountered this problem before: as a result of the objective needs, we need to create a class that directly or indirectly inherits the view, so that the ondraw () method provided by lateral navigation view can't get the result we want. Let's talk about ondraw () today () the method is not parsed by the response line. You may also set the tracking point or log in the ondraw () method, and the program does not have the ondraw () method, then you need to add the following sentence to the structure data of the view directly or indirectly:

Setwillnotdraw ( False );

Ii. solution:

So what is the role of adding this sentence? First look at the API:

If This View doesn ' T do any drawing on its own, set this flag to allow further optimizations. by default, this flag is not set on view, but cocould be set on some view subclasses such as viewgroup. typically, If you override ondraw (canvas) You shoshould clear this flag.

I am not very good at the external market. I need to translate it. If it is not good, don't throw it. I will repeat my statement: imagine, without imagination, what will the world look like. Hey:

If you do not perform any operation on the previous view, you need to set this note so as to better meet future requirements, this mark is not set in the view. However, some sub-classes like views such as viewgroup can be set. In typical cases, if you have implemented the ondraw (canvas) method, you need to clear this criterion.

So exactly, what we get is the Child class of the View: linearlayout. Of course, you can also inherit other sub-classes such:

Absolutelayout, adapterview < T Extends Adapter > , Framelayout, linearlayout, relativelayout, and slidingdrawer. The sub-class will not be mentioned. You can check the text watermark by yourself.

This sentence should be placed in the structured data of the statement type, for example:

Public Class Backgroundlayout Extends Linearlayout {

Public Backgroundlayout (context, Int Position ){

Super (Context );

// Todo auto-generated constructor stub

Setwillnotdraw ( False );

}

@ Override

Protected Void Ondraw (canvas ){

// Todo auto-generated method stub

Super . Ondraw (canvas );

}

}

}

Iii. Future views:

In eoeandroid, little's opinion on this sentence is:

Set whether the view is changed. If the developer uses a custom view, override ondraw () should set this method to false so that the program will call the custom layout.

Reference it here.

In fact, we can see from the literal meaning of this sentence: setwillnotdraw ( False ); Is it true that the configuration will not continue? When you repeat ondraw (), it is necessary to begin the statement. Therefore, set the number of the sentence to false.


@ Override
Protected VoidOnmeasure (IntWidthmeasurespec,IntHeightmeasurespec ){
Setmeasureddimension (0,0);
}

After the inflate method of viewstub is called, the layout file contained in viewstub will be loaded. The inflate and viewstub view objects are useless, and they will act as a sub-layout, after the child layout is inflate, you need to remove it and fill in the Child layout object to save more memory space. Here you can refer to the source code:


   Public View inflate (){
Final Viewparent = Getparent ();

If (Viewparent ! = Null && Viewparent Instanceof Viewgroup ){
If (Mlayoutresource ! = 0 ){
Final Viewgroup parent = (Viewgroup) viewparent;
Final Layoutinflater Factory = Layoutinflater. From (mcontext );
Final View = Factory. Inflate (mlayoutresource, parent,
False );

If (Minflatedid ! = No_id ){
View. setid (minflatedid );
}

Final Int Index = Parent. indexofchild ( This );
Parent. removeviewinlayout ( This );

Final Viewgroup. layoutparams = Getlayoutparams ();
If (Layoutparams ! = Null ){
Parent. addview (view, index, layoutparams );
} Else {
Parent. addview (view, index );
}

Minflatedviewref = New Weakreference < View > (View );

If (Minflatelistener ! = Null ){
Minflatelistener. oninflate ( This , View );
}

Return View;
} Else {
Throw New Illegalargumentexception ( " Viewstub must have a valid layoutresource " );
}
} Else {
Throw New Illegalstateexception ( " Viewstub must have a non-null viewgroup viewparent " );
}
}

3. Solve the topic image adaptation problem on low-definition mobile phones and high-definition mobile phones. The centercrop attribute dynamically updates the itemview height of the table and reestablishes the connection between listview and adpater.

The centercrop attribute is used to draw an image in a proportional manner so that the image size is equal to or greater than the length and width of the view (if the size of the image is smaller than the length and width of the view, the image will be scaled evenly to the center of the view's length and width. If the length and width of the image are greater than the length and width of the view, the image will be scaled down and centered, (This is different from the description of this attribute. I don't know why), and then the center is displayed.

In this case, an image is used as the background of the item in the listview. The image size is 480px x 160px, and the screen size of HTC G2 is 320 × 480, the width of the image is greater than the screen width of the mobile phone. The screen size of M9 is 640 × 960, and the image cannot be filled with the whole screen horizontally. How can this problem be adapted?

The solution is to set the scaletype attribute of the imageview to centercrop, and dynamically change the length and width of the imageview. Because the image is scaled proportionally, the length and width of the imageview must also meet the length and width ratio of the image, in this way, the image will be distorted after being stretched proportionally. Set the width of the imageview to fillparent, dynamically obtain the length of the screen, and then obtain the hight of the view according to the corresponding proportion.

Another problem is that when switching between the horizontal and vertical dimensions occurs, listview will re-draw the itemview. If the length and width of the itemview are calculated in getview, the calculation is performed once for every view, because the previous itemview will be reused, you can re-set the setadapter in the onconfigrationchanged method. In this way, the itemview will be re-created, and a hight will be set in the itemview constructor to avoid calling getview every time, you must set the issue once.

4. decompress the compressed file

5. Use of Gallery

6. Use of scrollview

7. tiled Images

8. asynchronous image download Mechanism

9. How to set the image size

10. XML parsing method. Dom Parsing is performed first, and then the advantage of SARS or pull Parsing is achieved.

11. hashset equals and hashcode Rewriting

12. An error occurs. In a linearlayout, three columns are placed horizontally.

Linearlayout, respectively set their weight to 0, 0

Linearlayout does not place anything and is used for filling. After experiment, this method is not feasible.

The linearlayout container does not hold anything, and it does not stretch. He decides his size based on his content.

After changing linearlayout to view, it will be OK. Later I studied it and found that

Linearlayout is also acceptable. Why?

13. There is a layout file bar. XML that contains three linearlayout. This layout is used in the toolbar at the bottom of an activity and the menu at the top, so there are several problems,

First, you cannot obtain the desired control through the findviewbyid method of the activity, because the control with the same ID appears in the activity.

Second, how to reuse the Layout


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.