如何动态更改单个项目的文本颜色?
How to change text color individual item dynamically?
如何动态改变列表视图中单个项目的文本颜色?
我想在Android.
中实现以下功能
- 定时调用API获取每条价格数据
- 在列表视图中打印
- 计算每个价格保证金
- 如果某项的边距为负,则将颜色更改为红色。加号是绿色的。
这个功能在加密货币应用中很常见。
像那样
https://www.youtube.com/watch?v=UuCkg_P6_Ws
我实现了 1、2 和 3。
如何实施 4?
如果您对此有任何了解,请告诉我。
public class CoinAdapter extends ArrayAdapter<CoinItem> {
Context context;
LayoutInflater layoutInflater;
ArrayList<CoinItem> coinItem;
float x;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CoinAdapter extends ArrayAdapter<CoinItem> {
Context context;
LayoutInflater layoutInflater;
ArrayList<CoinItem> coinItem;
float x;
public CoinAdapter(Context context, int resource, ArrayList<CoinItem> coinItem) {
super(context, resource, coinItem);
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.coinItem = coinItem;
}
public float diff(String btc_last, String btc_prelast) {
x = Float.parseFloat(btc_last) - Float.parseFloat(btc_prelast);
return x;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = layoutInflater.inflate(R.layout.item_ticker, null);
}
ImageView icon_coin = (ImageView) convertView.findViewById(R.id.icon_coin);
icon_coin.setImageBitmap(coinItem.get(position).getIcon_coin());
TextView text_last = (TextView) convertView.findViewById(R.id.text_last);
text_last.setText(coinItem.get(position).getText_last() + "円");
if (x > 0) text_last.setTextColor(Color.GREEN);
else if (x < 0) text_last.setTextColor(Color.RED);
else text_last.setTextColor(Color.BLACK);
TextView text_coin = (TextView) convertView.findViewById(R.id.text_coin);
text_coin.setText(coinItem.get(position).getText_coin());
TextView text_bid = (TextView) convertView.findViewById(R.id.text_bid);
text_bid.setText("売値");
TextView text_bid_price = (TextView) convertView.findViewById(R.id.text_bid_price);
text_bid_price.setText(coinItem.get(position).getText_bid_price() + "円");
TextView text_ask = (TextView) convertView.findViewById(R.id.text_ask);
text_ask.setText("買値");
TextView text_ask_price = (TextView) convertView.findViewById(R.id.text_ask_price);
text_ask_price.setText(coinItem.get(position).getText_ask_price() + "円");
return convertView;
}
}
调用API代码就是这样
final Handler handler = new Handler();
final int delay = 1000;
btc_prelast="0";
handler.postDelayed(new Runnable() {
@Override
public void run() {
coin_btc.clone().enqueue(new Callback<Ticker>() {
@Override
public void onResponse(Call<Ticker> call, Response<Ticker> response) {
btc_last = Float.toString(response.body().getLast());
btc_bid = Float.toString(response.body().getBid());
btc_ask = Float.toString(response.body().getAsk());
coinItem.set(0, new CoinItem(BitmapFactory.decodeResource(getResources(), icon_coin[0]), btc_last, name_coin[0], btc_bid, btc_ask));
coinAdapter.diff(btc_last,btc_prelast);
btc_prelast = btc_last;
coinAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Ticker> call, Throwable t) {
}
});
我假设在您的列表视图中您的布局中已经有一个 TextView
。您所要做的就是调用 TextView#setTextColor。一个例子是:
TextView t = (TextView)v.findViewById(R.id.textviewname);
t.setTextColor(Color.RED);
简单地输入 else 条件即可。
假设 TextView 的默认颜色是黑色。
if(x < 0 ){
textview.setTextColor(Color.RED);
}else if(x > 0){
textview.setTextColor(Color.GREEN);
}else{
//your textview default colour
textview.setTextColor(Color.BLACK);
}
在您的 Getview 方法中,
检查您的保证金。
if(margin > 0){textview.setTextColor(Color.GREEN);}
else if(margin < 0){textview.setTextColor(Color.RED);}
else {textview.setTextColor(Color.BLACK);}
检查这个条件。
如何动态改变列表视图中单个项目的文本颜色? 我想在Android.
中实现以下功能- 定时调用API获取每条价格数据
- 在列表视图中打印
- 计算每个价格保证金
- 如果某项的边距为负,则将颜色更改为红色。加号是绿色的。
这个功能在加密货币应用中很常见。 像那样 https://www.youtube.com/watch?v=UuCkg_P6_Ws
我实现了 1、2 和 3。 如何实施 4? 如果您对此有任何了解,请告诉我。
public class CoinAdapter extends ArrayAdapter<CoinItem> {
Context context;
LayoutInflater layoutInflater;
ArrayList<CoinItem> coinItem;
float x;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CoinAdapter extends ArrayAdapter<CoinItem> {
Context context;
LayoutInflater layoutInflater;
ArrayList<CoinItem> coinItem;
float x;
public CoinAdapter(Context context, int resource, ArrayList<CoinItem> coinItem) {
super(context, resource, coinItem);
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.coinItem = coinItem;
}
public float diff(String btc_last, String btc_prelast) {
x = Float.parseFloat(btc_last) - Float.parseFloat(btc_prelast);
return x;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = layoutInflater.inflate(R.layout.item_ticker, null);
}
ImageView icon_coin = (ImageView) convertView.findViewById(R.id.icon_coin);
icon_coin.setImageBitmap(coinItem.get(position).getIcon_coin());
TextView text_last = (TextView) convertView.findViewById(R.id.text_last);
text_last.setText(coinItem.get(position).getText_last() + "円");
if (x > 0) text_last.setTextColor(Color.GREEN);
else if (x < 0) text_last.setTextColor(Color.RED);
else text_last.setTextColor(Color.BLACK);
TextView text_coin = (TextView) convertView.findViewById(R.id.text_coin);
text_coin.setText(coinItem.get(position).getText_coin());
TextView text_bid = (TextView) convertView.findViewById(R.id.text_bid);
text_bid.setText("売値");
TextView text_bid_price = (TextView) convertView.findViewById(R.id.text_bid_price);
text_bid_price.setText(coinItem.get(position).getText_bid_price() + "円");
TextView text_ask = (TextView) convertView.findViewById(R.id.text_ask);
text_ask.setText("買値");
TextView text_ask_price = (TextView) convertView.findViewById(R.id.text_ask_price);
text_ask_price.setText(coinItem.get(position).getText_ask_price() + "円");
return convertView;
}
}
调用API代码就是这样
final Handler handler = new Handler();
final int delay = 1000;
btc_prelast="0";
handler.postDelayed(new Runnable() {
@Override
public void run() {
coin_btc.clone().enqueue(new Callback<Ticker>() {
@Override
public void onResponse(Call<Ticker> call, Response<Ticker> response) {
btc_last = Float.toString(response.body().getLast());
btc_bid = Float.toString(response.body().getBid());
btc_ask = Float.toString(response.body().getAsk());
coinItem.set(0, new CoinItem(BitmapFactory.decodeResource(getResources(), icon_coin[0]), btc_last, name_coin[0], btc_bid, btc_ask));
coinAdapter.diff(btc_last,btc_prelast);
btc_prelast = btc_last;
coinAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Ticker> call, Throwable t) {
}
});
我假设在您的列表视图中您的布局中已经有一个 TextView
。您所要做的就是调用 TextView#setTextColor。一个例子是:
TextView t = (TextView)v.findViewById(R.id.textviewname);
t.setTextColor(Color.RED);
简单地输入 else 条件即可。
假设 TextView 的默认颜色是黑色。
if(x < 0 ){
textview.setTextColor(Color.RED);
}else if(x > 0){
textview.setTextColor(Color.GREEN);
}else{
//your textview default colour
textview.setTextColor(Color.BLACK);
}
在您的 Getview 方法中, 检查您的保证金。
if(margin > 0){textview.setTextColor(Color.GREEN);}
else if(margin < 0){textview.setTextColor(Color.RED);}
else {textview.setTextColor(Color.BLACK);}
检查这个条件。