Simple horizontal ListView implementation (version 4.0), listviewversion

Source: Internet
Author: User

Simple horizontal ListView implementation (version 4.0), listviewversion

This version of blog is quite easy to write, and some codes have limited language organization capabilities. I feel that it is very difficult to describe them. I have changed the article five or six times before and after, but it is still unsatisfactory, however, I still insist on writing out my original ideas. If I cannot understand the content, I will still upload the source code at the end of the article, and I can directly run it to see the effect, after reading the running results, we can intuitively understand some awkward languages in the text. In version 3.0, although the items in listView are moved along with the left and right fingers, the following situations may occur:

When the first one is on the left, the following situation occurs (you can still move it to the right ):

When the right side is the last item of the adapter, the following situation occurs (you can still move it to the left ):

Normally, this version 4.0 will solve this problem when the first and last on the Left cannot be rolled. Before solving this problem, let's talk about the relevant knowledge points:

Knowledge Point 1): When the finger moves on the screen, other related methods will occur...> onScroll --> onScroll...> other related methods.

Knowledge Point 2): Version 3.0 uses the onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) method when processing finger scrolling. This method has a distanceX parameter, we know that distanceX> 0 is used when the finger wants to move, and distanceX is used to move the finger to the right. This is because the distanceX value is determined by the second parameter: distanceX = e2.getX () of the onScroll method used last time-e2.getX () of the current onScroll method or abbreviated as lastEvent2.getX ()-currentEvent2.getX () = distanceX;

In version 3.0, we directly use the distanceX variable (the opposite number of the variable value to simulate the moving and rolling distance between left and right ), however, to solve the above problem, we will not directly use distanceX, but define two new variables:

Variable: preTotalDistanceX: Sum of the distances generated by all previous onScroll calls before the current onScroll method is called. For example, if onScroll is called n times, then preTotalDistanceX = the sum of distanceX in onScroll for the first n-1 calls;

Variable: totalDistanceX: The sum of all distanceX in the onScroll method call currently. For example, the onScroll method is called n times, totalDistanceX is the sum of distanceX in the parameters of n onScrll method calls;

Then the rolling distance is distanceX = totalDistanceX-preTotalDistanceX.

Use the code to indicate the total distance as follows:

            public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {totalDistanceX += distanceX;requestLayout();return true;};

The onLayout method of this version is modified as follows:

int distanceX = totalDistanceX - preTotalDistanceX; removeAnvisiableViews(-distanceX);addRightChildViews(-distanceX);addLeftChildViews(-distanceX);layoutChildViews(-distanceX);                preTotalDistanceX = totalDistanceX;
To solve the problem raised at the beginning of the article, The key is to make distanceX = 0; that is, pretotalDistanceX = totalDistanceX;So the key to solving the problem is how to make the two variables equal. From knowledge point 2, let's assume that the user's Right and left: At this time, the leftmost item is the first item of the adapter, so the listView cannot scroll to the right when the finger moves to the right, but the variable totalDistanceX is <0, and the Cartesian coordinate system is far away from 0 and the negative increments, so here we can preliminarily judge: When totalDistanceX <= 0, let totalDistanceX = 0; in this way, preoTotalDistanceX = totalDistanceX = 0, this will make distanceX = 0, so that our ListView will not move to the right; assume that the user's hand Move the cursor to the left and right: When the finger moves to the left, totalDistanceX> 0 increases progressively. When the finger moves to the right, totalDistanceX decreases progressively, if totalDistanceX is changed to a negative value when the value is less than 0, moving to the right is similar to that of the right and left fingers. This is the key point, the entire process of moving left and right is like a physical shift. When totalDistanceX = 0, we can conclude that we cannot scroll right.

So I made the following judgment in the onLayout code:

@ Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) {if (listAdapter = null) {return ;} /** make sure that the first node on the left of the party is not rolled */if (totalDistanceX <= 0) {totalDistanceX = 0;} int distanceX = totalDistanceX-preTotalDistanceX; removeAnvisiableViews (-distanceX); addRightChildViews (-distanceX); addLeftChildViews (-distanceX); layoutChildViews (-distanceX); dependencies = totalDistanceX ;}

The idea above is true when running a notebook. So far, the first problem at the beginning of the blog has been solved.
The second problem is solved below: when the last item in the adapter is added to the viewGroup and displayed completely, the listView is prohibited from rolling to the left.

First, there is no full display of the item, for example:



The key to solving the second problem is how to make distanceX = totalDistanceX-pretotalDistanceX = 0; that is, how to make totalDistanceX = preototalDistanceXAccording to the illustration, we can easily draw the following conclusion: to display the last item, let the listView scroll to the X size distance based on the total rolling distance, the above equation is represented as totalDistanceX = X + pretotalDistanceX. So when totalDistanceX> X + pretotalDistanceX, let totalDistanceX = X + preototalDistanceX; in the program, I use scrollXMax to represent the value of X + preototalDistanceX (this variable name is a bit junk); in the code, I use:

//scrollXMax = X + pretotalDistanceXif(totalDistanceX>scrollXMax) {    totalDistanceX = scrollXMax;}......preototalDistanceX = totalDistanceX;
In this way, if (totalDistanceX> scrollXMax is still valid when the last item is drawn to listView by calling layout when X distance is rolled ). In this case, distanceX = 0. But I have already said it. Where can I add the code X + preDistanceX? Based on the previous blogs, it is not difficult to find out how to add them in the addRightChildViews method. The Code is as follows:

Private void addRightChildViews (int distanceX) {// 2. display the Item as much as possible on the screen. Note that at the beginning, there was no View rightChildView = getChildAt (getChildCount ()-1); // you can get the distance from the Right Border of this childView to the Left Border of parentView. int rightEdge = rightChildView! = Null? RightChildView. getRight (): 0; while (rightEdge + distanceX <getWidth () & rightIndex <listAdapter. getCount () {View child = listAdapter. getView (rightIndex, null, null); child = measureChild (child); addViewInLayout (child,-1, child. getLayoutParams (), true); rightEdge + = child. getMeasuredWidth (); // judge the last itemif (rightIndex = listAdapter. getCount ()-1) {scrollXMax = rightEdge + preTotalDistanceX-getWidth () ;} rightIndex ++ ;}}
At this point, the above two problems have been successfully solved; I had a lot of detours during the whole version, and I had a small sense of accomplishment after a long time; what is programming? To put it bluntly, we use this programming language to describe or express the programmer's ideas while following the rules of the feature programming language. In the implementation of Version 4.0, I wrote and painted on paper. After the idea was clear, I naturally expressed this idea using programming languages. This is programming! The version has not been completed yet. For example, if you cannot click here, the remaining problems will be solved in the next version. If time permits, you can write them directly. The source code of the Project is here. You are welcome to criticize and correct 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.