解决组件与父组件监听冲突问题

解决组件与父组件监听冲突问题

相信大家在做android开发的时候,都会有遇到组件与父组件的监听相互冲突的时候。

举个具体的例子:在游戏详情Activity中使用了一个横向的ListView为了显示图片,但是为了翻阅方便,我们通常会在这个Activity中使用一个ScrollView显示,这样的话,就不用担心文本中信息过长而导致的界面无法显示,虽然这样能够让我们的界面更加美观,但也增加了一个难题,就是在ListView中拖拽时,父组件(也就是ScrollView)也会监听到这样的一个事件,于是当你想拖动ListView时,有可能会没有反应,但是ScrollView却有可能做出相应的反应。其实解决这个问题的关键代码非常简单,只需要一句requestDisallowInterceptTounchEvent();

下面是效果图:

当ListView拖动时:

 

当ScrollView拖动时:



     以下便直接上代码:

主界面代码:

Xml代码 
  1. <span style=""><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <ScrollView  
  8.         android:id="@+id/scrollView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" >  
  11.   
  12.         <LinearLayout  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="wrap_content"  
  15.               
  16.             android:orientation="vertical" >  
  17.               
  18.             <com.example.controls.HorizontalListView  
  19.                 android:id="@+id/HorizontalListView"         
  20.                 android:layout_height="270dp"                 
  21.                 android:layout_width="wrap_content"  
  22.                 android:layout_marginBottom="10dp"  
  23.                 android:layout_marginLeft="10dp"  
  24.                 android:layout_marginRight="10dp"  
  25.                 android:layout_marginTop="10dp" />  
  26.   
  27.             <LinearLayout  
  28.                 android:layout_width="match_parent"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:orientation="vertical"  
  31.                  >  
  32.   
  33.                 <TextView  
  34.                     android:id="@+id/textView2"  
  35.                     android:layout_width="wrap_content"  
  36.                     android:layout_height="wrap_content"  
  37.                     android:layout_marginBottom="10dp"  
  38.                     android:layout_marginLeft="10dp"  
  39.                     android:layout_marginTop="10dp"  
  40.                     android:text="游戏名称:窦豆豆"  
  41.                     android:textSize="24sp" />  
  42.   
  43.                 <TextView  
  44.                     android:id="@+id/textView1"  
  45.                     android:layout_width="wrap_content"  
  46.                     android:layout_height="wrap_content"  
  47.                     android:layout_marginBottom="10dp"  
  48.                     android:layout_marginLeft="10dp"  
  49.                     android:layout_marginTop="10dp"  
  50.                     android:text="游戏大小:24M"  
  51.                     android:textSize="24sp" />  
  52.   
  53.                 <TextView  
  54.                     android:id="@+id/textView3"  
  55.                     android:layout_width="wrap_content"  
  56.                     android:layout_height="wrap_content"  
  57.                     android:layout_marginBottom="10dp"  
  58.                     android:layout_marginLeft="10dp"  
  59.                     android:layout_marginTop="10dp"  
  60.                     android:text="下载次数:500W次"  
  61.                     android:textSize="24sp" />  
  62.   
  63.                 <TextView  
  64.                     android:id="@+id/textView4"  
  65.                     android:layout_width="wrap_content"  
  66.                     android:layout_height="wrap_content"  
  67.                     android:text="游戏简介"  
  68.                     android:layout_marginBottom="10dp"  
  69.                     android:layout_marginLeft="10dp"  
  70.                     android:layout_marginTop="10dp"  
  71.                     android:textSize="24sp" />  
  72.   
  73.                 <TextView  
  74.                     android:id="@+id/textView5"  
  75.                     android:layout_width="wrap_content"  
  76.                     android:layout_height="wrap_content"  
  77.                     android:text="这是一个游戏简介\n这是一个游戏简介\n这是一个游戏简介\n这是一个游戏简介\n"  
  78.                      android:layout_marginBottom="10dp"  
  79.                     android:layout_marginLeft="10dp"  
  80.                     android:layout_marginTop="10dp"  
  81.                     android:textSize="24sp" />  
  82.                   
  83.             </LinearLayout>  
  84.               
  85.         </LinearLayout>  
  86.     </ScrollView>  
  87.   
  88. </LinearLayout>  
  89. </span>  

 这是ListView每个Item的代码

Xml代码 
  1. <span style=""><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="vertical" >  
  11.   
  12.         <ImageView  
  13.             android:id="@+id/imageView1"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_gravity="center"  
  17.             android:layout_margin="10dp"  
  18.             android:src="@drawable/ic_launcher" />  
  19.   
  20.     </LinearLayout>  
  21.   
  22. </LinearLayout>  
  23. </span>  

     主界面java代码:

Java代码 
  1. <span style="">package com.example.listviewdemo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.LayoutInflater;  
  9. import android.view.Menu;  
  10. import android.view.MotionEvent;  
  11. import android.view.View;  
  12. import android.view.View.OnTouchListener;  
  13. import android.view.ViewGroup;  
  14. import android.widget.BaseAdapter;  
  15. import android.widget.ImageView;  
  16. import android.widget.ScrollView;  
  17.   
  18. import com.example.controls.HorizontalListView;  
  19.   
  20. public class MainActivity extends Activity {  
  21.       
  22.     private ScrollView scrollView;  
  23.     private HorizontalListView horizontalListView;  
  24.     private List<Integer> list = new ArrayList<Integer>();  
  25.       
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.           
  31.         scrollView = (ScrollView) findViewById(R.id.scrollView);  
  32.         horizontalListView = (HorizontalListView) findViewById(R.id.HorizontalListView);  
  33.         horizontalListView.setOnTouchListener(new OnTouchListener() {             
  34.             @Override  
  35.             public boolean onTouch(View arg0, MotionEvent event) {  
  36.                 // TODO Auto-generated method stub  
  37.                 if (event.getAction() == MotionEvent.ACTION_UP) {  
  38.                     scrollView.requestDisallowInterceptTouchEvent(false);  
  39.                 } else {  
  40.                     scrollView.requestDisallowInterceptTouchEvent(true);  
  41.                 }  
  42.                 return false;  
  43.             }  
  44.         });  
  45.         setImageView(horizontalListView);  
  46.     }  
  47.   
  48.     @Override  
  49.     public boolean onCreateOptionsMenu(Menu menu) {  
  50.         // Inflate the menu; this adds items to the action bar if it is present.  
  51.         getMenuInflater().inflate(R.menu.main, menu);  
  52.         return true;  
  53.     }  
  54.       
  55.     private void setImageView(HorizontalListView horizontalListView){  
  56.         list.add(R.drawable.pic_game);  
  57.         list.add(R.drawable.pic_game);  
  58.         list.add(R.drawable.pic_game);  
  59.         horizontalListView.setAdapter(new MyAdapter(list, getLayoutInflater()));  
  60.     }  
  61.       
  62.     protected class MyAdapter extends BaseAdapter{  
  63.           
  64.         private List<Integer> list;  
  65.         private LayoutInflater layoutInflater;  
  66.           
  67.         public MyAdapter(List<Integer> list,LayoutInflater layoutInflater){  
  68.             this.list = list;  
  69.             this.layoutInflater = layoutInflater;  
  70.         }  
  71.           
  72.         @Override  
  73.         public int getCount() {  
  74.             // TODO Auto-generated method stub  
  75.             return list.size();  
  76.         }  
  77.   
  78.         @Override  
  79.         public Object getItem(int arg0) {  
  80.             // TODO Auto-generated method stub  
  81.             return list.get(arg0);  
  82.         }  
  83.   
  84.         @Override  
  85.         public long getItemId(int arg0) {  
  86.             // TODO Auto-generated method stub  
  87.             return arg0;  
  88.         }  
  89.   
  90.         @Override  
  91.         public View getView(int arg0, View arg1, ViewGroup arg2) {  
  92.             // TODO Auto-generated method stub  
  93.             int image = list.get(arg0);  
  94.             View view = layoutInflater.inflate(R.layout.item, null);  
  95.             ImageView iv = (ImageView) view.findViewById(R.id.imageView1);  
  96.             iv.setImageResource(image);  
  97.             return view;  
  98.         }  
  99.           
  100.     }  
  101.   
  102. }  
  103. </span>  

猜你喜欢

转载自839299993.iteye.com/blog/2208405