Created
May 15, 2018 19:07
-
-
Save bullheadandplato/ef094dd5f1ccd210eb75b46c9bd795ae to your computer and use it in GitHub Desktop.
listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bullhead.myapplication; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
public class AbcActivity extends AppCompatActivity implements FragmentOne.Listener{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_abc); | |
getSupportFragmentManager().beginTransaction() | |
.replace(R.id.container,new FragmentOne()) | |
.commit(); | |
} | |
@Override | |
public void onButtonClick() { | |
Toast.makeText(this,"Button clicked.",Toast.LENGTH_LONG).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bullhead.myapplication; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import butterknife.ButterKnife; | |
import butterknife.OnClick; | |
public class FragmentOne extends Fragment { | |
public interface Listener{ | |
void onButtonClick(); | |
} | |
private Listener listener; | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
listener= (Listener) context; | |
} | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_one, container, false); | |
ButterKnife.bind(this,view); | |
return view; | |
} | |
@OnClick({R.id.btnClick}) | |
public void onClick(View view){ | |
listener.onButtonClick(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bullhead.myapplication; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
public class FragmentTwo extends Fragment { | |
@BindView(R.id.tvClick) | |
TextView tvClick; | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fargment_two, container, false); | |
ButterKnife.bind(this,view); | |
return view; | |
} | |
public void buttonClicked() { | |
tvClick.setText("Button clicked."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bullhead.myapplication; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
public class MainActivity extends AppCompatActivity implements FragmentOne.Listener{ | |
private FragmentOne fragmentOne; | |
private FragmentTwo fragmentTwo; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
fragmentOne=new FragmentOne(); | |
fragmentTwo=new FragmentTwo(); | |
getSupportFragmentManager().beginTransaction() | |
.replace(R.id.frameOne,fragmentOne) | |
.commit(); | |
getSupportFragmentManager().beginTransaction() | |
.replace(R.id.frameTwo,fragmentTwo) | |
.commit(); | |
} | |
@Override | |
public void onButtonClick() { | |
Intent intent=new Intent(this,AbcActivity.class); | |
startActivity(intent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment