Fundapter - 如何操纵视图/布局

时间:2014-05-19 11:41:14

标签: java android

有没有办法操纵BindDictionary()中的视图?

或者我如何为图像视图添加点击事件?我可以在BindDictionar()中做到这一点吗?

我试图完成此任务的库是Fun Dapter

1 个答案:

答案 0 :(得分:0)

你可以通过添加.onClick(...之后例如.addStringField(res,met).onClick(

)在BindDictionary()中执行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) {
            }
        });

修改 第二种可能性是自己编辑筹款库。

首先在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
            }


        });