将接口传递给片段



让我们考虑一个我有Fragment AFragment B的情况。

B声明:

public interface MyInterface {
    public void onTrigger(int position);
}

A实现此接口。

Fragment B推送到堆栈中时,我应该如何在Bundle中传递Fragment A的引用,以便A在需要时可以获取onTrigger回调。

我的用例场景是,A ListView了项目,B ViewPager了项目。两者都包含相同的项目,当用户在弹出B之前从B -> A开始时,它应该触发回调,A更新其ListView位置以匹配B寻呼机位置。

谢谢。

Passing interface to Fragment

我认为您正在两个Fragment之间进行交流

为此,您可以查看与其他片段的通信

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 ");
        }
    }
    ...
}

对于 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);
    }
}

两个片段最好只通过一个活动进行通信。因此,您可以在片段 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);
    }

祝你好运!

使用@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");
        }
    }
}

我认为你应该使用沟通,正如我在下面写的。这段代码来自这个 Android Dev 页面,其中包含片段之间的通信:

标题片段

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);
}
// ...
}

主活动

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
}

您可以通过这种方式创建回调接口。

 var screenVisibility=activity as YourActivity
 screenVisibility.setScreenVisibility("which screen you want")

相关内容

  • 没有找到相关文章

最新更新