[英]How to change background color dynamically created table row
我已經動態創建了表格行,並添加了具有表格布局的視圖,現在當用戶選擇另一行時,我想更改所選行的背景顏色和特定的textview,應禁用上一行的顏色。 提前致謝。 下面是我的代碼。
for(int s=0;s<lstFilteredTradeOrder.size();s++)
{
final TableRow tableRow = new TableRow(OrderbookFragment.this.getActivity());
tableRow.setId(s);
tableRow.setClickable(true);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT) );
tableRow.setWeightSum(1);
final View rowView;
rowView = OrderbookFragment.this.getActivity().getLayoutInflater().inflate(R.layout.custom_order_book, null);
TextView textName = (TextView)rowView.findViewById(R.id.txt_name);
TextView textLastUpdated = (TextView)rowView.findViewById(R.id.txt_lastupdated);
TextView textAction = (TextView)rowView.findViewById(R.id.txt_action);
TextView textOrdPrice = (TextView)rowView.findViewById(R.id.txt_ordprice);
TextView textOrdQty = (TextView)rowView.findViewById(R.id.txt_ordqty);
TextView textStatus = (TextView)rowView.findViewById(R.id.txt_status);
final ImageView imageView =(ImageView)rowView.findViewById(R.id.image_arrow);
textName.setText(lstFilteredTradeOrder.get(s).getstockCodeName());
textLastUpdated.setText(FormatUtil.formatDateString(String.valueOf(lstFilteredTradeOrder.get(s).getLastUpdate()),
"yyyyMMddHHmmss", "dd/MM/yyyy HH:mm:ss"));
textAction.setText(lstFilteredTradeOrder.get(s).getAction());
textOrdPrice.setText(String.format("%.3f", lstFilteredTradeOrder.get(s).getPrice()));
textOrdQty.setText(FormatUtil.formatOpDouble(lstFilteredTradeOrder.get(s).getQuantity()));
textStatus.setText(lstFilteredTradeOrder.get(s).getStatusText());
tableRow.addView(rowView);
tableRow.setOnClickListener(new View.OnClickListener() {
Boolean color=true;
public void onClick(View view) {
// here would like to change the background color.
TradeOrder selectOrd= lstFilteredTradeOrder.get(tableRow.getId());
constructOrderbookOnclick(selectOrd);
}
});
tblOrderbookDetails.addView(tableRow, new TableLayout.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
}
只需使用下面的代碼
data_Row_tv.setBackgroundColor(ContextCompat.getColor(context, R.color.your_color));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.