问题
I need to perform onClick to call the onItemSelected listener method of another class.I don't know how to call that method in Image button onClick listener.So that it will move to HomeFirstFragment Class.
ItmeSelectedListener
public interface ItemSelectedListener {
public void onItemSelected(final int position, final String content);
}
LayoutActivity.java:
public class LayoutActivity extends Activity implements OnClickListener {
ImageButton btn_click;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid3);
btn_click = (ImageButton) findViewById(R.id.btn_click);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_click:
break;
}
}
}
MainActivity.java:
public class MainActivity extends ActionBarActivity implements OnTabChangeListener,ItemSelectedListener {
private TextView action_bar_hometext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onItemSelected(int position, String content)
{
if(position==0)
{
action_bar_hometext.setText(content);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
HomeFirstFragment newFragment = new HomeFirstFragment();
ft.replace(R.id.realtabcontent, newFragment);
ft.addToBackStack(null);
ft.commit();
}
}
If I click the btn_click in LayoutActivity class,I need to perform onClick to call interface method from MainActivity.
Anyone can help me with this.Thank you.
回答1:
You may create your own listener and add the block of code that you want to execute on click in your own listener.
Create your Interface like
Interface MyListener{
public void myClickListener(String content);
}
Now implement this in your MainActivity
public class MainActivity extends ActionBarActivity implements OnTabChangeListener,ItemSelectedListener,MyListener {
public void myClickListener(String content){
action_bar_hometext.setText(content);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
HomeFirstFragment newFragment = new HomeFirstFragment();
ft.replace(R.id.realtabcontent, newFragment);
ft.addToBackStack(null);
ft.commit();
}
}
回答2:
You need to register your MainActivity class in the LayoutActivity class, so that LayoutActivity class can invoke the interface's method.
Add this to your LayoutActivity.java:
private static ItemSelectedListener mListener = null;
public static void register(ItemSelectedListener listener){
mListener = listener;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_click:
if(mListener!=null){
// ADD THIS LINE
mListener.onItemSelected(POS/*Your position*/, CONTENT/*Your content*/);
}
break;
}
}
Now, in your MainActivity class, register it to the LayoutActivity class and follows:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutActivity.register(this);
}
Hope it helps! :)
来源:https://stackoverflow.com/questions/31602233/onclick-to-call-interface-method-from-mainactivity