UI design and combination of HorizontalScrollView and GridView

Source: Internet
Author: User

First look:

It mainly works with HorizontalScrollView and GridView, and the rest is the UI design (this project is only tested in 320x480)

There are comments in the code.

First, write a GridView and give it data.

/***** SubTitleGridView. java *****/

Protected GridView gvSubTitle;
Protected SimpleAdapter saSubTitle; // Adapter
Protected ArrayList <HashMap <String, String> srcSubTitle; // Data Source
View Code

Public SubTitleGridView (Context context, int itemWidth, ArrayList <HashMap <String, String> data ){
Super (context );
This. setOrientation (VERTICAL );

GvSubTitle = new GridView (context );
GvSubTitle. setColumnWidth (itemWidth); // you can specify the width of each page.
GvSubTitle. setNumColumns (GridView. AUTO_FIT); // The number of paging buttons is automatically set.
Addview (gvsubtitle, new layoutparams (layoutparams. fill_parent,
Layoutparams. wrap_content ));
Srcsubtitle = data;

Sasubtitle = new simpleadapter (context, Data, // Data Source
R. layout. gv_subtitle_item, // XML implementation
New String [] {"itemtext"}, // subitem of the dynamic array and imageitem
New int [] {R. Id. itemtext });

Gvsubtitle. setadapter (sasubtitle );

Gvsubtitle. setonitemclicklistener (New onitemclicklistener (){

@ Override
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2,
Long arg3 ){
Gvsubtitle. setselection (arg2 );
If (switchlistener! = NULL) {// when the title is switched

Switchlistener. onsubtitleswitchlistener (arg2, srcsubtitle
. Size ());
}
}

});
}

Then, an interface is thrown to the gridview to call the external operation.

View Code

Protected onsubtitleswitchlistener switchlistener; // callback function for title Switching
//---------------------------------------------------------
/**
* Callback function when the title bar is clicked
*/
Public void setonsubtitleswitchlistener (onsubtitleswitchlistener listen ){
This. switchlistener = listen;
}

Public interface onsubtitleswitchlistener {
Public void onsubtitleswitchlistener (INT pageid, int pagecount );
}

/***** Gridview. Java *****/

Private Final int per = 4; // The number of entries per row
Private gridview;
Private imagebutton webnav_left;
Private imagebutton webnav_right;
Arraylist Horizontalscrollview horizontalview;
View Code

// Calculate the screen and set the columns bandwidth length in the gridview
Displaymetrics dm = new displaymetrics ();
Dm = getapplicationcontext (). getresources (). getdisplaymetrics ();
Int menuwidth = DM. widthpixels-16;
Menuwidth = menuwidth-120;

Int itemwidth = menuwidth/per;

Data = new arraylist Hashmap <string, string> map;
For (INT I = 0; I <10; I ++ ){
Map = new hashmap <string, string> ();
Map. Put ("itemtext", "GG" + I );
Data. Add (MAP );
}
View Code

// This is the interface thrown by the gridview. Different view interfaces can be displayed based on different clicks.
SubTitleGridView subtitle = new SubTitleGridView (this, itemWidth, data );
Subtitle. setOnSubTitleSwitchListener (new OnSubTitleSwitchListener (){

@ Override
Public void onSubTitleSwitchListener (int pageID, int pageCount ){
String str = "Total" + String. valueOf (pageCount) + "current"
+ String. valueOf (pageID) + "page" + "content :"
+ Data. get (pageID). get ("ItemText"). toString ();
Toast. makeText (Gridview. this, str, 1000). show ();

}
});

Here is the sliding effect between the buttons on both sides of the HorizontalScrollView

horizontalview = (HorizontalScrollView) findViewById(R.id.horizontalview);

webnav_left = (ImageButton) findViewById(R.id.webnav_left);
webnav_left.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
System.out.println("filing");
horizontalview.fling(-500);
}
});
webnav_right = (ImageButton) findViewById(R.id.webnav_right);
webnav_right.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
horizontalview.fling(500);
}
});

Good! Here is an introduction. ^-^

Project code

Http://files.cnblogs.com/not-code/seaHorizontalScrollView.rar

This article is original. If you need to reprint it, please indicate the author and the source. Thank you!

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.