让我们考虑一下Fragment A
和Fragment B
的情况。
B
声明:
public interface MyInterface {
public void onTrigger(int position);
}
A
实现了这个界面。
将Fragment B
推入堆栈时,如何在Fragment A
中为Bundle
传递A
的引用,以便onTrigger
可以在需要时获得A
回调。
我的用例场景是ListView
有B
个项目,ViewPager
有B -> A
个项目。两者都包含相同的项目,当用户在弹出B
之前从A
开始时,它应触发ListView
的回调,以更新它的B
位置以匹配{{ 1}}寻呼机位置。
感谢。
答案 0 :(得分:22)
Passing interface to Fragment
我认为您正在两个Fragment
为了做到这一点,您可以查看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 ");
}
}
...
}
答案 1 :(得分:4)
对于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);
}
}
答案 2 :(得分:1)
两个片段仅通过活动进行通信是最佳的。因此,您可以在活动中实现的Fragment B中定义接口。然后在活动中,在接口方法中定义要在片段A中发生的内容。
在片段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");
}
}
确定用户是否从B到A的方法
public void onChangeFragment(int position){
//other logic here
mCallback.onTrigger(position);
}
在活动中,
public void onTrigger(int position) {
//Find listview in fragment A
listView.smoothScrollToPosition(position);
}
古德勒克!
答案 3 :(得分:0)
使用@Amit的回答,并适应OP问题,以下是所有相关代码:
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");
}
}
}
答案 4 :(得分:0)
我认为您应该使用交流方式,如下所述。这段代码来自this Android Dev page of communication between Fragments:
标题片段
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
}