我的每一行都有两个ImageView。 如果我选择它们,它们将按预期工作,但是,它也会影响其他行。
这是代码。
viewHolder.homeTeamLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickYourProsModelsHashMap.put(scheduleList.get(position).getAway() + "vs" + scheduleList.get(position).getHome(), scheduleList.get(position).getHome());
// if selected, make opacity full but change the awayTeamLogo to 50
}
});
viewHolder.awayTeamLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickYourProsModelsHashMap.put(scheduleList.get(position).getAway() + "vs" + scheduleList.get(position).getHome(), scheduleList.get(position).getAway());
// if selected, make opacity full but change the homeTeamLogo to 50
}
});
在不影响列表中其他行的情况下执行此操作的正确方法是什么?
我确实在那里有if语句,如果我选择了hometeam,我会改变不透明度,反之亦然。我还添加了notifyDataSetChanged();它并没有影响它
这是我的整个getView
@Override
public View getView(final int position, View view, ViewGroup viewGroup){
final ViewHolder viewHolder;
//final ParseObject leagues = leaguesList.get(position);
if (view == null) {
view = LayoutInflater.from(activityContext).inflate(R.layout.schedule_list_view_pyp, null);
viewHolder = new ViewHolder();
viewHolder.awayTeam = (TextView) view.findViewById(R.id.awayTeam);
viewHolder.awayTeamName = (TextView) view.findViewById(R.id.awayTeamName);
viewHolder.homeTeam = (TextView) view.findViewById(R.id.homeTeam);
viewHolder.homeTeamName = (TextView) view.findViewById(R.id.homeTeamName);
// viewHolder.dateInformation = (TextView) view.findViewById(R.id.dateInformation);
viewHolder.homeTeamLogo = (ImageView) view.findViewById(R.id.homeTeamLogo);
viewHolder.awayTeamLogo = (ImageView) view.findViewById(R.id.awayTeamLogo);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
String month = scheduleList.get(position).getDate().substring(4, 6);
String day = scheduleList.get(position).getDate().substring(6, 8);
String year = scheduleList.get(position).getDate().substring(0, 4);
home = scheduleList.get(position).getHome();
away = scheduleList.get(position).getAway();
viewHolder.homeTeam.setText(home);
viewHolder.homeTeam.setTextColor(getContext().getResources().getColor(setColorOfTeam(home)));
viewHolder.homeTeamName.setText(" " + scheduleList.get(position).getHomeTeamName().substring(0, 1).toUpperCase() + scheduleList.get(position).getHomeTeamName().substring(1).toLowerCase());
viewHolder.awayTeam.setText(away);
viewHolder.awayTeam.setTextColor(getContext().getResources().getColor(setColorOfTeam(away)));
viewHolder.awayTeamName.setText(" " + scheduleList.get(position).getAwayTeamName().substring(0, 1).toUpperCase() + scheduleList.get(position).getAwayTeamName().substring(1).toLowerCase());
// viewHolder.dateInformation.setText(scheduleList.get(position).getDay() + " " + month + "/" + day + "/" + year + " " + "at " + scheduleList.get(position).getTimeAMPM() + "M");
viewHolder.homeTeamLogo.setImageDrawable(getContext().getResources().getDrawable(setTeamLogo(home)));
viewHolder.awayTeamLogo.setImageDrawable(getContext().getResources().getDrawable(setTeamLogo(away)));
viewHolder.homeTeamLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickYourProsModelsHashMap.put(scheduleList.get(position).getAway() + "vs" + scheduleList.get(position).getHome(), scheduleList.get(position).getHome());
// if selected, make opacity full but change the awayTeamLogo to 50
}
});
viewHolder.awayTeamLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickYourProsModelsHashMap.put(scheduleList.get(position).getAway() + "vs" + scheduleList.get(position).getHome(), scheduleList.get(position).getAway());
// if selected, make opacity full but change the homeTeamLogo to 50
}
});
return view;
}
这是viewHolder
private static class ViewHolder {
TextView awayTeam;
TextView homeTeam;
TextView awayTeamName;
TextView homeTeamName;
ImageView homeTeamLogo;
ImageView awayTeamLogo;
}
答案 0 :(得分:0)
试试这个。
要在按下它时保持listview项目的颜色,请在listview项目布局中包含以下行:
android:background="@drawable/bg_key"
然后在drawable文件夹中定义bg_key.xml,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
最后,将它包含在ListView onClickListener中:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
... // Anything
}
});
这样,任何时候都只会选择一个项目进行颜色选择。您可以使用以下内容在res / values / colors.xml中定义颜色值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#4d90fe</color>
<color name="default_color">#ffffff</color>
</resources>