我有一个表格布局,表格行根据JSON提供的数据构建。我希望在按下后突出显示一行,但我需要在每个给定时间内只突出显示一行,这意味着如果我突出显示一行然后按另一行,则应突出显示最后一行,而第一行不应突出显示。一切都工作正常,但是在突出显示后我无法突出显示。这是我的代码:
TableLayout tv = (TableLayout) findViewById(R.id.tblFrnd);
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = -1; i < ans.length(); i++) {
TableRow tr = new TableRow(AddFriend.this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b3 = new TextView(AddFriend.this);
b3.setText("Display Name");
b3.setTextColor(Color.BLUE);
b3.setTextSize(20);
b3.setPadding(10, 0, 0, 0);
tr.addView(b3);
TextView b4 = new TextView(AddFriend.this);
b4.setPadding(50, 0, 0, 0);
b4.setTextSize(20);
b4.setText("User Name");
b4.setTextColor(Color.BLUE);
tr.addView(b4);
TextView b5 = new TextView(AddFriend.this);
b5.setPadding(90, 0, 0, 0);
b5.setTextSize(20);
b5.setText("ID");
b5.setVisibility(View.INVISIBLE);
b5.setTextColor(Color.BLUE);
tr.addView(b5);
tv.addView(tr);
final View vline = new View(AddFriend.this);
vline.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
JSONObject json_data = ans.getJSONObject(i);
TextView b = new TextView(AddFriend.this);
String str = json_data.getString("DisplayName");
b.setText(str);
b.setPadding(10, 0, 0, 0);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(AddFriend.this);
b1.setPadding(50, 0, 0, 0);
b1.setTextSize(15);
String str1 = json_data.getString("UserName");
b1.setText(str1);
b1.setTextColor(Color.RED);
tr.addView(b1);
final TextView b2 = new TextView(AddFriend.this);
b2.setPadding(90, 0, 0, 0);
b2.setTextSize(15);
String str2 = String.valueOf(json_data.getInt("UserID"));
b2.setText(str2);
b2.setVisibility(View.INVISIBLE);
b2.setTextColor(Color.RED);
tr.addView(b2);
tr.setClickable(true);
tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(getResources().getColor(R.color.Yellow));
}
});
tv.addView(tr);
final View vline1 = new View(AddFriend.this);
vline1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
提前谢谢!
答案 0 :(得分:1)
int selectedRow = -1
。 在课堂上添加一个功能:
private void updateHighlightedRow()
{
// loop through the rows and set the background
// for a row to highlighted color if the row index == selectedRow
// else sent normal bg color
}
在添加行时,将行可单击并在单击侦听器中将行索引设置为selectedRow
,然后调用updateHighlightedRow
这应该做的事情。
答案 1 :(得分:0)
使textview setClickable(false)。