Android Gallery sliding stop listening solution (thread-less)

Source: Internet
Author: User

I have checked a lot of Gallery methods on the Internet to stop listening to the location. They are all determined by starting a thread and then sleeping. I don't think it is necessary to open a thread, consume resources, and raise your hand.

When I accidentally saw the source code of the image library, I saw a piece of code, packages \ apps \ Gallery \ src \ com \ android \ camera \ ViewImage. java.

 

1
<Strong> private void scheduleDismissOnScreenControls (){
2
MHandler. removeCallbacks (mDismissOnScreenControlRunner );
3
MHandler. postDelayed (mDismissOnScreenControlRunner, 2000); // latency
4
 
5
} </Strong>
Combined with the OnItemSelectedListener of gallery, you can perform some operations after the sliding stop:

 

01
Class MyOnSelectLinstener implements OnItemSelectedListener {
02
@ Override
03
Public void onItemSelected (AdapterView <?> Parent, View view, int position, long id ){
04
ScheduleDismissOnScreenControls ();
05
}
06
Public void onNothingSelected (AdapterView <?> Parent ){
07
}
08

09
}
10
Private void scheduleDismissOnScreenControls (){
11
MHandler. removeCallbacks (mDismissOnScreenControlRunner );
12
MHandler. postDelayed (mDismissOnScreenControlRunner, 2000 );
13
}
Execute the operation in Runnable. When Gallery slides and switches pages, mHandler first deletes Runnable from the queue, so the operation is not executed, and then adds the delayed Runnable in the queue, if you do not flip the page again within the delay period, you can perform the Runnable operation here in gallery. When you drag the page, you can skip the Runnable method.

In this way, the listener can be stopped by sliding.


Author: dipper

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.