23

と がある場合を考えてみましょFragment AFragment B

B宣言します:

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

Aこのインターフェースを実装します。

スタックにプッシュするとき、必要に応じてコールバックを取得できるように、その参照Fragment Bをどのように渡す必要がありますか。Fragment ABundleAonTrigger

私のユース ケース シナリオは、with with items とAhas with items です。どちらにも同じアイテムが含まれており、ユーザーがポップする前から移動すると、ページャーの位置と一致するように位置を更新するためのコールバックがトリガーされます。ListViewBViewPagerB -> ABAListViewB

ありがとう。

4

6 に答える 6

6

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);
    }
}
于 2015-12-18T03:50:28.787 に答える
1

2 つのフラグメントがアクティビティを介してのみ通信することが最適です。したがって、アクティビティに実装されているフラグメント 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);
    }

幸運を!

于 2016-07-15T00:35:51.903 に答える
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");
        }
    }
}
于 2014-11-27T15:15:13.833 に答える