"Android Combat" Scrollview+gridview+viewpager implementation navigation page

Source: Internet
Author: User

According to the conventional thinking, the implementation of the navigation page has a special tabhost or Viewpager, but the custom space is not small, moreover, I want to familiarize yourself with the use of multiple layouts and animation effects, so this scrollview+gridview+viewpager effect.

One of the more troublesome is the GridView to achieve horizontal loading, and the following scroll bar with the sliding also has to scroll and dynamic location changes.

public class Mainact extends fragmentactivity {Viewpager viewpager; Horizontalscrollview ScrollView; String[] titles = {"Home", "News", "Friends", "shopping", "entertainment", "video", "reading", "Sports", "medical", "Food"}; Layoutparams textlayoutparams;private mygridview gridview;int[] tabmoving = new int[2]; View Downview;long dutime = 500;boolean right = True;int old = 0; @Overrideprotected void onCreate (Bundle savedinstancestat e) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.scrollpager); Textlayoutparams = new Layoutparams (layoutparams.match_parent,layoutparams.match_parent); Textlayoutparams.setmargins (Ten, Ten, ten); Viewpager = (Viewpager) Findviewbyid (r.id.pager); ScrollView = ( Horizontalscrollview) Findviewbyid (r.id.myscroll); GridView = (MyGridView) Findviewbyid (r.id.gridview);d Ownview = ( View) Findviewbyid (R.id.down), final myadapter adapter = new Myadapter (); Pageradapter myadapter = new Pageradapter (Getsupportfragmentmanager ()); Viewpager.setadapter (myAdapter); int size = Titles. length;final displaymetrics dm = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); float  Density = dm.density;int allwidth = (int) (* (size) * density)-(titles.length) * 10;final int itemwidth = (int) (100 * density);d ownview.setlayoutparams (New Layoutparams (Itemwidth, 8)); Linearlayout.layoutparams params = new Linearlayout.layoutparams (allwidth, LinearLayout.LayoutParams.FILL_PARENT); Gridview.setlayoutparams (params); Gridview.setcolumnwidth (itemwidth); gridview.sethorizontalspacing (10); Gridview.setstretchmode (Gridview.no_stretch); gridview.setnumcolumns (size); Gridview.setadapter (adapter); Viewpager.setcurrentitem (0); Gridview.getparent (). Requestdisallowintercepttouchevent (True); Viewpager.setonpagechangelistener (New Onpagechangelistener () {@Overridepublic void onpageselected (int arg0) {//TODO Auto-generated method Stubview view = Gridview.getchildat (arg0), if (view = = null) Return;view.getlocationonscreen ( tabmoving), if (old <= tabmoving[0]) {right = true;}else {right = false;} Old = Tabmoving[0];int Difvalue = dm.widthpixels-tabmoving[0]-itemwidth;if (Difvalue < 0) {Scrollview.scrollby (Math . ABS (Difvalue), 0); Translateanimation tt = new Translateanimation (dm.widthpixels-2 * itemWidth-10, dm.widthpixels-itemwidth, 0, 0); tt.se Tduration (Dutime); Tt.setfillafter (True);d ownview.startanimation (TT);} else if (Tabmoving[0] < 0) {Scrollview.scrollby (tabmoving[0], 0); Translateanimation tt = new Translateanimation (itemwidth + ten, 0, 0, 0); Tt.setduration (dutime); Tt.setfillafter (true); Downview.startanimation (TT); gridview.setselection (arg0); adapter.notifydatasetchanged ();} else {if (right) {translateanimation TT = new Translateanimation (tabmoving[0]-itemWidth-10, tabmoving[0], 0,0); tt.setd Uration (Dutime); Tt.setfillafter (True);d ownview.startanimation (TT);} else {translateanimation tt = new Translateanimation (tabmoving[0], tabmoving[0]-itemWidth-10, 0,0); Tt.setduration (dut IME); Tt.setfillafter (True);d ownview.startanimation (TT);}}} @Overridepublic void onpagescrolled (int arg0, float arg1, int arg2) {//TODO auto-generated method stub} @Overridepublic void O npagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}}); Class Pageradapter extends Fragmentstatepageradapter {fragment[] fs = {new Myfragment (Titles[0]), New Myfragment (titles[ 1]), new Myfragment (Titles[2]), new Myfragment (Titles[3]), new Myfragment (Titles[4]), new Myfragment (Titles[5]), new Myfragment (Titles[6]), new Myfragment (Titles[7]), new Myfragment (Titles[8]), new Myfragment (Titles[9])};p ublic Pageradapter (Fragmentmanager FM) {super (FM);//TODO auto-generated constructor stub} @Overridepublic Fragment GetItem ( int arg0) {//TODO auto-generated method Stubreturn fs[arg0];} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn fs.length;}} Class Myadapter extends Baseadapter {@Overridepublic int getcount () {//TODO auto-generated method Stubreturn Titles.lengt H;} @Overridepublic Object getItem (int position) {//TODO auto-generatedMethod Stubreturn titles[position];} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;}  @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubtextview TextView = new TextView (Getapplicationcontext ()); Textview.settext (titles[position]);//Textview.setwidth (240);// Textview.setheight (+); textview.setgravity (Gravity.center); Textview.settextcolor (Color.Black); Textview.setbackgroundresource (R.DRAWABLE.S); Textview.setpadding ((a);//Textview.setlayoutparams ( Textlayoutparams); return TextView;}}}


"Android Combat" Scrollview+gridview+viewpager implementation navigation page

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.