在这里,我选择listitem
,其颜色正在针对该特定项目进行更改。
当我第二次选择该项目时,此前一项应该被取消,并且需要更改新的项目颜色。
delivery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater1 = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v1 = inflater1.inflate(R.layout.activity_employees_list_for_pop_up, null);
final Button ok = (Button) v1.findViewById(R.id.do_ok);
Button cancle = (Button) v1.findViewById(R.id.do_cancle);
final TextView empId=(TextView)v1.findViewById(R.id.employeeId);
ok.setEnabled(false);
listView = (ListView) v1.findViewById(R.id.employeePopUpList);
employeePopUpAdapter = new EmployeePopUpAdapter(ct, employeeIdNameBeans);
//enable ok button if listitem is checked
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ok.setEnabled(true);
//Toast.makeText(ct, ""+employeeIdNameBeans.get(position).getEmpId(), Toast.LENGTH_SHORT).show();
view.setBackgroundColor(Color.GREEN);
selectedemployeeid=employeeIdNameBeans.get(position).getEmpId();
}
});
我需要让特定的员工改变颜色。
在适配器类中,我只是将值转换为empId和empName
public class EmployeePopUpAdapter extends BaseAdapter {
Context ct;
private List<EmployeeIdNameBean> employeeIdNameBeans;
private int lastPosition = -1;
public EmployeePopUpAdapter(Context ct, List<EmployeeIdNameBean> employeeIdNameBeans) {
this.ct = ct;
this.employeeIdNameBeans = employeeIdNameBeans;
}
@Override
public int getCount() {
return employeeIdNameBeans.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.list_view_for_employee_popup_window, null);
TextView empId = (TextView) v.findViewById(R.id.employeeId);
TextView empName = (TextView) v.findViewById(R.id.empFullName);
//For animation
Animation animation = AnimationUtils.loadAnimation(ct, (position > lastPosition) ? R.anim.top_from_bottom : R.anim.down_from_top);
v.startAnimation(animation);
lastPosition = position;
final EmployeeIdNameBean empbean = employeeIdNameBeans.get(position);
empId.setText(empbean.getEmpId());
empName.setText(empbean.getEmpName());
return v;
}
}
答案 0 :(得分:0)
获取所选项目位置并更改其背景颜色,当您单击其他项目然后刷新列表,即删除所有选定颜色,然后为所选项目设置颜色。
您还可以在模型类中添加布尔参数,然后将所选项的值更改为true,在适配器中检查true值并更改相同的背景颜色,每次清除标志并刷新列表。
答案 1 :(得分:0)
试试这样。
public class EmployeePopUpAdapter extends BaseAdapter {
Context ct;
private List<EmployeeIdNameBean> employeeIdNameBeans;
private int lastPosition = -1;
private int chooseItem = -1;
public EmployeePopUpAdapter(Context ct, List<EmployeeIdNameBean> employeeIdNameBeans) {
this.ct = ct;
this.employeeIdNameBeans = employeeIdNameBeans;
}
@Override
public int getCount() {
return employeeIdNameBeans.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public void setChooseItem(int i) {
chooseItem = i;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.list_view_for_employee_popup_window, null);
TextView empId = (TextView) v.findViewById(R.id.employeeId);
TextView empName = (TextView) v.findViewById(R.id.empFullName);
//For animation
Animation animation = AnimationUtils.loadAnimation(ct, (position > lastPosition) ? R.anim.top_from_bottom : R.anim.down_from_top);
v.startAnimation(animation);
lastPosition = position;
final EmployeeIdNameBean empbean = employeeIdNameBeans.get(position);
empId.setText(empbean.getEmpId());
empName.setText(empbean.getEmpName());
// edited here ,change color
try {
if (chooseItem == -1) {
} else {
if (chooseItem == position) {
empId.setBackgroundColor(ct.getResources().getColor(android.R.color.holo_blue_bright));
} else {
empId.setBackgroundColor(ct.getResources().getColor(android.R.color.holo_red_dark));
}
}
} catch (Exception E) {
}
return v;
}
}
在您的活动代码中。
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.setChooseItem(i);
}
});