Using the basic method of the ListView to achieve the effect

Source: Internet
Author: User

Daily development work often requires the ListView as a component of the layout, and many times you will encounter situations where you need to customize the ListView.

There are also some problems that do not require a custom ListView, which can be implemented based on the native ListView, but requires flexibility in handling adapter.

We now have a tricky problem: the original dropdown refresh ListView is a single item refresh, now to change to a row of two item, as a matter of principle, this should be a GridView, but to consider compatibility with the previous drop-down refresh effect, It is difficult to redefine such a GridView because it is intended to be modified based on the native ListView, because the original drop-down refresh of the ListView is itself a sub-class of the ListView.

An obvious change is in the adapter GetCount ().

This method returns the number of item that the ListView has to display altogether, but since it has now been changed to display two item in a row, the actual number displayed should be divided by 2, but given that if the data source is odd, the code is as follows:

   @Override    publicint  GetCount () {        int size = list.size ();         if (size% 2 = = 0)            {return size/2;        } Else {            return SIZE/2 + 1;        }    }

Then we have to pay attention when dealing with GetView, and now this position represents a different meaning than before.

When the previous position was 0, the expression was the first element, but here we dealt with it in GetCount, so returning 0 actually means returning the first and second elements.

If position is 0, take 0 and 1, if 1, take 2 and 3, 2, take 4 and 5 ... And so on, when we fetch, we want to position * 2 and position * 2 + 1, which means to take out the data of the left and right item on one line.

We must pay attention to the case where the last data is taken at odd times.

We can determine if position * 2 + 1 is less than the number of data, if it is greater than the last data is taken, it is necessary to hide the right item.

This allows us to implement the case where a ListView row shows two columns.

When it comes to changes in GetCount, which are generally similar, one line shows multiple item cases, but we don't recommend it when dealing with a similar situation, if you can use Grideview or Recyclerview, if it's like this, This can be done because the previous business relationship resulted in the modification of the existing ListView.

The ListView, as a sliding control, has many effects that require some sort of response when slipping to a position. The implementation of this kind of effect can be very complex, or it can be slightly simpler, depending on the situation.

The requirement we meet is that the ListView will display a control when a control slides out of the screen.

This requirement is not particularly complex, as long as we determine the coordinates of the controls that slide out of the screen.

Sliding monitoring of all controls that can be slid can be in the Onscroll method.

To determine whether a control slides out of the screen, we first need to know the extent of the screen.

1 New Point (); 2 Getwindowmanager (). Getdefaultdisplay (). GetSize (p); 3 int screenwidth = p.x; 4 int screenheight = p.y; 5 New Rect (0, 0, screenwidth, screenheight);

This rect is the coordinate range of the screen.

Then we get the coordinate range of a control.

int New int [2];view.getlocationinwindow (location);

The coordinate range of this control is placed inside the location.

We then determine whether a control is visible in a range, that is, within a rect, and this method can be called:

View.getlocalvisiblerect (Rect)

Any complex effect is based on the basic properties and methods of the underlying control, although the implementation will be trivial, but we see the simple method invocation, its encapsulation implementation is very trivial.

Some of the special effects of controls involved in Android, especially when it comes to coordinate changes, require our special care, as long as we find ideas, there are ways to achieve them.

Using the basic method of the ListView to achieve the effect

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.