How to judge the left and right sliding direction in Android programming
This article describes the method of ViewPage judging the left and right sliding directions in Android programming. Share it for your reference, as follows:
package ; import ; import .; import ; import ; import ; /** * Rewrite, added a method to determine the sliding direction * @author zxy * */ public class meityitianViewPager extends ViewPager { private boolean left = false; private boolean right = false; private boolean isScrolling = false; private int lastValue = -1; private ChangeViewCallback changeViewCallback = null; public meityitianViewPager(Context context, AttributeSet attrs) { super(context, attrs); init(); } public meityitianViewPager(Context context) { super(context); init(); } /** * init method . */ private void init() { setOnPageChangeListener(listener); } /** * listener ,to get move direction . */ public OnPageChangeListener listener = new OnPageChangeListener() { @Override public void onPageScrollStateChanged(int arg0) { if (arg0 == 1) { isScrolling = true; } else { isScrolling = false; } ("meityitianViewPager", "meityitianViewPager onPageScrollStateChanged : arg0:" + arg0); if (arg0 == 2) { ("meityitianViewPager", "meityitianViewPager onPageScrollStateChanged direction left ? " + left); ("meityitianViewPager", "meityitianViewPager onPageScrollStateChanged direction right ? " + right); //notify .... if(changeViewCallback!=null){ (left, right); } right = left = false; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { if (isScrolling) { if (lastValue > arg2) { // Decrease, slide to the right right = true; left = false; } else if (lastValue < arg2) { // Decrease, slide to the right right = false; left = true; } else if (lastValue == arg2) { right = left = false; } } ("meityitianViewPager", "meityitianViewPager onPageScrolled last :arg2 ," + lastValue + ":" + arg2); lastValue = arg2; } @Override public void onPageSelected(int arg0) { if(changeViewCallback!=null){ (arg0); } } }; /** * Get whether to slide to the right * @return true Swipe right */ public boolean getMoveRight(){ return right; } /** * Get whether to slide to the left * @return true Slide for left */ public boolean getMoveLeft(){ return left; } /** * Sliding state changes callback * @author zxy * */ public interface ChangeViewCallback{ /** * Switch view? Depends on left and right. * @param left * @param right */ public void changeView(boolean left,boolean right); public void getCurrentPageIndex(int index); } /** * set ... * @param callback */ public void setChangeViewCallback(ChangeViewCallback callback){ changeViewCallback = callback; } }
I hope this article will be helpful to everyone's Android programming design.
Related Articles
Android custom controls implement universal verification code input box (II)
This article mainly introduces the second article of Android custom controls to implement a universal verification code input box. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.2021-01-01Android implements vertical progress bar VerticalSeekBar
This article mainly introduces the relevant information on Android's implementation of VerticalSeekBar, which has certain reference value. Interested friends can refer to it.2018-07-07Android soft keyboard pops up and other problems such as hidden squeezing interface
This article mainly introduces the relevant knowledge of various issues such as the pop-up of Android soft keyboards, such as hidden extrusion interface. This article introduces you very detailed and has reference value. Interested friends can take a look.2016-11-11Summary of the way to save data in Android application development
This article mainly introduces a summary of the data storage methods in Android application development, including the selection of core codes implemented by ROM, SD card, and SharedPreference. Friends who need it can refer to it2016-02-02Simple implementation of Android student management system (with source code)
This article mainly introduces how to simply implement the Android student management system, which is especially suitable for students who are about to graduate from computer majors to learn and learn to learn from and create a student management system. Interested friends can refer to it.2015-12-12Android uses open source component PagerBottomTabStrip to implement bottom menu and top navigation functions
This article mainly introduces Android to use PagerBottomTabStrip to implement the bottom menu and top navigation function. This article introduces you very detailed and has certain reference value. Friends who need it can refer to it.2018-08-08A brief discussion on the use of android nexus private server
This article mainly introduces a brief discussion on the use of Android Nexus private server. The editor thinks it is quite good. I will share it with you now and give you a reference. Let's take a look with the editor2017-12-12How to solve the problem of getting the control to return empty in Android
This article mainly introduces the relevant information on how to solve the problem of getting the control return empty in Android. It is very good and has reference value. Friends who need it can refer to it.2016-06-06Simple way to implement Flutter's ban on horizontal screens on your phone
The app can be horizontally screen by default. If you need to ban horizontal screen, you can refer to this article. This article mainly introduces to you the simple implementation method of Flutter banning horizontal screen on mobile phones. Friends who need it can refer to it.2021-07-07Android class FileDownloadList analysis
This article mainly introduces the analysis of Android FileDownloadList. Friends who need it can refer to it.2014-07-07