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