85 Android ListView 和 ScrollView 事件冲突

avctivity_main.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="37dp"
        android:layout_marginTop="43dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#00ff00"
             >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="290dp"
                android:layout_height="270dp" >
            </ListView>

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@drawable/a1" />
              <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@drawable/a2" />
                <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:src="@drawable/a3" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>



MainActivity.java

package com.example.amdroid_listview_scrollview;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ViewTreeObserver.OnScrollChangedListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;

public class MainActivity extends Activity {

	private ListView listView;
	private ScrollView scrollView;
	private ArrayAdapter<String> adapter;
	private boolean is_move;
	private boolean is_scrollView_move;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView=(ListView)findViewById(R.id.listView1);
		scrollView=(ScrollView)findViewById(R.id.scrollView1);
		adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getData());
		listView.setAdapter(adapter);
		
		listView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				if(is_move&& scrollState==OnScrollListener.SCROLL_STATE_IDLE)
				{
					//琛ㄧず鐢ㄦ埛浠巐istView 婊戝姩鍒颁綆绔�
					is_scrollView_move=true;
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				
				is_move=(firstVisibleItem+visibleItemCount==totalItemCount);
			}
		});
		
	}
	
	public List<String> getData()
	{
		List<String> list=new ArrayList<String>();
		for(int i=1;i<=30;i++)
		{
			list.add("object"+i);
		}
		return list;
	}

	/**
	 * 鍒嗗彂浜嬩欢
	 */
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
//		// TODO Auto-generated method stub
//		if(ev.getAction()==MotionEvent.ACTION_MOVE)
//		{
//			listView.dispatchTouchEvent(ev);
//		}
//		if(is_scrollView_move&&ev.getAction()==MotionEvent.ACTION_MOVE)
//		{
//			scrollView.dispatchTouchEvent(ev);
//		}
		
		if(ev.getAction()==MotionEvent.ACTION_MOVE)
		{
			if(is_scrollView_move)
			{
				scrollView.dispatchTouchEvent(ev);
			}
			else{
				listView.dispatchTouchEvent(ev);
			}
		}
		return super.dispatchTouchEvent(ev);
		
		
	}
	@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;
	}

}



你可能感兴趣的:(85 Android ListView 和 ScrollView 事件冲突)