Use viewpager and gridview to achieve horizontal sliding of the gridview.

Source: Internet
Author: User
Before class: The post is not replied, and the chest is drooping.. The implementation principle is actually very simple. It is to use viewpager to add a static gridview, And the gridview is written to death. First introduce the android-support-v4.jar package, which is used to support the viewpager component. Then in the oncreate method of the main class:

Layoutinflater minflater = getlayoutinflater ();

// Layoutinflater can convert an XML layout file to the view type

View view_01 = minflater. Inflate (R. layout. layout1, null );
View view_02 = minflater. inflate (R. layout. layout2, null); Private list <View> mviewlist = new arraylist <View> (); // stores the view mviewlist to be slide. clear (); // clear the old pager content before adding a new one. Mviewlist. Add (view_01 );

Mviewlist. Add (view_02 );

Gridview = (gridview) view_01.findviewbyid (R. id. gridview); gridview gridview2 = (gridview) view_02.findviewbyid (R. id. gridview2); (obtain the gridview plug-in through the above method, and then define the gridview through a custom method. If no one can implement the gridview, then add the code I wrote.) viewpager mviewpager = (viewpager) findviewbyid (R. id. viewpager); mviewpager. setadapter (New mypageradapter (); mviewpager. setcurrentitem (0); // set the current pager to add an internal class, which is also the adapter C of viewpager Lass mypageradapter extends pageradapter {// This is the custom adapter of viewpager.

@ Override public int getcount () {// return the number of views. Return mviewlist. size () ;}@ override public Boolean isviewfromobject (view arg0, object arg1) {return arg0 = arg1 ;}@ override public void destroyitem (viewgroup container, int position, object) {container. removeview (mviewlist. get (position) ;}@ override public object instantiateitem (viewgroup container, int position) {container. addview (mviewlist. get (position), 0); Return mviewlist. get (position );}

}

Layout layout file:
Add the code in the corresponding position to the main layout file.<Android. support. v4.view. viewpager Android: layout_marginleft = "3dip" Android: layout_margintop = "3dip" Android: layout_marginbottom = "3dip" Android: layout_marginright = "3dip" Android: id = "@ + ID/viewpager" Android: layout_width = "fill_parent" Android: layout_height = "match_parent" Android: layout_weight = "1">

</Android. Support. v4.view. viewpager> there are two layout files of the gridview. Only layout1.xml layout content is pasted here, And layout2 will not be pasted. <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical" Android: gravity = "center"> <gridview Android: Id = "@ + ID/gridview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: numcolumns = "5" Android: verticalspacing = "5dp" Android: horizontalspacing = "5dp" Android: columnwidth = "5dp" Android: stretchmode = "columnwidth" Android: gravity = "center"> </gridview>

</Linearlayout>

It cannot be pasted because this application cannot be put out at will, and the specific effect will be the paging gridview (nonsense) that can be slide horizontally. It is suitable for applications with fixed icons that suit my needs, the dynamic display needs to be modified on this basis.

 

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.