我有一个Gridview,其中包含客户列表,我想要的是每当点击工具栏中的发送消息选项时,Gridview中每个客户名称前面都会出现一个复选框,表示只有在发送消息时才能看到复选框可见性单击,否则复选框可见性应该消失。
gv=(GridView)findViewById(R.id.gridview);
gv.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gv.setMultiChoiceModeListener(new GridView.MultiChoiceModeListener() {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
public void onDestroyActionMode(ActionMode mode) {
//UN SELECT ALL IF PRESSED BACK
tv_select_all.setText("Select all");
selectedAll=false;
for (int i = 0; i < gv.getAdapter().getCount(); i++)
{
gv.setItemChecked(i, false);
}
}
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,boolean checked)
{
View v = getViewByPosition(position,gv);
int selectCount = gv.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("1 item selected");
break;
default:
mode.setSubtitle("" + selectCount + " items selected");
break;
}
//CHANEG COLOR TO CYAN IF SELECTED BLUE IF NOT
if(checked){
((CheckBox)v.findViewById(R.id.checkBox)).setChecked(true);
}else{
((CheckBox)v.findViewById(R.id.checkBox)).setChecked(false);
}
}
});
tv_select_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!selectedAll) {
tv_select_all.setText("Unselect all");
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
gv.setItemChecked(i, true);
}
selectedAll=true;
}
else {
tv_select_all.setText("Select all");
selectedAll=false;
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
gv.setItemChecked(i, false);
}
}
}
});
public void checkItem(int i){
gv.setItemChecked(i, true);
}
public void uncheckItem(int i){
gv.setItemChecked(i, false);
}
//TO GET VIEW FROM ID
public View getViewByPosition(int pos, GridView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return gv.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return gv.getChildAt(childIndex);
}
}
答案 0 :(得分:0)
如果你有GridView,你应该有一个这样的自定义适配器:
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]){
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivity(intent);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}
请查看here。