0

我有一個ListViewTimer我通過調用smoothScrollToPositionFromTop(position, offset, duration)來控制此列表的滾動移動。 ListView擁有OnItemClickListener的收聽者。點擊更改Android平滑滾動行爲

如果在平滑滾動發生時單擊某個項目,滾動將停止,但不會觸發onItemClick事件。爲了做到這一點,你需要再次點擊該項目。

我想知道如何覆蓋此行爲。我的意思是,如果我點擊一個項目,而光滑滾動正在發生,除了停止滾動,我想觸發onItemClick對項目點擊

我真的不知道是否有簡單的方法來做到這一點。我嘗試了一個GestureDetectorOnTouchListener的名單,我聽onSingleTapConfirmed爲了在那裏撥打performClick,但我不知道如何從MotionEvent獲得正在按的物品的位置。

回答

0

我終於找到了解決方案,通過使用GestureDetector

final GestureDetector gestureDetector = new GestureDetector(MyActivity.this, 
    new GestureDetector.SimpleOnGestureListener(){ 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
     int position = listView.pointToPosition((int)e.getX(), (int)e.getY()); 
     if(position != ListView.INVALID_POSITION){ 
      listView.playSoundEffect(SoundEffectConstants.CLICK); 
      //onItemClick code goes here 
      //or call listView.performItemClick 
      return true; 
     } 
     return false; 
    }; 
}); 

listView.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     return gestureDetector.onTouchEvent(event); 
    } 
}); 

listView.setSelector(android.R.color.transparent); 

在我的情況,我說我在onItemClick在做內部onSingleTapConfirmed,並從列表中移除的OnItemClickListener。這就是爲什麼我還添加playSoundEffect函數來模擬點擊。

在最後一行我禁用ListView突出顯示點擊,因爲只有當沒有平滑滾動時才顯示線條。通過禁用它,每次點擊都會得到相同的行爲。