Android uses Viewpager to implement navigation menus

Source: Internet
Author: User

First set the fragment layout of the page:

Public class Tabfragment extends Listfragment {
@Override
Public void onviewcreated (view view, Bundle savedinstancestate) {
super.onviewcreated (view, savedinstancestate);
Bundle bundle = Getarguments ();
String url = bundle.getstring ("url");
list<string> datas = new arraylist<> ();
for (int i = 0; i <; i++) {
datas.add (url + "-------->" + i);
}
arrayadapter<string> adapter = new Arrayadapter<string> (Getactivity (), Android. R.layout
. Simple_list_item_1, datas);
Setlistadapter (adapter);
}
}
Setting the Viewpager adapter
Public class Myfragmentpageradapter extends Fragmentpageradapter {
private string[] URLs;

Public Myfragmentpageradapter (Fragmentmanager FM, string[] URLs) {
super (FM);
this.urls = URLs;
}

@Override
Public Fragment GetItem (int position) {
Fragment Fragment = new Tabfragment ();
Bundle bundle = new bundle ();
bundle.putstring ("url", Urls[position]);
fragment.setarguments (bundle);
return fragment;
}

@Override
public int GetCount () {
return urls.length;
}
}
Code:
Public class Tabactivity extends Appcompatactivity {
private Viewpager Vp_tab;
private LinearLayout ll_title;//title
Private View v_indicator;//indicator
private int mcurrentindex = 0;//record current position
private Horizontalscrollview Hsv_tab;
private string[] titles = {"Social", "military", "entertainment", "Sports", "technology", "local", "economic", "property"};
private string[] URLs = {"Social url", "Military url", "Entertainment url", "Sports url", "Technology url", "Local url", "Economic url",
"Property url"};

@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_tab);
Initview ();
}

private void Initview () {
Vp_tab = (viewpager) Findviewbyid (r.id.vp_tab);
Hsv_tab = (horizontalscrollview) Findviewbyid (r.id.hsv_tab);
Ll_title = (linearlayout) Findviewbyid (r.id.ll_title);
v_indicator = Findviewbyid (r.id.v_indicator);
Fragmentpageradapter adapter = new Myfragmentpageradapter (Getsupportfragmentmanager (),
URLs);
Vp_tab.setadapter (adapter);
InitTab ();
}

private void InitTab () {
int pixels = getresources (). Getdisplaymetrics (). widthpixels;//Get screen width
final int titlewidth = pixels/4;//Set title width
viewgroup.layoutparams params = V_indicator.getlayoutparams ();
params.width = titlewidth;
v_indicator.setlayoutparams (params);//Set the same width for the indicator
for (int i = 0; i < titles.length; i++) {
final TextView tv_title = new TextView (this);//Set Caption
Tv_title.settext (Titles[i]);
if (i = = Mcurrentindex) {
Tv_title.settextcolor (color.red);
}
tv_title.setwidth (titlewidth);
tv_title.setgravity (gravity.center);
tv_title.setpadding (5, 5, 5, 0);
Tv_title.settag (i);//Record the location of each title corresponding to the page
//Add Click to listen, let Viewpager change with the title change
Tv_title.setonclicklistener (New View.onclicklistener () {
@Override
Public void OnClick (View v) {
Vp_tab.setcurrentitem ((int) Tv_title.gettag (), false);
}
});
Ll_title.addview (tv_title);//Add the title TextView to the LinearLayout
}
Vp_tab.addonpagechangelistener (New Viewpager.onpagechangelistener () {
@Override
Public void onpagescrolled (int position, float positionoffset, int
positionoffsetpixels) {
linearlayout.layoutparams params = (linearlayout.layoutparams) v_indicator
. Getlayoutparams ();
//Set left margin
params.leftmargin = (int) ((position + positionoffset) * titlewidth);
v_indicator.setlayoutparams (params);
//Let ScrollView follow the move
Hsv_tab.scrollto (params.leftmargin-titlewidth, 0);
}

@Override
Public void onpageselected (int position) {
//Set the original TextView color to Gray,
TextView tv_title = (TextView) ll_title.getchildat (mcurrentindex);
Tv_title.settextcolor (color.gray);
Mcurrentindex = position;//Gets the current page index
Tv_title = (TextView) ll_title.getchildat (mcurrentindex);
//Set the current TextView color to red
Tv_title.settextcolor (color.red);
}

@Override
Public void Onpagescrollstatechanged (int.) {

}
});
}

}

Android uses Viewpager to implement navigation menus

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.