Activity和Fragment之间的通信

219次阅读
没有评论

Activity中触发,Fragment中响应 Activity写接口和触发

private OnClickActivityListener mOnClickActivityListener;

/**
 *接口定义
**/
public interface OnClickActivityListener {
    void onClickActivity();
}

/**
 * 绑定接口
 *
 * @param
 */
@Override
public void onAttachFragment(Fragment fragment) {
    try {
        mOnClickActivityListener = (OnClickActivityListener) fragment;
    } catch (Exception e) {

    }
    super.onAttachFragment(fragment);
}


//触发
btnActivity.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(mOnClickActivityListener!=null){
            mOnClickActivityListener.onClickActivity();
        }
    }
});

Fragment中接口实现

public class InterfaceBackFragment extends Fragment implements InterfaceBackActivity.OnClickActivityListener {
    
    @Override
    public void onClickActivity() {
        btnFragment.setText("Activity中的按钮被点击");
    }
}

Fragment中触发,Activity中响应 Fragment中写接口和触发

private OnFragmentInteractionListener mListener;

/**
 *接口定义
**/
public interface OnFragmentInteractionListener {
    void onFragmentInteraction();
}

/**
 *绑定
**/
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

//触发
btnFragment.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onFragmentInteraction();
        }
    }
});

Activity中接口实现

public class InterfaceBackActivity extends AppCompatActivity implements InterfaceBaceFragment.OnFragmentInteractionListener{
    @Override
    public void onFragmentInteraction() {
        Toast.makeText(getApplicationContext(), "点击了Fragment中的控件", Toast.LENGTH_SHORT).show();
    }
}

Activity中触发,Adapter中响应 Adapter接口定义和触发

/**
 *接口定义
**/
public interface OnChildItemClickListener {
    void onItemClick(int pos);
}

/**
 *Adapter中的构造函数传入listener
**/
public InterfaceBackAdapter(ArrayList<String> mList, OnChildItemClickListener listener) {
    this.mList = mList;
    mListener = listener;
}

//触发
holder.mTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onItemClick(position);
        }
    });

Activity中实现

private InterfaceBackAdapter.OnChildItemClickListener mListener;
private InterfaceBackAdapter mInterfaceBackAdapter;

mInterfaceBackAdapter = new InterfaceBackAdapter(mList, mListener);

mListener = new InterfaceBackAdapter.OnChildItemClickListener() {
    @Override
    public void onItemClick(int pos) {
        Toast.makeText(getApplicationContext(), "我点击了第" + pos + "项", Toast.LENGTH_SHORT).show();
    }
};
迷鹿, lostfawn
版权声明:本站原创文章,由迷鹿, lostfawn2021-06-16发表,共计2345字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
载入中...
召唤伊斯特瓦尔