2015-01-22 7 views
8

카드를 선택하면 Android Leanback Library의 ImageCardView에서 infoArea의 배경색을 변경하려고합니다. 현재 내가 시도한 것은 OnItemViewSelectedListener의 배경을 변경하는 것입니다. 이렇게하면 배경이 변경되지만 이전에 선택한 항목은 지워지지 않습니다.ImageCardView에서 정보 영역의 배경색을 변경하는 방법은 무엇입니까?

desired effect

어떤 아이디어 :

private final class ItemViewSelectedListener implements OnItemViewSelectedListener { 
     @Override 
     public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 
            RowPresenter.ViewHolder rowViewHolder, Row row) { 
      if (item instanceof Video) { 
       mBackgroundURI = ((Video) item).getBackgroundImageURI(); 
       startBackgroundTimer(); 
       ((ImageCardView) itemViewHolder.view) 
         .setInfoAreaBackgroundColor(getResources().getColor(R.color.dark_blue_grey)); 
      } 
     } 
    } 

나는 이런 식으로 뭔가를 달성하기 위해시겠습니까? 감사.

답변

5

나는 현재 선택된보기를 추적하고 그에 따라 배경 영역을 변경하는 간단한 솔루션을 발견했습니다.

private final class ItemViewSelectedListener implements OnItemViewSelectedListener { 

     private ImageCardView currentlySelectedView = null; 

     @Override 
     public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 
            RowPresenter.ViewHolder rowViewHolder, Row row) { 
      if (item instanceof Video) { 
       mBackgroundURI = ((Video) item).getBackgroundImageURI(); 
       startBackgroundTimer(); 

       if (currentlySelectedView != null) { 
        currentlySelectedView.setInfoAreaBackgroundColor(
          getResources().getColor(R.color.lb_basic_card_info_bg_color)); 
       } 

       currentlySelectedView = (ImageCardView) itemViewHolder.view; 
       currentlySelectedView 
         .setInfoAreaBackgroundColor(getResources().getColor(R.color.dark_blue_grey)); 
      } 
     } 
    } 
4

사용자 정의 선택한 색상을 유지하기 위해 ImageCardView를 확장하여이 작업을 수행했습니다.

public static class CustomImageCardView extends ImageCardView { 

    private int mColor; 

    public CustomImageCardView(Context context) { 
     super(context); 
    } 

    public CustomImageCardView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomImageCardView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public int getCustomSelectedSwatch() { 
     return mColor; 
    } 

    public void setCustomColor(int color) { 
     mColor = color; 
    } 
} 

내 발표자의 기본 배경색과 기본 선택 색상을 회원 변수로 유지합니다.

private final int mDefaultInfoBackgroundColor; 
private final int mDefaultSelectedInfoBackgroundColor; 

그리고 명함 이미지보기의하는 setSelected 메소드를 오버라이드 (override) :

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent) { 
    mContext = parent.getContext(); 

    final CustomImageCardView cardView = new CustomImageCardView(mContext) { 
     @Override 
     public void setSelected(boolean selected) { 
      if (getCustomColor() != 0 && selected) { 
       setInfoAreaBackgroundColor(getCustomColor()); 
      } else setInfoAreaBackgroundColor(selected ? mDefaultSelectedInfoBackgroundColor : mDefaultInfoBackgroundColor); 
      super.setSelected(selected); 
     } 
    }; 

    cardView.setFocusable(true); 
    cardView.setFocusableInTouchMode(true); 
    return new ViewHolder(cardView); 
} 

문의 사항이 있으면 알려주세요!

0

집중된 카드보기의 시각적 스타일을 동적으로 변경하려면 ImageCardView에서 OnFocusChangeListener를 설정할 수 있습니다. 전체 예제는 Google Samples leanback-showcase 프로젝트에서 찾을 수 있습니다. 여기 예를 들어,

@Override 
public Presenter.ViewHolder onCreateViewHolder(ViewGroup parent) { 
    Context context = parent.getContext(); 

    ImageCardView cardView = new ImageCardView(context); 
    cardView.setFocusable(true); 
    cardView.setFocusableInTouchMode(true); 

    cardView.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       // Set bg color for the whole card 
       cardView.setBackgroundColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR)); 
       // Set bg color for the info area only 
       cardView.setInfoAreaBackgroundColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR)); 
       // Set title text color 
       ((TextView) cardView.findViewById(R.id.title_text)).setTextColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR)); 
       // Set description text color 
       ((TextView) cardView.findViewById(R.id.content_text)).setTextColor(ContextCompat.getColor(context, R.color.FOCUSED_COLOR)); 
      } 
      else { 
       cardView.setBackgroundColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR)); 
       cardView.setInfoAreaBackgroundColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR)); 
       ((TextView) cardView.findViewById(R.id.title_text)).setTextColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR)); 
       ((TextView) cardView.findViewById(R.id.content_text)).setTextColor(ContextCompat.getColor(context, R.color.NORMAL_COLOR)); 
      } 
     } 
    }); 

    return new ViewHolder(cardView); 
} 

이 모든 카드의 아이 뷰의 적절한 레이아웃 ID 년대를 얻을 XML 소스에서보고 :

이것은 당신의 ImageCardViewPresenter 클래스에서, 간단한 예제는 다음과 같이 넣어 lb_image_card_view.xml