0

我有一个列表视图和自定义适配器,它为它扩展 SingleTypeAdapter。列表视图包含文本视图。每当文本视图中的数值发生变化时,文本颜色也应该发生变化。例如。如果 textview 更改为无符号数字,则颜色应为红色,否则为黄色。如果我想要最好的性能,我该怎么做?

我只对更改红色和白色之间的颜色感兴趣,仅适用于 3 个视图文本字段(更改、百分比更改、价格)。我怎么做 ?

我的适配器:

/**
 * Adapter to display a list of stock quotes
 */
public class StockListAdapter extends SingleTypeAdapter<CurrentStock> {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMMM dd");

    /**
     * @param inflater
     * @param items
     */
    public StockListAdapter(LayoutInflater inflater, List<CurrentStock> items) {
        super(inflater, R.layout.stock_quote_list_item);

        setItems(items);
    }

    /**
     * @param inflater
     */
    public StockListAdapter(LayoutInflater inflater) {
        this(inflater, null);

    }

    /*@Override
    public long getItemId(final int position) {
        final String id = getItem(position).getObjectId();
        return !TextUtils.isEmpty(id) ? id.hashCode() : super
                .getItemId(position);
    }*/

    @Override
    protected int[] getChildViewIds() {
        return new int[] { R.id.symbol, R.id.exchange, R.id.price, R.id.companyName, R.id.change, R.id.percentChange };
    }

    @Override
    protected void update(int position, CurrentStock currentStock) {
        setText(0, currentStock.getSymbol());
        setText(1, currentStock.getExchange());
        setText(2, currentStock.getPrice().toString());
        setText(3, currentStock.getCompanyName());
        setText(4, currentStock.getChange().toString());
        setText(5, currentStock.getPercentChange());
    }

}

以及列表的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp">

    <TextView
            android:id="@+id/companyName"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/table_text_selector"/>

    <TextView
            android:id="@+id/exchange"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_text_light_selector"
            android:layout_below="@id/companyName"/>

    <TextView
            android:id="@+id/symbol"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_text_light_selector"
            android:layout_below="@id/companyName"
            android:layout_toLeftOf="@id/exchange"/>


    <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:ellipsize="end"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/table_text_selector"
            android:layout_alignParentRight="true"/>

    <TextView
            android:id="@+id/change"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_stock_change_text"
            android:layout_alignParentRight="true"
            android:layout_below="@id/price"/>

    <TextView
            android:id="@+id/percentChange"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_stock_change_text"
            android:layout_alignBaseline="@id/change"
            android:layout_alignBottom="@id/change"
            android:layout_toLeftOf="@id/change"
            android:layout_marginRight="5dp"/>

</RelativeLayout>
4

1 回答 1

0

编辑: 中的代码getView将更加容易,并且不会在将文本解析为数字时遇到麻烦。请参阅新的代码片段。

在您StockListAdapter添加getView()方法以覆盖行为时,显示新的库存项目。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View line = super.getView(position, convertView, parent);

    // Set price, change and %change to red/white
    ((TextView) line.findViewById(R.id.price)).setTextColor((currentStock.getPrice() < 0d)? 0xFFFF0000:0xFFFFFFFF);
    ((TextView) line.findViewById(R.id.change)).setTextColor((currentStock.getChange() < 0d)? 0xFFFF0000:0xFFFFFFFF);
    ((TextView) line.findViewById(R.id.percentChange)).setTextColor((currentStock.getPercentChange() < 0d)? 0xFFFF0000:0xFFFFFFFF);
}

一些备注:

  • 最好的表现是相对的。使用此解决方案,检查和设置只在必要时进行。从这个角度来看,它是最有效的解决方案。
于 2013-10-24T09:33:31.163 回答