我遇到了以下非常奇怪的情况。我有一个列表视图,每个项目都有一个按钮。按钮执行的操作之一是更改按钮文本的颜色和大小。按下按钮时,会出现下图所示的情况。如果我按下第一个项目中的按钮,则对第一个项目执行该方法,对于在屏幕上不可见的第一个项目执行该方法。同样,如果我为第二个项目执行此操作,则在屏幕上看不到第二个项目时也会出现相同情况。我对此感到非常困惑。我尝试将onClickListener添加到我的适配器而不是单独使用该方法,我还尝试使用b.setTextSize()
而不是在列表视图中查找按钮。日志记录仅返回我单击的项目,并且一切看起来都很好,但事实并非如此。
这是列表适配器和持有者:
class ViewHolder {
TextView leaguetxt;
TextView datetxt;
TextView teamstxt;
TextView bettxt;
TextView betid;
TextView typetxt;
TextView commentstxt;
Button oddsbtn;
LinearLayout item;
ViewHolder(View v) {
leaguetxt = (TextView) v.findViewById(R.id.leaguetxt);
datetxt = (TextView) v.findViewById(R.id.datetxt);
teamstxt = (TextView) v.findViewById(R.id.teamstxt);
bettxt = (TextView) v.findViewById(R.id.bettxt);
betid = (TextView) v.findViewById(R.id.gid);
typetxt = (TextView) v.findViewById(R.id.difficultytxt);
commentstxt = (TextView) v.findViewById(R.id.commenttxt);
oddsbtn = (Button) v.findViewById(R.id.oddsbutton);
item = (LinearLayout) v.findViewById(R.id.item);
}
}
private class MyListAdapter extends ArrayAdapter<TipDisplayer> {
public MyListAdapter() {
super(AllGameslistActivity.this, R.layout.list_item, tomee);
}
public MyListAdapter(ArrayList<TipDisplayer> tomee) {
super(AllGameslistActivity.this, R.layout.list_item, tomee);
tomee = tomee; // declare tomee in the Adapter, don't use static
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
TipDisplayer currentwriter = tomee.get(position);
ViewHolder holder = null;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
holder = new ViewHolder(itemView);
itemView.setTag(holder);
Log.d("SETTING","new holder");
}
else {
holder = (ViewHolder) itemView.getTag();
Log.d("plzwork",ids.toString());
if (!ids.contains(Integer.parseInt(holder.betid.getText().toString()))) {
//holder.oddsbtn.setTextColor(Color.parseColor("#000000"));
//holder.oddsbtn.setTextSize(18);
Log.d("doesn't contain", "doesn't contain");
Log.d("betid",holder.betid.getText().toString());
Log.d("bettxt", holder.bettxt.getText().toString());
}
else {
Log.d("recycling contains", "contains");
//holder.oddsbtn.setTextColor(Color.parseColor("#EB102E"));
//holder.oddsbtn.setTextSize(20);
Log.d("betid", holder.betid.getText().toString());
Log.d("bettxt", holder.bettxt.getText().toString());
}
Log.d("Recycling","recycling");
}
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) v;
Button b = (Button) layout.findViewById(R.id.oddsbutton);
ListView listView = (ListView) layout.getParent();
final int position = listView.getPositionForView(listView);
String buttontext = b.getText().toString();
Button lastchance = (Button) layout.findViewById(R.id.oddsbutton);
TextView betidtextbox = (TextView) layout.findViewById(R.id.gid);
String betid = betidtextbox.getText().toString();
TipDisplayer currentwriter = tomee.get(position + 1);
Log.d("plzworkkkk", newBet.toString());
if (ids.contains(Integer.parseInt(betid))) {
ids.remove(new Integer(Integer.parseInt(betid)));
Log.d("plzworkkkkkkk", newBet.toString());
currentwriter.toggleHighlighted();
//checkhighlight(layout);
Log.d("getodds", buttontext);
selection = "home";
TextView teamss = (TextView) layout.findViewById(R.id.teamstxt);
String teams = teamss.getText().toString();
Log.d("teams", teams);
TextView bet = (TextView) layout.findViewById(R.id.bettxt);
String bettxt = bet.getText().toString();
Log.d("btxttext", bettxt);
newBet.generateoddstesting(betid, buttontext, false, teams, selection, bettxt);
double newodds = newBet.calculateodds();
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f", newodds) + "/1");
EditText mEdit = (EditText) findViewById(R.id.editText2);
if (mEdit.getText().toString().length() == 0) {
stake = 0.00;
newwinnings = 0.00;
potentialWinnings.setText("0.00");
} else {
mEdit.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() != 0) {
stake = Double.parseDouble(s.toString());
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f", newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
} else {
stake = 0.00;
newwinnings = 0.00;
double newodds = newBet.calculateodds();
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f", newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
}
public void afterTextChanged(Editable s) {
}
});
//stake = Double.parseDouble(mEdit.getText().toString());
newwinnings = stake * newodds;
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
} else {
ids.add(Integer.parseInt(betid));
//checkhighlight(layout);
currentwriter.toggleHighlighted();
selection = "home";
String getodds = lastchance.getText().toString();
Log.d("getoddsss", getodds);
EditText mEdit = (EditText) findViewById(R.id.editText2);
if (mEdit.getText().toString().length() == 0) {
stake = 0.00;
newwinnings = 0.00;
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
potentialWinnings.setText("0.00");
} else {
mEdit.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (s.length() != 0) {
stake = Double.parseDouble(s.toString());
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f", newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
} else {
stake = 0.00;
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f", newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
}
public void afterTextChanged(Editable s) {
}
});
stake = Double.parseDouble(mEdit.getText().toString());
TextView teamms = (TextView) layout.findViewById(R.id.teamstxt);
String teams = teamms.getText().toString();
Log.d("teams", teams);
TextView bet = (TextView) layout.findViewById(R.id.bettxt);
String bettxt = (String) bet.getText().toString();
Log.d("bettxt", bettxt);
newBet.generateoddstesting(betid, buttontext, true, teams, selection, bettxt);
double newodds = newBet.calculateodds();
newwinnings = stake * newodds;
TextView myBetOdds = (TextView) findViewById(R.id.bettingodds);
TextView potentialWinnings = (TextView) findViewById(R.id.potentialwinnings);
myBetOdds.setText("@" + String.format("%.2f", newodds) + "/1");
potentialWinnings.setText(String.format("%.2f", (newwinnings)));
newwinningstoString = potentialWinnings.getText().toString();
}
}
}
});
String leaguetext = currentwriter.getLeague();
String datetext = currentwriter.getDatetimer();
String teamstext = currentwriter.getTeams();
String bettext = currentwriter.getBet();
String typetext = currentwriter.getType();
String idtext = currentwriter.getId();
String commentsText = currentwriter.getComments();
String oddstext = currentwriter.getOdds();
holder.leaguetxt.setText(leaguetext);
holder.datetxt.setText(datetext.substring(0,datetext.lastIndexOf(":")) + " GMT");
holder.teamstxt.setText(teamstext);
holder.bettxt.setText(bettext);
holder.betid.setText(idtext);
holder.commentstxt.setText(commentsText);
holder.oddsbtn.setText(oddstext);
holder.typetxt.setText(typetext);
if (typetext.equals("Low Risk")) {
holder.typetxt.setTextColor(Color.parseColor("#067103"));
}
else if (typetext.equals("Medium Risk")) {
holder.typetxt.setTextColor(Color.parseColor("#D9D216"));
}
else if (typetext.equals("Longshot")) {
holder.typetxt.setTextColor(Color.parseColor("#F75528"));
}
return itemView;
}
}
private void PopulateList() {
ArrayAdapter<TipDisplayer> adapter = new MyListAdapter(tomee);
final ListView list = (ListView) findViewById(R.id.mylist);
list.setAdapter(adapter);
}
&#13;
答案 0 :(得分:0)
而不是使用lastchance.setTag("highlighted");
将标记设置为突出显示。将private boolean highlighted
等成员变量添加到TipCollector
。
然后在您的活动中有一个onListItemClick()
,它会将其切换为:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
this.toggleHighlighted();
myAdapter.notifyDataSetChanged();
}
在你的TipDisplayer类中:
public void toggleHighlighted() {
highlighed = !highlighted;
}
然后在你的getView()方法中你可以添加如下内容:
if (currentwriter.isHighlighed()) {
change color code here
}
答案 1 :(得分:0)
ListView通常包含的数据多于显示的行数。如果用户滚动列表,则行和它们的相关视图将滚动出可见区域。表示行的Java对象可以重用于新显示的行。
如果Android确定某行不再可见,它允许适配器方法的getView()通过convertView参数重用关联的视图。
适配器可以将新数据分配给convertView的视图层次结构中包含的视图。这样可以避免膨胀XML文件并创建新的Java对象。
如果Android无法重复使用某行,则Android系统会将null传递给convertView参数。因此,适配器实现需要检查这一点。
您可以使用ViewHolder模式来解决您的问题,请查看此链接以获取参考和代码示例: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder
答案 2 :(得分:0)
问题是,只要第一个项目在屏幕上不可见,其布局就会被回收并用于列表视图中显示的下一个项目,这意味着如果第一个项目突出显示,则只要我们滚动到第一个不可见的项目,它也将突出显示,因为它使用突出显示的第一个项目的视图。本视频于11:45 https://www.youtube.com/watch?v=Wq2o4EbM74k解释。
为了解决这个问题,请添加以下代码:
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}