2015-07-12 117 views
0

我想刪除一個gridView項目的陰影,當用戶按下,然後恢復它,一旦用戶釋放。 (無法使用selector.xml,因爲項目有用戶選擇的顏色)ACTION_DOWN被檢測到但不是ACTION_UP? android gridView項目的onTouch

此代碼在第一次按下時刪除陰影,但在釋放後它仍然停留在沒有陰影的情況下。

gridItemView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if(event.getAction()==MotionEvent.ACTION_DOWN) { 
       image.removeShadow(); 
       image.invalidate(); 
      } 
      else if(event.getAction()==MotionEvent.ACTION_UP){ 
       image.setShadow(); 
       image.invalidate(); 

      } 
      return false; 
     } 
    }); 

我不能將它設置爲true,因爲然後片段dosent中的.OnItemClickListener工作。此外,我還挺通過設置陰影onItemClickListener打開固定的,但如果用戶滑動拇指關閉該項目,而不只是點擊它會保持按下

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      MainActivity.selectedItem = position; 
      if (lastView[0] != null) { 
       lastView[0].setBackgroundResource(R.drawable.nullr); 
      } 
      picker.setOldCenterColor(MainActivity.items.get(MainActivity.selectedItem).getColor()); 
      picker.setColor(MainActivity.items.get(MainActivity.selectedItem).getColor()); 

      View imageContainer = view.findViewById(R.id.imageContainer); 
      CircularImageView circleImage = (CircularImageView) view.findViewById(R.id.circleView); 

      artistText.setText(MainActivity.items.get(position).getArtist()); 
      songText.setText(MainActivity.items.get(position).getSong()); 

      int backgroundColor = Color.parseColor("#FFEECC"); 
      GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{backgroundColor, backgroundColor}); 
      drawable.setShape(GradientDrawable.OVAL); 
      drawable.setStroke(25, MainActivity.items.get(MainActivity.selectedItem).getColor()); 

      imageContainer.setBackgroundDrawable(drawable); 
      circleImage.setShadow(); 
      circleImage.invalidate(); 

      lastView[0] = imageContainer; 
      MainActivity.anItemIsSelected = true; 
     } 
    }); 

回答

1

您忘記

else if(event.getAction()==MotionEvent.ACTION_UP){ 
      image.setShadow(); 
      image.invalidate(); 
} 

什麼你可以在這裏做的就是儘量創建自定義Gridview和覆蓋的onTouchEvent,這樣的事情,(這是不準確雖然準確)

public class MyGridv extends GridView{ 
    //implement all constructors; 
    //the override onTouchEvent(MotionEvent event); 
    @override 
    protected boolean onTouchEvent(MotionEvent event){ 
    // put your ontouch code here and return true, you might need to do 
    //some changes because you can not get access to your methods or 
    //you can make this class a private class to your mainActivity 
    // now delete your ontouch for your gridview 
    } 
    } 

這裏的邏輯是onTouchEvent()最初叫你onTouchListener之前它被稱爲第一,返回true,那麼就會有事件傳遞給onTouch()onClick,有一切工作都正常

希望它有幫助

+0

THX但我以前就這樣,並試圖switch語句也一樣,它仍然dosent工作,並保持影去除 –

+0

注意,當我返回true它的工作原理,但我不能返回正確的,因爲onItemClick dosent工作,然後 –

+0

檢查我正在做的編輯,我正在做一個粗略的解決方案,因爲我現在不在android。@ TintinabulatorZea – Elltz

1

嘗試下面的加你的觀點。

android:clickable="true"