2014-05-19 42 views

回答

0

您可以BindDictionary()加入.onClick(做的onClick ...爲後例如.addStringField(RES,滿足).onClick(

這是一個完整的例子:。

BindDictionary<SomeObject> dict = new BindDictionary<SomeObject>(); 

dict.addStringField(R.id.time, new StringExtractor<SomeObject>() { 

      @Override 
      public String getStringValue(SomeObjectitem, int position) {     
       return item.getSomething(); 

      } 
     }).onClick(new ItemClickListener<SomeObject>() { 

      @Override 
      public void onClick(SomeObjectitem, int position, View view) { 
      } 
     }); 

編輯 第二種可能性是編輯fundapter libary自己

首先創建com.ami.fundapter.interfaces一個新的接口:

package com.ami.fundapter.interfaces; 

import android.widget.TextView; 

public interface TextViewExtractor { 
    public void getTextView(String string, TextView view); 
} 

然後在BindDictionary創建這個方法:

public StringField<T> addStringField(int viewResId, StringExtractor<T> extractor,TextViewExtractor viewExtractor) { 

     StringField<T> field = new StringField<T>(viewResId, extractor,viewExtractor); 

     mStringFields.add(field); 

     return field; 
    } 

後,在FunDapterUtils,直到它看起來像這樣修改這個方法:

private static <T> void handleStringFields(T item, GenericViewHolder holder, int position, 
              BindDictionary<T> dictionary) { 
    // handle string fields 
    for (int i = 0; i < dictionary.getStringFields().size(); i++) { 
     StringField<T> field = dictionary.getStringFields().get(i); 
     String stringValue = field.extractor.getStringValue(item, position); 
     TextView view = holder.stringFields[i]; 
    // fill data 
    if(!TextUtils.isEmpty(stringValue) && view != null &&field.viewExtractor!=null){ 
     field.viewExtractor.getTextView(stringValue, view); 
    } 
    if (!TextUtils.isEmpty(stringValue) && view != null) { 
     view.setText(stringValue); 
     view.setVisibility(View.VISIBLE); 
    } else { 
     view.setVisibility(field.visibilityIfNull); 
    } 

    // set textcolor if needed 
    if (field.conditionalTextColorEntry != null) { 
     boolean condition = 
       field.conditionalTextColorEntry.getKey().getBooleanValue(item, position); 

     if (condition) { 
      view.setTextColor(field.conditionalTextColorEntry.getValue()[0]); 
     } else { 
      view.setTextColor(field.conditionalTextColorEntry.getValue()[1]); 
     } 
    } 

    setClickListener(item, position, field, view); 
} 

}

最後在StringField類創建這種方法:

public StringField(int viewResId, StringExtractor<T> extractor,TextViewExtractor viewExtractor) { 
     super(viewResId, extractor); 
     this.viewExtractor = viewExtractor; 
    } 

現在你可以做這樣的事情:

BindDictionary<Product> dict = new BindDictionary<Product>(); 
dict.addStringField(R.id.oldPrice, new StringExtractor<Product>() { 

      @Override 
      public String getStringValue(Product item, int position) { 
       return item.getSomething(); 
      } 
     },new TextViewExtractor() { 

      @Override 
      public void getTextView(String string, TextView view) { 
       view.setPaintFlags(view.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); //for example strikethrough text 
      } 


     });