解决组件与父组件监听冲突问题
解决组件与父组件监听冲突问题
相信大家在做android开发的时候,都会有遇到组件与父组件的监听相互冲突的时候。
举个具体的例子:在游戏详情Activity中使用了一个横向的ListView为了显示图片,但是为了翻阅方便,我们通常会在这个Activity中使用一个ScrollView显示,这样的话,就不用担心文本中信息过长而导致的界面无法显示,虽然这样能够让我们的界面更加美观,但也增加了一个难题,就是在ListView中拖拽时,父组件(也就是ScrollView)也会监听到这样的一个事件,于是当你想拖动ListView时,有可能会没有反应,但是ScrollView却有可能做出相应的反应。其实解决这个问题的关键代码非常简单,只需要一句requestDisallowInterceptTounchEvent();
下面是效果图:
当ListView拖动时:
当ScrollView拖动时:
以下便直接上代码:
主界面代码:
- <span style=""><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <ScrollView
- android:id="@+id/scrollView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <com.example.controls.HorizontalListView
- android:id="@+id/HorizontalListView"
- android:layout_height="270dp"
- android:layout_width="wrap_content"
- android:layout_marginBottom="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="10dp" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:text="游戏名称:窦豆豆"
- android:textSize="24sp" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:text="游戏大小:24M"
- android:textSize="24sp" />
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:text="下载次数:500W次"
- android:textSize="24sp" />
- <TextView
- android:id="@+id/textView4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="游戏简介"
- android:layout_marginBottom="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:textSize="24sp" />
- <TextView
- android:id="@+id/textView5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="这是一个游戏简介\n这是一个游戏简介\n这是一个游戏简介\n这是一个游戏简介\n"
- android:layout_marginBottom="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:textSize="24sp" />
- </LinearLayout>
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
- </span>
这是ListView每个Item的代码
- <span style=""><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_margin="10dp"
- android:src="@drawable/ic_launcher" />
- </LinearLayout>
- </LinearLayout>
- </span>
主界面java代码:
- <span style="">package com.example.listviewdemo;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ScrollView;
- import com.example.controls.HorizontalListView;
- public class MainActivity extends Activity {
- private ScrollView scrollView;
- private HorizontalListView horizontalListView;
- private List<Integer> list = new ArrayList<Integer>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- scrollView = (ScrollView) findViewById(R.id.scrollView);
- horizontalListView = (HorizontalListView) findViewById(R.id.HorizontalListView);
- horizontalListView.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View arg0, MotionEvent event) {
- // TODO Auto-generated method stub
- if (event.getAction() == MotionEvent.ACTION_UP) {
- scrollView.requestDisallowInterceptTouchEvent(false);
- } else {
- scrollView.requestDisallowInterceptTouchEvent(true);
- }
- return false;
- }
- });
- setImageView(horizontalListView);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- private void setImageView(HorizontalListView horizontalListView){
- list.add(R.drawable.pic_game);
- list.add(R.drawable.pic_game);
- list.add(R.drawable.pic_game);
- horizontalListView.setAdapter(new MyAdapter(list, getLayoutInflater()));
- }
- protected class MyAdapter extends BaseAdapter{
- private List<Integer> list;
- private LayoutInflater layoutInflater;
- public MyAdapter(List<Integer> list,LayoutInflater layoutInflater){
- this.list = list;
- this.layoutInflater = layoutInflater;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return list.size();
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return list.get(arg0);
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- // TODO Auto-generated method stub
- int image = list.get(arg0);
- View view = layoutInflater.inflate(R.layout.item, null);
- ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
- iv.setImageResource(image);
- return view;
- }
- }
- }
- </span>