2016-09-01 4 views
0

다른 "스타일 지정된"구성 요소를 사용하여 ListView를 사용합니다. 그림의 첫 번째와 두 번째 구성 요소는 프로그래밍 방식으로 그려진 배경을 사용합니다. 세 번째와 네 번째 요소는 9 패치 배경을가집니다. 요소를 클릭하면 ListView의 선택기가 채워지고 선택한 요소가 표시됩니다.목록 요소 (LinearLayout)의 테두리와 9 패치 배경 사이의 틈새

내 문제는 테두리와 9 패치 배경 사이에 간격이 있다는 것을 알고 있습니다. 이 격차를 없애기 위해 어떤 해결책을 제시 하시겠습니까?

Screenshot with selected second list element (programmatically background)

Screenshot with selected fourth list element (nine-patch background)

답변

0

나를 위해 해결책을 발견했다. 모든 선택기 상태를 ListView (setSelector (new StateListDrawable()))에서 제거하고 OnTouchListener를 ListView에 추가합니다.

OnTouchListener는 뷰 요소에서 그려진 배경에 ColorFilter를 설정하거나 삭제하는 여러 가지 방법을 트리거합니다. ColorFilter는 틈새가 아닌 페인트 된 (9 패치 또는 프로그래밍 방식으로 배경) 영역 위에 만 배치합니다.

public class ClickAndReleaseListener implements View.OnTouchListener 
{ 
    /** 
    * Activity with integrated Adapter (from ListView) to do the triggered methods 
    */ 
    public IClickAndReleaseListener releaseListener; 

    /** 
    * Call from Activity (with the ListView) ex. listView.setOnTouchListener(new ClickAndReleaseListener(Activity.this)) 
    * @param releaseListener => Activity with implemented Interface 
    */ 
    public ClickAndReleaseListener(IClickAndReleaseListener releaseListener) 
    { 
     this.releaseListener = releaseListener; 
    } 


    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     //Aenderung der Visualisierung für das angeklickte Element. 
     switch(event.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
       //method find the triggered element and set a ColorFilter at the drawable background 
       //ex. visibleElement.getBackground().setColorFilter(ColorSpace.getHighlightColorFilter) 
       releaseListener.setColorFilter(event); 
       break; 
      case MotionEvent.ACTION_UP: 
       //method iterate to all elements and set a null ColorFilter at the drawable background 
       releaseListener.releaseColorFilter(); 
       break; 
     } 

     return false; 
    } 

}