标签:etc tip data- creat main bre launcher find pack
项目开发中须要使用GridView批处理操作,多项选择。
可是GridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL)不兼容低版本号。
找解决方式。查看android sdk提供的demo里介绍的GridView多项选择的方法
public class Grid3 extends Activity { GridView mGrid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadApps(); setContentView(R.layout.grid_1); mGrid = (GridView) findViewById(R.id.myGrid); mGrid.setAdapter(new AppsAdapter()); mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener()); } private List<ResolveInfo> mApps; private void loadApps() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mApps = getPackageManager().queryIntentActivities(mainIntent, 0); } public class AppsAdapter extends BaseAdapter { public AppsAdapter() { } public View getView(int position, View convertView, ViewGroup parent) { CheckableLayout l; ImageView i; if (convertView == null) { i = new ImageView(Grid3.this); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ViewGroup.LayoutParams(50, 50)); l = new CheckableLayout(Grid3.this); l.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT)); l.addView(i); } else { l = (CheckableLayout) convertView; i = (ImageView) l.getChildAt(0); } ResolveInfo info = mApps.get(position); i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); return l; } public final int getCount() { return mApps.size(); } public final Object getItem(int position) { return mApps.get(position); } public final long getItemId(int position) { return position; } } public class CheckableLayout extends FrameLayout implements Checkable { private boolean mChecked; public CheckableLayout(Context context) { super(context); } public void setChecked(boolean checked) { mChecked = checked; setBackgroundDrawable(checked ? getResources().getDrawable(R.drawable.blue) : null); } public boolean isChecked() { return mChecked; } public void toggle() { setChecked(!mChecked); } } public class MultiChoiceModeListener implements 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) { } public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { int selectCount = mGrid.getCheckedItemCount(); switch (selectCount) { case 1: mode.setSubtitle("One item selected"); break; default: mode.setSubtitle("" + selectCount + " items selected"); break; } } } }
后来自己琢磨发现了解决兼容低版本号的方法。见代码
给GridView设置的Adapter
List<CodeGoodsListEntiy> goods = new ArrayList<CodeGoodsListEntiy>(); public class CollectionAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return goods.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public void addDataList(List<CodeGoodsListEntiy> goodsList){ goods.addAll(goodsList); } public void removeAllList(){ goods.removeAll(goods); } public void removeData(CodeGoodsListEntiy entity){ goods.remove(entity); } public List<CodeGoodsListEntiy> getList(){ return goods; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub CheckableLayout layout = null; CodeGoodsListEntiy good = goods.get(position); if(convertView==null){ layout = new CheckableLayout(); convertView = inflater.inflate(R.layout.mycollection_griditem, null); layout.imgviewlayout = (RelativeLayout) convertView.findViewById(R.id.imgviewlayout); layout.iv = (ImageView) convertView.findViewById(R.id.iv); layout.price = (TextView) convertView .findViewById(R.id.price); layout.title = (TextView) convertView.findViewById(R.id.title); layout.sellnums = (TextView) convertView.findViewById(R.id.sells); layout.item_selectbtn = (ImageView) convertView.findViewById(R.id.item_selectbtn); layout.item_topview = (ImageView) convertView.findViewById(R.id.item_topview); convertView.setTag(layout); }else{ layout = (CheckableLayout) convertView.getTag(); } layout.price.setText("¥"+good.getSalePrice()); layout.title.setText(good.getTitle()); layout.sellnums.setText("月销量"+good.getSellNums()); layout.imgviewlayout.getLayoutParams().height = (SettingUtil.getDisplaywidthPixels() - SettingUtil .dip2px(8)) / 2; layout.iv.getLayoutParams().height = (SettingUtil.getDisplaywidthPixels() - SettingUtil .dip2px(10)) / 2; App.imageLoader.displayImage(good.getMainImg(), layout.iv, options, new SimpleImageLoadingListener()); if(editModel){ if(selecteditem.contains(position)){ layout.setChecked(true); }else{ layout.setChecked(false); } }else{ layout.item_selectbtn.setVisibility(View.GONE); layout.item_topview.setVisibility(View.GONE); } return convertView; } } class CheckableLayout implements Checkable{ View convertView; ImageView item_selectbtn; ImageView item_topview; ImageView iv; TextView price; TextView title; TextView sellnums; RelativeLayout imgviewlayout; private boolean mChecked = false; public CheckableLayout() { // TODO Auto-generated constructor stub } @Override public void setChecked(boolean checked) { // TODO Auto-generated method stub mChecked = checked; if(item_topview!=null){ if(mChecked){ item_topview.setVisibility(View.GONE); }else{ item_topview.setVisibility(View.VISIBLE); } } if(item_selectbtn!=null){ if(mChecked){ item_selectbtn.setVisibility(View.VISIBLE); }else{ item_selectbtn.setVisibility(View.GONE); } } } @Override public boolean isChecked() { // TODO Auto-generated method stub return mChecked; } @Override public void toggle() { // TODO Auto-generated method stub setChecked(!mChecked); } }
然后给GridView设置的ItemClickListener的方法
private OnItemClickListener onlvItemClick = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if(editModel){ CheckableLayout holder = (CheckableLayout) arg1.getTag(); if(selecteditem.contains(arg2)){ selecteditem.remove((Integer)arg2); holder.setChecked(false); }else{ selecteditem.add(arg2); holder.setChecked(true); } }else{ ProDetailActivity.launch(MyCollectionActivity.this, goods .get(arg2).getItemId()); } } };
CheckableLayout holder = (CheckableLayout) arg1.getTag();
或者用以下的代码
View view = gv.getChildAt(arg2 - gv.getFirstVisiblePosition());
CheckableLayout holder = (CheckableLayout) view.getTag();
这里一般的GridView或者ListView的BaseAdapter里复用了convertView的话,GridView.getChildCount的数量少于BaseAdapter.getCount数量。
仅仅会显示屏幕显示出的数量,这是BaseAdapter用ViewHolder复用convertView的特点。
所以假设要获取到单个itemview的话用上面的方法。
说到这里吧。非常多项目代码涉及到商业性,有问题能够交流。这样的方法须要注意细节就是了。
GridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL)不兼容低版本号系统解决方式
标签:etc tip data- creat main bre launcher find pack
原文地址:http://www.cnblogs.com/cynchanpin/p/7230893.html