2014-11-08 54 views
0

我有一個ExpandableListView,每行有四個元素。在這一行中,我在第一個項目上有一個TouchListener。現在,當我點擊(拖動)這個項目時,我想拖動整行,而不僅僅是第一個項目,並設置一個可繪製到整行的背景。如何在單擊此行中的項目時獲取整個ListView行

switch (motionEvent.getAction()) { 
      case MotionEvent.ACTION_DOWN: 

       View wholerow = view.getRootView().findViewById(R.id.dbtabellelistviewitem); 
       wholerow.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.circle)); 

       view.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.square)); 
       ClipData data = ClipData.newPlainText("", ""); 
       View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
       view.startDrag(data, shadowBuilder, view, 0); 


      case MotionEvent.ACTION_UP: 
       view.setBackgroundDrawable(null); 


     } 
     return true; 

但是,查看整行只給了我列表的第一行。如何獲取我點擊的項目的整行?

在此先感謝。

回答

1

就你而言,我懷疑視圖中創建的每一行都有相同的id(在佈局中定義)。 FindViewById只要找到合適的視圖(使用正確的ID)就會停下來,它不會處理具有相同ID的視圖。這就是爲什麼你的方法總是返回第一行。

要檢索父行,你需要通過你的視圖的父母的getParent()

手動瀏覽類似這樣的方法應該工作:

public View findParentWithId(View myView, int idParent) { 
    if (myView.getParent() instanceof View) { 
     View parent = (View) myView.getParent(); 
     if (parent.getId() == idParent) { 
      return parent; 
     } else { 
      return findParentWithId(parent, idParent); 
     } 
    } 

    return null; 
} 
+0

非常感謝。這就像一個魅力。 – alltooconfusingthereforesleep 2014-11-08 16:31:44

相關問題