雖然說Google 也不建議在 ScrollView 中再放 ListView 可是樣本是要這樣做沒辦法
最後解決方案是
Eclipse Project : TestListInScroll.rar
在ListView OnTouch Down 的時候 設計 target 為自己, MOVE 的時候 Scroll 自己 , UP 的時候 設回target 為 ScrollView
而ScrollView 的OnTouch 則要判斷target 是否為自己 如果不是就 停住不移動
以下是 Code:
package com.test.listinscroll; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.ScrollView; public class TestListInScrollActivity extends Activity { private int scrollTarget = 0; private ListView lv1, lv2, lv3; private ScrollView sv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); super.onCreate(savedInstanceState); setContentView(R.layout.list_in_scroll); ArrayList佈局XMLlist = new ArrayList (); for(int i = 0; i < 50; i++){ list.add("Item"); } ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, list); sv = (ScrollView)this.findViewById(R.id.scrollview1); lv1 = (ListView)this.findViewById(R.id.listView1); lv2 = (ListView)this.findViewById(R.id.listView2); lv3 = (ListView)this.findViewById(R.id.listView3); lv1.setAdapter(adapter); lv2.setAdapter(adapter); lv3.setAdapter(adapter); sv.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { if(scrollTarget != 0){ return true; } return false; } }); lv1.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ scrollTarget = 1; }else if(event.getAction() == MotionEvent.ACTION_MOVE){ lv1.scrollBy(0, 1); }else if(event.getAction() == MotionEvent.ACTION_UP){ scrollTarget = 0; } return false; } }); lv2.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ scrollTarget = 2; }else if(event.getAction() == MotionEvent.ACTION_MOVE){ lv2.scrollBy(0, 1); }else if(event.getAction() == MotionEvent.ACTION_UP){ scrollTarget = 0; } return false; } }); lv3.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ scrollTarget = 3; }else if(event.getAction() == MotionEvent.ACTION_MOVE){ lv3.scrollBy(0, 1); }else if(event.getAction() == MotionEvent.ACTION_UP){ scrollTarget = 0; } return false; } }); } }
Can u also provide xml file ... Thanks
ReplyDelete