2015-11-26 2 views
1

Я следую руководству по адресу this site, чтобы реализовать пользовательское состояние для списка состояний. Он работает с логическими атрибутами, но не со строковыми атрибутами.Android - Пользовательские состояния со строковым значением

Это мой исходный код:

  • Государственно-лист:

  • Атрибуты:

<declare-styleable name="CategoryIconView"> 
      <attr name="category_id" format="string"/> 
     </declare-styleable> 
  • на заказ Вид:

    public class CategoryIconView extends ImageView { 
    
    // this is used when we want to merge our state with the ones from the system 
    private static final int[] CATEGORY_ID_STATE_SET = {R.attr.category_id}; 
    
    String categoryID; 
    public CategoryIconView(Context context) { 
        super(context); 
    } 
    
    public CategoryIconView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
    } 
    
    public CategoryIconView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
    } 
    
    @Override 
    public int[] onCreateDrawableState(int extraSpace) { 
        LogUtils.d(getClass().getSimpleName(), "category ID " + categoryID); 
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 
        if (categoryID != null) { 
         mergeDrawableStates(drawableState, CATEGORY_ID_STATE_SET); 
        } 
        return drawableState; 
    } 
    
    // this method will be used when you will need to set your image button state in the code 
    public void setCategoryID(String categoryId){ 
        this.categoryID = "Three"; 
        refreshDrawableState(); 
    } 
    } 
    
+0

Вы можете просто правильно форматировать код ? –

+0

Извините. Я не знаю, почему я не могу отформатировать код с помощью SO-редактора. Я использую firefox. –

+0

Здравствуйте, я дал какой-то ответ, можете ли вы это проверить? После проверки сообщите мне. –

ответ

0

На самом деле вам не хватает некоторые заявления, ниже найти решение этой

public class CategoryIconView extends ImageView { 

     // this is used when we want to merge our state with the ones from the system 
     private static final int[] CATEGORY_ID_STATE_SET = {R.attr.category_id}; 

     String categoryID; 

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

     public CategoryIconView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
// R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable 
     // from attributes.xml 
     TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0); 

     // R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your 
     // custom state list in attributes.xml 
     categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id); 
     } 

     public CategoryIconView(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
      // R.styleable.CustomStates is the id of the custom state and CustomStates it's the name of your styleable 
      // from attributes.xml 
      TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CategoryIconView, 0, 0); 

      // R.styleable.CustomStates_has_new_data is an ID that is created automatically when you create your 
      // custom state list in attributes.xml 
      categoryID = typedArray.getString(R.styleable.CategoryIconView_category_id); 
     } 

     @Override 
     public int[] onCreateDrawableState(int extraSpace) { 
      Log.d(getClass().getSimpleName(), "category ID " + categoryID); 
      final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 
      if (categoryID != null) { 
       mergeDrawableStates(drawableState, CATEGORY_ID_STATE_SET); 
      } 
      return drawableState; 
     } 

     // this method will be used when you will need to set your image button state in the code 
     public void setCategoryID(String categoryId) { 
      this.categoryID = "Three"; 
      refreshDrawableState(); 
     } 
    }