English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Frontier: Sometimes, the fields that need to be displayed in the listview item are quite a few. Considering the display issue, the item has to be nested inside a ScrollView to achieve this, and then the problem arises: when the listview needs to handle click events, due to the nested use of ScrollView, it intercepts the listview click event: it is only possible to rewrite the listview to implement it.
/** * * @author Author: Yihuangxing * * @da2016Year10Month24Day * * @toTODO Class description: Solution to the problem of nesting ScrollView within ListView, where ScrollView intercepts ListView's item click events * * * When nesting ScrollView within listview, horizontal and vertical scrolling are normal, but cannot click on listview items. After consulting Android dispatch mechanism, the issue was resolved by inheriting ListView and overriding ListView's onInterceptTouchEvent. * * Always call listview's onTouchEvent in onInterceptTouchEvent to ensure that all listview events are executed. * super.onInterceptTouchEvent(ev) will not intercept the horizontal scroll that needs to be passed to ScrollView. */ public class MyListView extends ListView { private int flag = 0; private float StartX; private float StartY; public MyListView(Context context) { super(context); // TODO Auto-Generated constructor stub } public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-Generated constructor stub } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-Generated constructor stub } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // Always call the touch event handler of listview. onTouchEvent(ev); if (ev.getAction() == MotionEvent.ACTION_DOWN) { StartX = ev.getX(); StartY = ev.getY(); return false; } if (ev.getAction() == MotionEvent.ACTION_MOVE) { float ScollX = ev.getX(); - StartX; float ScollY = ev.getY(); - StartY; // Determine whether it is a horizontal or vertical swipe. If it is a vertical swipe, intercept the move event and up event (not intercepting will cause juddering when both ListView and ScrollView perform sliding at the same time). if (Math.abs(ScollX) < Math.abs(ScollY)) { flag = 1; return true; } return false; } if (ev.getAction() == MotionEvent.ACTION_UP) { if (flag == 1{ return true; } return false; } return super.onInterceptTouchEvent(ev); } }
The above-mentioned solution to nesting ScrollView in Android ListView items introduced by the editor is for your reference. If you have any questions, please leave a message, and the editor will reply to you in time. Thank you very much for your support of the Yell Tutorial website!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)