Today in the optimization of Bluetooth, found that the old version of the ListView Listselector in the scroll up and down when there is a cross-border situation, the manager said to solve the problem,
Can not affect the experience, I also went to play the next, found that it is really ugly, so it took some time to solve this bug, by the way to share the solution, I hope to encounter the same
Kind of situation of friends, can walk less fruitless, increase development efficiency
Let's take a look at this cross-border situation:
First, select any item, scroll up and down to see if the selected color is out of bounds
Scroll up the bounds, that is, no matter where you scroll up, the orange item will always be on Top, just like Excel's pinned, which affects the experience
Down the
So, how to solve it? So for the reason, in fact the root cause I did not understand, I think of a general solution, is to control the color by scrolling events to display and do not need to display the visible range can be
Write the following code:
Mphonebooklist.setonscrolllistener (Monscrolllistener);p rivate onscrolllistener monscrolllistener = new Onscrolllistener () {/*** when scrolling state changed */@Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {// 1th time: Scrollstate = scroll_state_touch_scroll (1) scrolling//2nd time: Scrollstate = scroll_state_fling (2) The finger did the throwing action (the finger left the screen before, A hard slide)///3rd time: scrollstate = scroll_state_idle (0) Stop scrolling//do not scroll when saving the current scroll to the position if (scrollstate = = Onscrolllistener.scroll_state _idle) {//Stop processing}}/*** scrolling when called */@Overridepublic void onscroll (abslistview view, int firstvisibleitem,int VisibleItemCount, int totalitemcount) {//Scroll all the time until the callback stops when scrolling stops. Callback once when clicked. Firstvisibleitem: The first list item ID currently visible (starting at 0)//visibleitemcount: The number of list items currently visible (small half is counted)//totalitemcount: List item total number int last = Visibleitemcount+firstvisibleitem-1; LOG.D ("engineer-jsp", "Onscroll () ListView Check ID:" +mcurrentposition+ "\ nthe first list item currently visible:" +firstvisibleitem+ "\ n the number of list items currently visible: "+visibleitemcount+" \ n the last item currently visible: "+last+" \n\n**************************************************** *******************"); int color = r.color.orangered;if (mcurrentposition<firstvisibleitem| | Mcurrentposition>last) {color = r.color.transparence;} Mphonebooklist.setselector (color);}};
Logical thinking: Since he no matter how much before or after, it will show that the visible range of the ListView as the Listselector range of activities, outside the scope of the set to invisible, that is, transparent state
I add a scrolling event to the ListView in the Code and let the Onscroll () function control the listselector so that the bug can be fundamentally resolved
First , the meaning of the onscroll () function
Firstvisibleitem: The first list item ID (starting from 0) that can be seen at the moment
VisibleItemCount: Number of list items currently visible (small half is counted)
Totalitemcount: Total number of list items
abslistview: Do not use, do not explain
Just place the Item in the dot position in Firstvisibleitem before and after the last item of the visible list item, the last item obtained is
Test picture, randomly select an Item:
Again do scroll up and down, did not appear in the above picture of the situation:
On the Android ListView listselector to solve cross-border issues