GridView. setChoiceMode (GridView. CHOICE_MODE_MULTIPLE_MODAL) is not compatible with earlier version system solutions. setchoicemode

Source: Internet
Author: User

GridView. setChoiceMode (GridView. CHOICE_MODE_MULTIPLE_MODAL) is not compatible with earlier version system solutions. setchoicemode

In project development, you need to use the GridView batch processing operation and select multiple options.

However, GridView. setChoiceMode (GridView. CHOICE_MODE_MULTIPLE_MODAL) is not compatible with earlier versions.


Find a solution and view the options of multiple options of the GridView in the demo provided by the android sdk.

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;            }        }    }}

Implements Checkable, And you can set multiple options for the GridView, but it is not compatible with earlier versions.

Later, I learned how to solve the problem of compatibility with lower versions. For details, refer to the code.

Adapter set for the GridView

List <CodeGoodsListEntiy> goods = new ArrayList <CodeGoodsListEntiy> (); public class CollectionAdapter extends BaseAdapter {@ Overridepublic int getCount () {// TODO Auto-generated method stubreturn goods. size () ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn position ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn 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;} @ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubCheckableLayout layou T = 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 = (T ExtView) 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 ("monthly sales" + 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 SimpleImageLoadingList Ener (); 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 sellnu MS; RelativeLayout imgviewlayout; private boolean mChecked = false; public CheckableLayout () {// TODO Auto-generated constructor stub} @ Overridepublic void setChecked (boolean checked) {// TODO Auto-generated method stubmChecked = 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) ;}}@ Overridepublic boolean isChecked () {// TODO Auto-generated method stubreturn mChecked ;}@ Overridepublic void toggle () {// TODO Auto-generated method stubsetChecked (! MChecked );}}

In fact, the class CheckableLayout implements Checkable is similar to the ViewHolder class in a general Adapter.

The ItemClickListener method set for the GridView

private OnItemClickListener onlvItemClick = new OnItemClickListener() {@Overridepublic 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());}}};

In fact, the key here is to get the ViewHolder class through the sent View.

CheckableLayout holder = (CheckableLayout) arg1.getTag ();

Or use the following code

View view = gv. getChildAt (arg2-gv. getFirstVisiblePosition ());
CheckableLayout holder = (CheckableLayout) view. getTag ();

If convertView is reused in the GridView or ListView BaseAdapter, the number of GridView. getChildCount is less than that of BaseAdapter. getCount. Only the number displayed on the screen is displayed. This is the feature that BaseAdapter uses ViewHolder to reuse convertView.

Therefore, if you want to obtain a single itemview, use the above method.

Let's talk about it here. Many project codes involve commercialization and can be shared if you have any questions. This method requires attention to the details.

Copy the Translation results to Google
How does the Compatibility View of IE browser solve style incompatibility of controls such as gridview? I 've been doing this for a long time, but it never works.

Browsers earlier than IE8 should not be compatible, and it is difficult to get rid of them. People who often access the Internet will not use those browsers. For compatibility, you can find the content about css compatibility.

The vro version is too low, and the WIN7 system is not compatible. You cannot set the vro.

It should not be a problem with the vro version.

Unable to log on. It is the gateway originally set,
In the browser address, no pair is written.
After you restore the factory settings,
If you try 192.168.1.1, try 192.168.0.1.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.