Android 将接口传递给 Fragment
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23380097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing interface to Fragment
提问by Niko
Let's consider a case where I have Fragment A
and Fragment B
.
让我们考虑一个我有Fragment A
和 的情况Fragment B
。
B
declares:
B
声明:
public interface MyInterface {
public void onTrigger(int position);
}
A
implements this interface.
A
实现这个接口。
When pushing Fragment B
into stack, how should I pass reference of Fragment A
for it in Bundle
so A
can get the onTrigger
callback when needed.
当推Fragment B
入堆栈时,我应该如何传递Fragment A
对它的引用,Bundle
以便在需要时A
获得onTrigger
回调。
My use case scenario is that A
has ListView
with items and B
has ViewPager
with items. Both contain same items and when user goes from B -> A
before popping B
it should trigger the callback for A
to update it's ListView
position to match with B
pager position.
我的用例场景是A
有ListView
项目和B
有ViewPager
项目。两者都包含相同的项目,当用户从B -> A
弹出之前开始时,B
它应该触发回调A
以更新它的ListView
位置以匹配B
寻呼机位置。
Thanks.
谢谢。
回答by Amit Gupta
Passing interface to Fragment
I think you are communicating between two Fragment
我想你是在两个人之间交流 Fragment
In order to do so, you can have a look into Communicating with Other Fragments
为此,您可以查看Communicating with Other Fragments
public class FragmentB extends Fragment{
MyInterface mCallback;
// Container Activity must implement this interface
public interface MyInterface {
public void onTrigger();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (MyInterface ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement MyInterface ");
}
}
...
}
回答by Krtko
For Kotlin 1.0.0-beta-3595
对于 Kotlin 1.0.0-beta-3595
interface SomeCallback {}
class SomeFragment() : Fragment(){
var callback : SomeCallback? = null //some might want late init, but I think this way is safer
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
callback = activity as? SomeCallback //returns null if not type 'SomeCallback'
return inflater!!.inflate(R.layout.frag_some_view, container, false);
}
}
回答by Yaya
It is optimal for two fragments to only communicate through an activity. So you can define an interface in Fragment B that is implemented in the activity. Then in the activity, define in the interface method what you want to happen in fragment A.
两个片段仅通过一个活动进行通信是最佳的。因此,您可以在 Fragment B 中定义一个在活动中实现的接口。然后在活动中,在接口方法中定义您希望在片段 A 中发生的事情。
In Fragment B,
在片段 B 中,
MyInterface mCallback;
public interface MyInterface {
void onTrigger(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (MyInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement MyInterface");
}
}
Method for determining if user goes from B to A
判断用户是否从B到A的方法
public void onChangeFragment(int position){
//other logic here
mCallback.onTrigger(position);
}
In Activity,
在活动中,
public void onTrigger(int position) {
//Find listview in fragment A
listView.smoothScrollToPosition(position);
}
Goodluck!
祝你好运!
回答by Yaroslav Ovdiienko
I think you should use communication, as I've written below. This code comes from this Android Dev page of communication between Fragments:
我认为你应该使用交流,正如我在下面写的。此代码来自Fragments 之间通信的 Android Dev 页面:
HeadlinesFragment
头条新闻片段
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
public void setOnHeadlineSelectedListener(Activity activity) {
mCallback = activity;
}
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
// ...
}
MainActivity
主要活动
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
// ...
@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof HeadlinesFragment) {
HeadlinesFragment headlinesFragment = (HeadlinesFragment) fragment;
headlinesFragment.setOnHeadlineSelectedListener(this);
}
}
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener {
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
回答by EdmundYeung99
Using @Amit's answer, and adapting to the OPs question, here is all the relevant code:
使用@Amit 的回答,并适应 OPs 问题,这里是所有相关代码:
public class FragmentA extends BaseFragment implements MyInterface {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// THIS IS JUST AN EXAMPLE OF WHERE YOU MIGHT CREATE FragmentB
FragmentB myFragmentB = new FragmentB();
}
void onTrigger(int position){
// My Callback Happens Here!
}
}
...
...
public class FragmentB extends BaseFragment {
private MyInterface callback;
public interface MyInterface {
void onTrigger(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
callback = (MyInterface ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement MyInterface");
}
}
}