Learn a little about simple paging processing after flash (63) reads XML every day

Source: Internet
Author: User

This feature is simple and has no technical knowledge. Just record it.

After obtaining XML data, you can use a simple paging process to complete the simple paging effect.

 

1. Use the remainder and division methods to determine the number of pages

This is a common practice and a method. Here, we will not discuss the optimal results of such paging processing. Here we will only discuss a method. If the data volume is small, try these steps. However, for a large amount of data, other measures are required. I am not familiar with databases, and even have forgotten a lot. However, it is okay to use the current resources to handle these issues.

 

Suppose that XML contains 50 pieces of information. If every page shows 10 pieces of information, 50/10 = 5. Five pages are calculated.

Assume that XML contains 51 pieces of information, and we need to pagination. If 10 pieces are displayed on each page, 51/10 = 5 ..... 1 (more than 1), we need 5 + 1 = 6 pages.

 

It can be assumed that when the Division is complete, the number of pages is easy to calculate. If it cannot be calculated, we can determine whether the Division can be obtained through the method of remainder. (Page % 2 = 0 ).

 

 

The following is an idea.

 

If (total page number % 2 = 0)

{

Number of pages = total number of pages/number of entries per page

}

 

Else

{

Number of pages = total number of pages/number of lines per page + 1;

}

 

With this method, we can write a simple class to implement this function.

Package COM. web <br/> {<br/> // paging class <br/> public class page <br/> {<br/> private var MAX: int; <br/> private var pagenum: int; <br/> private var currentpages: int; <br/> Public Function page () <br/>{</P> <p >}< br/> // set the number of paging items <br/> public function set maxrow (MAX: INT ): void <br/>{< br/> This. max = max; <br/>}< br/> // obtain the number of items on the page <br/> Public Function get maxrow (): int <br/>{< br/> return Max; <br/>}</P> <p> public function set currentpage (value: INT ): void <br/> {</P> <p> This. currentpages = value; <br/>}</P> <p> Public Function get currentpage (): int <br/>{< br/> return currentpages; </P> <p >}</P> <p> // obtain the total number of points and determine whether the whole page is displayed by using the remainder method. <br/> Public Function getpagenumber (total: INT): int <br/>{< br/> If (Total % maxrow = 0) <br/>{< br/> pagenum = total/maxrow; <br/>} else <br/>{< br/> pagenum = int (total/maxrow) + 1; <br/>}< br/> return pagenum; <br/>}</P> <p> Public Function get totalpage (): int <br/>{< br/> return pagenum; <br/>}</P> <p> // display page number <br/> Public Function getpage (): String <br/>{< br/> var STR: string = (currentpage + 1) + "/" + totalpage; <br/> return STR; <br/>}</P> <p >}< br/>}

 

 

2. Next, we need to perform paging processing on it.

 

 

3. Extended text. In order to implement the function of a prompt, a string is extended for the text.

Package COM. web <br/> {</P> <p> Import flash. text. *; <br/> // Extended text <br/> public class textfieldex extends textfield <br/> {<br/> Public var tooptip: string; <br/> Public Function textfieldex () <br/>{</P> <p >}< br/>}

 

 

4. General document classes.

 

 

 

 

 

Package <br/> {</P> <p> Import flash. display. movieclip; <br/> Import flash. display. sprite; <br/> Import flash. events. *; <br/> Import flash. text. *; </P> <p> Import flash.net. urlloader; <br/> Import flash.net. URLRequest; <br/> Import flash.net. navigatetoURL; <br/> Import COM. web. page; <br/> Import COM. web. textfieldex; <br/> public class main extends movieclip <br/>{< br/> private var list: array = new array (); <br/> PR Ivate var URL: array = new array (); // hyperlink data <br/> private var Description: array = new array (); // Brief description of the news <br/> private var count: Int = 0; // calculator for the number of clicks <br/> private var temp: Int = 0; <br/> private var Total: int; <br/> private var contain: SPRITE = new sprite (); <br/> private var page: page = new page (); // paging class <br/> private var toolcontain: SPRITE = new sprite (); <br/> public function main () <br/>{< br/> Init (); <br/>}< br/> privat E function Init (): void <br/>{< br/> page. maxrow = 10; // set the maximum number of pages <br/> var Loader: urlloader = new urlloader (); <br/> loader. addeventlistener (event. complete, completehandler); <br/> loader. addeventlistener (ioerrorevent. io_error, errorhandler); <br/> loader. load (New URLRequest ("http://www.people.com.cn/rss/politics.xml"); <br/> btn_preview.addeventlistener (mouseevent. click, onpreview); <br/> btn_next.addeventliste NER (mouseevent. click, onnext); <br/> btn_preview.buttonmode = true; <br/> btn_next.buttonmode = true; <br/> addchild (contain); <br/> addchild (toolcontain ); <br/> page. currentpage = count; <br/>}< br/> private function onpreview (Event: mouseevent): void <br/>{< br/> If (count! = 0) <br/>{< br/> count --; <br/> page. currentpage = count; <br/> showtitle (); <br/>}< br/> private function onnext (Event: mouseevent ): void <br/>{< br/> If (count <total-1) <br/>{< br/> count ++; <br/> page. currentpage = count; <br/> showtitle (); <br/>}< br/> private function completehandler (Event: Event ): void <br/>{< br/> var XML: xmlbeanxml(event.tar get. data); <br/> Read (XML); <br/> total = page. getpagenumber (List. length); // perform paging <br/> showtitle (); // display the news title <br/>}</P> <p> private function errorhandler (event: ioerrorevent): void <br/>{< br/> throw new error ("path or XML error "); <br/>}</P> <p> // read remote XML data <br/> private function read (XML: XML ): void <br/> {<br/> for each (VAR node: XML in XML .. ITEM) <br/>{< br/> list. push (node. title); <br/> URL. push (node. link); <br/> description. push (node. description); <br/>}</P> <p> // output News Title <br/> private function showtitle (): void <br/> {<br/> clearall (contain); <br/> var K: Int = 0; <br/> temp = page. maxrow * (count + 1)> list. length? List. length: page. maxrow * (count + 1); <br/> for (var I: Int = page. maxrow * count; I <temp; I ++) <br/>{< br/> var TXT: textfieldex = new textfieldex (); <br/> contain. addchild (txt); <br/> TXT. X = 30; <br/> TXT. width = 500; <br/> TXT. height = 23; <br/> TXT. y = TXT. height * k + 10; <br/> TXT. border = true; <br/> TXT. tooptip = Description [I]; <br/> txt.html text = "<a href =" "+ URL [I] +" "mce_href =" "+ URL [I] +" ">" + list [I] + "</a> "; // hyperlink <br/> TXT. addeventlistener (mouseevent. roll_over, onroll); <br/> TXT. addeventlistener (mouseevent. roll_out, onroll); <br/> TXT. selectable = false; <br/> TXT. addeventlistener (textevent. link, onlink); <br/> K ++; <br/>}< br/> pagemsg. TEXT = page. getpage (); <br/>}</P> <p> // hyperlink <br/> private function onlink (Event: textevent ): void <br/>{< br/> navigatetoURL (New URLRequest (event. text); <br/> // navigatetoURL (New URLRequest (event. currenttarget. URL); <br/>}</P> <p> private function onroll (Event: mouseevent): void <br/>{< br/> switch (event. type) <br/>{< br/> case mouseevent. roll_over: <br/> event. currenttarget. textcolor = 0xff0000; <br/> showtooltip (event. currenttarget. tooptip); <br/> break; </P> <p> case mouseevent. roll_out: <br/> event. currenttarget. textcolor = 0x000000; <br/> clearall (toolcontain); <br/> break; </P> <p >}< br/>}</P> <p> // prompt <br/> private function showtooltip (MSG: string ): void <br/> {<br/> clearall (toolcontain); <br/> var tool: textfield = new textfield (); <br/> toolcontain. addchild (Tool); <br/> tool. TEXT = MSG; <br/> tool. wordwrap = true; <br/> tool. width = 300; <br/> tool. background = true; <br/> tool. backgroundcolor = 0 xcccccc; <br/> tool. autosize = textfieldautosize. left; <br/> tool. X = mousex + 20; <br/> tool. y = Mousey; <br/>}</P> <p> // calculate the total number of pages by using the remainder method. <br/> private function clearall (contain: SPRITE): void <br/>{< br/> while (contain. numchildren> 0) <br/>{< br/> contain. removechildat (0); <br/>}< br/>}

 

 

 

 

 

 

 

 

Get XML Data Source

 

Now let's take a look at how we can get XML resources? This time, the XML we use is not performed locally, but is parsed through the XML data provided by the RSS of the external network.

 

VaR Loader: urlloader = new urlloader (); <br/> loader. addeventlistener (event. complete, completehandler); <br/> loader. addeventlistener (ioerrorevent. io_error, errorhandler); <br/> loader. load (New URLRequest ("http://www.people.com.cn/rss/politics.xml "));

After reading the XML, we save some data and use an array to display the XML title and connection address. Description.

 

 

Display Data Processing Method

After saving this, we now use interactive operations to make the XML look like pagination.

Each time, the data we read is limited. If I set the maximum number of data displayed on each page to 10, the page number is calculated and the content in the number group is displayed cyclically each time.

 

0---10

11---21

22---32

33---43

 

In this way;

Processing output conditions in a key place. Start -- limit the number of output results to determine

Temp = page. maxrow * (count + 1)> list. length? List. Length: page. maxrow * (count + 1 );
For (var I: Int = page. maxrow * count; I <temp; I ++)

 

 Private function showtitle (): void <br/>{< br/> clearall (contain); <br/> var K: Int = 0; <br/> temp = page. maxrow * (count + 1)> list. length? List. length: page. maxrow * (count + 1); <br/> for (var I: Int = page. maxrow * count; I <temp; I ++) <br/>{< br/> var TXT: textfieldex = new textfieldex (); <br/> contain. addchild (txt); <br/> TXT. X = 30; <br/> TXT. width = 500; <br/> TXT. height = 23; <br/> TXT. y = TXT. height * k + 10; <br/> TXT. border = true; <br/> TXT. tooptip = Description [I]; <br/> txt.html text = "<a href =" "+ URL [I] +" "mce_href =" "+ URL [I] +" ">" + list [I] + "</a> "; // hyperlink <br/> TXT. addeventlistener (mouseevent. roll_over, onroll); <br/> TXT. addeventlistener (mouseevent. roll_out, onroll); <br/> TXT. selectable = false; <br/> TXT. addeventlistener (textevent. link, onlink); <br/> K ++; <br/>}< br/> pagemsg. TEXT = page. getpage (); <br/>}

 

 

Mouse interaction: Page flip

After this is done, we will discuss the interaction of the mouse. When we click the button, we hope to achieve the paging effect. Here I think of a method to calculate the number of clicks

The page is displayed by the number of clicks of a person.

Private function onpreview (Event: mouseevent): void <br/>{< br/> If (count! = 0) <br/>{< br/> count --; <br/> page. currentpage = count; <br/> showtitle (); <br/>}< br/> private function onnext (Event: mouseevent ): void <br/>{< br/> If (count <total-1) <br/>{< br/> count ++; <br/> page. currentpage = count; <br/> showtitle (); <br/>}< br/>}

 

The Count variable indicates that the counter is clicked with the mouse, and 0 is initialized. When the counter is clicked, it becomes 1.

 

The number of clicks must be limited. The total number of pages cannot exceed the total number (the number of pages after the page)

 

 

Display page number

By interacting with the mouse, different pages are displayed. The page number is displayed.

 

// Display page number <br/> Public Function getpage (): String <br/>{< br/> var STR: String = (currentpage + 1) + "/" + totalpage; <br/> return STR; <br/>}

 

 

 

There are still many shortcomings, and only one simple demo can be used.

 

Problems:

Not all files in SWF can be loaded. Therefore, if the external XML file is not supported by the crossdomain. xml file, an error will occur.

For example, read the Sina rssxml file.

Http://blog.sina.com.cn/crossdomain.xml

 

It can be found that, so open SWF to read the XML file of external RSS, so compare and use this file. Check whether the other party has crossdomain. XML files.

Only the server allows it. In this way, we can open SWF to load external files without exceptions.

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/>-<cross-domain-Policy> <br/> <allow-access-from domain = "blog.sina.com.cn"/> <br/> <allow-access-from domain = "control.blog.sina.com.cn"/> <br/> <allow-access-from domain = "sjs.sinajs.cn"/> <br/> <allow-access-from domain = "simg.sinajs.cn"/> <br/> <allow-access-from domain = "icp.api.sina.com.cn"/> <br/> <allow-access-from domain = "icp.api.cws.sina.com.cn"/> <br/> <allow -Access-from domain = "api.blog.sina.com.cn"/> <br/> <allow-access-from domain = "blog.2008.sina.com.cn"/> <br/> <allow-access-from domain = "blogimg.sinajs.cn"/> <br/> <allow-access-from domain = "blogjs.sinajs.cn"/> <br/> <allow-access-from domain = "blogold.sinajs.cn"/> <br/> <allow-access-from domain = "blog.photo.sina.com.cn"/> <br/> <allow-access-from domain = "photo.sina.com.cn"/> <br/> <allow- access-from domain = "blog.sports.sina.com.cn"/> <br/> <allow-access-from domain = "circle.blog.sina.com.cn"/> <br/> <allow-access-from domain =" photo.blog.sina.com.cn "/> <br/> <allow-access-from domain =" widget.say-on.com "/> <br/> <allow-access-from domain =" edu.photo.sina.com.cn "/> <br /> <allow-access-from domain = "baby.photo.sina.com.cn"/> <br/> </Cross-Domain-Policy>

 

 

 

 

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.