"Android" monitor button in Viewpager sub-page

Source: Internet
Author: User

Recently do the project encountered viewpager+fragment sliding page, to listen to the buttons in the sub-page, the online search for some solutions:

Method One :

This method is in the adapter initialization to listen, some people pass through, but I inherited Fragmentpageradapter, failed.

@Override
Public Object Instantiateitem (final view view, int position) {
Switch (position) {
Case 0:
Btnknowledge = (Button) mlistviews.get (position). Findviewbyid (R.id.tab_knowledge);
Btnknowledge.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Toast.maketext (Homeactivity.this, "XXXXXX", 1). Show ();
}
});
Break

Default
Break
}
((Viewpager) view). AddView (Mlistviews.get (position), 0);
return Mlistviews.get (position);
}

Method Two :

This method is also my own method, can be solved. Not much to say, directly on the code.

Private class Shinstarterhelppageadapter extends Fragmentpageradapter {

Public Shinstarterhelppageadapter (fragmentmanager FM) {
Super (FM);
}

@Override
Public Fragment getItem (int position) {
Starterhelppagefragment fragment = null;
if (Position! = 2)
fragment = new Starterhelppagefragment ();
Else
fragment = new Starterhelpfirstpagefragment (Selectirm03listener, Selectirm01listener, Selectirm02listener);
Bundle bundle = new bundle ();
Bundle.putint (starterhelppagefragment.key_res_id, resids[position]);
Fragment.setarguments (bundle);

return fragment;
}

@Override
public int GetCount () {
return resids.length;
}
}

public class Starterhelpfirstpagefragment extends Starterhelppagefragment {
Private Onclicklistener selectirm03listener = null;
Private Onclicklistener selectirm02listener = null;
Private Onclicklistener selectirm01listener = null;

Public Starterhelpfirstpagefragment (Onclicklistener Selectirm03listener, Onclicklistener SelectIRM01Listener, Onclicklistener Selectirm02listener) {
This.selectirm01listener = Selectirm01listener;
This.selectirm02listener = Selectirm02listener;
This.selectirm03listener = Selectirm03listener;
}

@Override
public void onviewcreated (view view, Bundle savedinstancestate) {
super.onviewcreated (view, savedinstancestate);

Button Again_connect = (button) View.findviewbyid (R.id.again_connect);
Button Histus = (button) View.findviewbyid (r.id.histus);
Button St_search = (button) View.findviewbyid (R.id.st_search);

Again_connect.setonclicklistener (This.selectirm01listener);
Histus.setonclicklistener (This.selectirm02listener);
St_search.setonclicklistener (This.selectirm03listener);
}
}

Private Onclicklistener Selectirm03listener = new Onclicklistener () {

@Override
public void OnClick (View v) {

}
};

Private Onclicklistener Selectirm01listener = new Onclicklistener () {

@Override
public void OnClick (View v) {

}
};
Private Onclicklistener Selectirm02listener = new Onclicklistener () {

@Override
public void OnClick (View v) {

}
};

This method is directly to the third page to take out, listen to the individual, and then put into the viewpager inside, I test, the code through the compilation, I hope to help you.

"Android" monitor button in Viewpager sub-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.