English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Solução para item de ListView do Android anexar ScrollView

 Fronteira: Às vezes, os campos que os itens do ListView precisam exibir são muitos, considerando o problema de exibição, é necessário que o item esteja dentro de um ScrollView para implementar, então surge um problema: quando o ListView precisa de eventos de clique, devido ao uso aninhado do ScrollView, ele intercepta o evento de clique do ListView: só pode rewritten o ListView para implementar.

/**
* 
* @author Author: Yihuangxing
* 
* @da2016Year10Month24Day
* 
* @toTODO Class description: Solution to the problem of nesting ScrollView within ListView, where ScrollView intercepts the ListView item click event
* 
* 
* When nesting ScrollView within listview, horizontal and vertical scrolling are normal, but it is impossible to click on the listview item. After querying the Android dispatch mechanism, the problem was solved 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 scrolling 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;
// Determinar se é um deslize horizontal ou vertical, se for vertical, interceptar os eventos move e up (não interceptar causará trancamento ao executar simultaneamente o deslize do ListView e ScrollView)
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);
}
}

O que foi mencionado acima é a solução que o editor apresentou para o problema de nested ScrollView no item do Android ListView, esperando ajudar a todos. Se você tiver alguma dúvida, por favor, deixe um comentário, o editor responderá a todos o mais rápido possível. Agradecemos também o apoio ao site Tutorial de Grito!

Declaração: O conteúdo deste artigo é de origem na Internet, pertence ao respectivo proprietário, é contribuído e carregado voluntariamente pelos usuários da Internet, este site não possui direitos de propriedade, não foi editado artificialmente e não assume responsabilidade legal relevante. Se você encontrar conteúdo suspeito de violação de direitos autorais, por favor, envie um e-mail para: notice#oldtoolbag.com (ao enviar e-mail, substitua # por @ para denunciar, e forneça provas relacionadas. Em caso de verificação, o site deletará imediatamente o conteúdo suspeito de violação de direitos autorais.)

Você também pode gostar