龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android中使用ListView实现漂亮的表格效果(7)

时间:2014-10-11 03:06来源:网络整理 作者:网络 点击:
分享到:
mStockListCursor.moveToPosition(position); if (null == convertView) { String code = mStockListCursor.getString(sCodeIndex); String symbol = mStockListCursor.getString(sSymbolIndex); String board = mSt

  mStockListCursor.moveToPosition(position);
  if (null == convertView) {
   String code = mStockListCursor.getString(sCodeIndex);
   String symbol = mStockListCursor.getString(sSymbolIndex);
   String board = mStockListCursor.getString(sBoardIndex);
   float lastPrice = mStockListCursor.getFloat(sLastPriceIndex);
   float changePrice = mStockListCursor.getFloat(sChangePriceIndex);
   float changePercent = mStockListCursor
     .getFloat(sChangePricePercentIndex);

   listItem = new StockListItem(mContext, code, symbol, board,
     lastPrice, changePrice, changePercent);
  } else {
   listItem = (StockListItem) convertView;
  }

  return listItem;
 }

}

StockListItem.java

复制代码 代码如下:

package com.android.msoft.mfinance.ui;

import com.android.msoft.mfinance.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StockListItem extends LinearLayout {

 public StockListItem(Context context, String code, String symbol,
   String board, float lastPrice, float changePrice,
   float changePercent) {
  super(context);

  StockListActivity stockListActivity = (StockListActivity) context;
  float textSize = stockListActivity.getTextSize();

  LayoutInflater factory = LayoutInflater.from(context);
  factory.inflate(R.layout.stock_list_item, this);

  TextView codeTextView = (TextView) findViewById(R.id.stock_code);
  codeTextView.setTextSize(textSize);
  codeTextView.setText(code);

  TextView symbolTextView = (TextView) findViewById(R.id.stock_symbol);
  symbolTextView.setTextSize(textSize);
  symbolTextView.setText(symbol);

  TextView lastPriceTextView = (TextView) findViewById(R.id.stock_last_price);
  lastPriceTextView.setTextSize(textSize);
  lastPriceTextView.setText(Float.toString(lastPrice));

  TextView changePriceTextView = (TextView) findViewById(R.id.stock_change_price);
  changePriceTextView.setTextSize(textSize);
  changePriceTextView.setText(Float.toString(changePrice));

  TextView ChangePercentTextView = (TextView) findViewById(R.id.stock_change_percentage);
  ChangePercentTextView.setTextSize(textSize);
  ChangePercentTextView.setText(Float.toString(changePercent));

  if (changePrice > 0) {
   int textColor = stockListActivity.getUpTextColor();

   // codeTextView.setTextColor(textColor);
   // symbolTextView.setTextColor(textColor);
   lastPriceTextView.setTextColor(textColor);
   changePriceTextView.setTextColor(textColor);
   ChangePercentTextView.setTextColor(textColor);
  }
  else if (changePrice < 0)
  {
            int textcolor="stockListActivity.getDownTextColor(); codetextview.settextcolor(textcolor);
   symboltextview.settextcolor(textcolor);
   lastpricetextview.settextcolor(textcolor); changepricetextview.settextcolor(textcolor);
      changepercenttextview.settextcolor(textcolor)
     }
    }
 }

到此就大功告成了,这个例子我们是通过View来画线条分割各个单元格的,另外我们还可以通过定义不同的背景色,通过背景色来达到相似的效果,这个不难,就不写了。

精彩图集

赞助商链接