When tabhost and viewpager are combined, the first page is not displayed.

Source: Internet
Author: User

=============== Problem description ====================


I use tabhost and viewpager to achieve the sliding switching effect. However, when a program is opened, the first page of viewpager is not displayed (blank), and only other tabs are clicked, click the first tab to display the first page of viewpager. setcurrentitem (0) command, but it still does not work. Which of the following experts can give you some advice !!!

Package com. Product. shiningbaby. Fragment;

Import Android. OS. Bundle;
Import Android. Support. v4.app. Fragment;
Import Android. Support. v4.app. fragmentmanager;
Import Android. Support. v4.app. fragmentstatepageradapter;
Import Android. Support. v4.view. pageradapter;
Import Android. Support. v4.view. viewpager;
Import Android. Support. v4.view. viewpager. onpagechangelistener;
Import Android. util. log;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. viewgroup;
Import Android. widget. tabhost;
Import Android. widget. tabhost. ontabchangelistener;
Import Android. widget. tabwidget;
Import Android. widget. textview;

Import com. Product. shiningbaby. R;

Public class fragmentimage extends fragment {

Private tabhost mtabhost;
// Private tabwidget mtabwidget;
Private viewpager mviewpager;
Private pageradapter mpageradapter;
Private string [] tags = {"Most Popular", "latest", "dynamic "};

@ Override
Public View oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate)
{
View root = Inflater. Inflate (R. layout. fragment_image, container, false );
Mviewpager = (viewpager) root. findviewbyid (R. Id. viewpager1 );
Mpageradapter = new slidepageradapter (getfragmentmanager ());
Mviewpager. setadapter (mpageradapter );
Mviewpager. setcurrentitem (0 );

Mtabhost = (tabhost) root. findviewbyid (Android. R. Id. tabhost );
Mtabhost. Setup ();
Mtabhost. addtab (mtabhost. newtabspec ("tab1"). setindicator (tags [0]). setcontent (R. Id. viewpager1 ));
Mtabhost. addtab (mtabhost. newtabspec ("tab2"). setindicator (tags [1]). setcontent (R. Id. viewpager1 ));
Mtabhost. addtab (mtabhost. newtabspec ("tab3"). setindicator (tags [2]). setcontent (R. Id. viewpager1 ));

Mtabhost. setontabchangedlistener (New ontabchangelistener ()
{
// When the program is opened for the first time, the tabchange time will not occur and will only happen after clicking
@ Override
Public void ontabchanged (string Tabid)
{
Mviewpager. setcurrentitem (mtabhost. getcurrenttab ());
Log. D ("myfather", "currenttab is:" + mtabhost. getcurrenttab ());

}
});

Mviewpager. setonpagechangelistener (New onpagechangelistener ()
{
@ Override
Public void onpageselected (INT index)
{
Mtabhost. setcurrenttab (INDEX );
}

@ Override
Public void onpagescrolled (INT arg0, float arg1, int arg2)
{

}

@ Override
Public void onpagescrollstatechanged (INT arg0)
{

}
});

Return root;

}

@ Override
Public void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
}

Private class slidepageradapter extends fragmentstatepageradapter
{
Public slidepageradapter (fragmentmanager FM)
{
Super (FM );
}

@ Override
Public fragment getitem (INT position)
{
Return slidefragment. Create (tags [position]);
}

@ Override
Public int getcount ()
{
Return tags. length;
}

}

Public static class slidefragment extends Fragment
{
Public static slidefragment create (string tag)
{
Slidefragment F = new slidefragment ();
Bundle B = new bundle ();
B. putstring ("tag", tag );
F. setarguments (B );
Return F;
}

@ Override
Public View oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate)
{

View v = Inflater. Inflate (R. layout. fragment_image_newest, container, false );
Textview t = (textview) v. findviewbyid (R. Id. text1 );
T. settext (getarguments (). getstring ("tag "));
Return V;
}
}

}

============= Solution 1 ======================


This is not displayed, and the reason is not found for half a day. In the final method, set it to 1 and then to 0.
In your code, it becomes
Mviewpager. setcurrentitem (1 );
Mviewpager. setcurrentitem (0 );

When tabhost and viewpager are combined, the first page is not displayed.

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.