我正在尝试像某些Android资源管理器一样执行列表视图,如果您长按一个项目,则会在所有行中显示一个复选框。
我有下一个Adapter,而我的片段中的下一个代码就是这样,但由于某种原因,一行没有改变。
public class MultiListAdminTiemposAdapter extends ArrayAdapter<MultiListAdminTiempos> {
ArrayList<MultiListAdminTiempos> multiListAdminTiemposArrayList;
LayoutInflater mLayoutInflater;
private Context mContext;
public MultiListAdminTiemposAdapter(Context context, int textViewResourceId, ArrayList<MultiListAdminTiempos> multiListAdminTiemposArrayList) {
super(context, textViewResourceId, multiListAdminTiemposArrayList);
this.multiListAdminTiemposArrayList = multiListAdminTiemposArrayList;
this.mContext = context;
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public ArrayList<MultiListAdminTiempos> getSelectedList() {
return this.multiListAdminTiemposArrayList;
}
private class ViewHolder {
private TextView tv_name, tv_tarea, tv_hours;
private CheckBox chk_tiempo;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.listview_admin_tiempos, null);
holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
holder.tv_tarea = (TextView) convertView.findViewById(R.id.tv_tarea);
holder.tv_hours = (TextView) convertView.findViewById(R.id.tv_hours);
holder.chk_tiempo = (CheckBox) convertView.findViewById(R.id.chk_tiempo);
holder.chk_tiempo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
CheckBox checkBox = (CheckBox) view;
MultiListAdminTiempos item = (MultiListAdminTiempos) checkBox.getTag();
item.setSelected(checkBox.isChecked());
for(int i = 0; i < multiListAdminTiemposArrayList.size(); i++){
Log.e("CHK_T", "" + multiListAdminTiemposArrayList.get(i).getId() + "::" + multiListAdminTiemposArrayList.get(i).isSelected());
}
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MultiListAdminTiempos item = multiListAdminTiemposArrayList.get(position);
holder.tv_name.setText(item.getProject_name());
holder.tv_tarea.setText(item.getTask_name());
holder.tv_hours.setText(parseTime(item.getHours()));
holder.chk_tiempo.setChecked(item.isSelected());
holder.chk_tiempo.setTag(item);
return convertView;
}
public String parseTime(int min) {
int hours = min / 60; //since both are ints, you get an int
int minutes = min % 60;
String lb_hour = (hours == 1 ? mContext.getResources().getString(R.string.HORA_STRING) : mContext.getResources().getString(R.string.HORAS_STRING));
String lb_minute = (minutes == 1 ? mContext.getResources().getString(R.string.MINUTO_STRING) : mContext.getResources().getString(R.string.MINUTOS_STRING));
String time = "";
time += hours + " " + lb_hour;
if (minutes > 0) {
time += "\n" + minutes + " " + lb_minute;
}
return time;
}
}
我的列表视图代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_admin_tiempos, container, false);
setHasOptionsMenu(true);
list_admin_tiempos = (ListView) view.findViewById(R.id.list_admin_tiempos);
list_admin_tiempos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (selecting) {
CheckBox chk = (CheckBox) view.findViewById(R.id.chk_tiempo);
changeCheckBoxState(chk);
/*
if (areAllUnchecked(parent)) {
for (int i = 0; i < parent.getCount(); i++) {
parent.getChildAt(i).findViewById(R.id.chk_tiempo).setVisibility(View.INVISIBLE);
}
selecting = false;
}*/
}
else {
//Vamos al detalle
viewDetail(position);
}
}
});
list_admin_tiempos.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (!selecting) {
MenuItem item = menu.findItem(R.id.action_delete);
item.setVisible(true);
for (int i = 0; i < parent.getChildCount(); i++) {
parent.getChildAt(i).findViewById(R.id.chk_tiempo).setVisibility(View.VISIBLE);
}
//changeCheckBoxState( ((CheckBox) view.findViewById(R.id.chk_tiempo)));
selecting = true;
}else if (selecting) {
MenuItem item = menu.findItem(R.id.action_delete);
item.setVisible(false);
for (int i = 0; i < parent.getChildCount(); i++) {
parent.getChildAt(i).findViewById(R.id.chk_tiempo).setVisibility(View.GONE);
}
//changeCheckBoxState( ((CheckBox) view.findViewById(R.id.chk_tiempo)));
selecting = false;
}
return true;
}
});
return view;
}
屏幕截图,当我向下滚动列表时: