如果一个布局中同事引用到了listview 和scrollview 两种滚动控件,那么listview 的滚动效果将被屏蔽掉,那么此时应加上dispatchTouchEvent()方法,然后选择先执行滚动的控件加上次方法。
代码展示如下:
package com.example.android_listview_scrollview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
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 ArrayAdapter<String> adapter;
private ScrollView scrollview;
boolean is_move=false;
boolean is_scroll_move =false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView) this.findViewById(R.id.listView1);
scrollview= (ScrollView) this.findViewById(R.id.scrollView1);
adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getdata());
listview.setAdapter(adapter);
public List<String> getdata(){
List<String> list=new ArrayList<String>();
for(int i=0;i<30;i++){
list.add("rose"+i);
}
return list;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(ev.getAction()==MotionEvent.ACTION_MOVE){
//想让那个控件先执行就添加改事件的方法
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;
}
}