Fragment监听返回键

2017-08-31  本文已影响25人  冉桓彬

稍微进行了一起提取, 把公共方法写在了BaseUi中.

public abstract class BaseUi extends AppCompatActivity {
    protected Context mContext;
    protected KeyBackDownListener mKeyBackDownListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getApplicationContext;
        initView();
        initData();
        initListener();
    }

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK) {
            return true;
        }
        if (mKeyBackDownListener != null) {
            return mKeyBackDownListener.onKeyBackDownForFragment();
        } else {
            return onKeyBackDownForActivity(keyCode, event);
        }
    }

    public boolean onKeyBackDownForActivity(int keyCode, KeyEvent event) {
        return true;
    }

    public void addKeyDownListener(KeyBackDownListener keyBackDownListener) {
        mKeyBackDownListener = keyBackDownListener;
    }

    public void removeKeyDownListener() {
        mKeyBackDownListener = null;
    }
}
public interface KeyBackDownListener {
    boolean onKeyBackDownForFragment();
}
public class FragmentUi extends BaseUi {

    private AlertDialog mBackDialog;

    @Override
    protected void initView() {
        setContentView(R.layout.ui_fragment);
        TestFragment testFragment = new TestFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_frameLayout, testFragment).commit();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public boolean onKeyBackDownForActivity(int keyCode, KeyEvent event) {
        showExitDialog();
        return true;
    }

    private void showExitDialog() {
        mBackDialog = new AlertDialog.Builder(this).setMessage("确定要退出吗?")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mBackDialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mBackDialog.dismiss();
                    }
                })
                .show();
    }
}
public class TestFragment extends Fragment implements KeyBackDownListener, View.OnClickListener {

    private FragmentUi mFragmentUi;
    private Button mBtnAddKeyDownListener;
    private Button mBtnRemoveKeyDownListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mFragmentUi = (FragmentUi) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_test, null);
        mBtnAddKeyDownListener = (Button) rootView.findViewById(R.id.btnAddKeyDownListener);
        mBtnRemoveKeyDownListener = (Button) rootView.findViewById(R.id.btnRemoveKeyDownListener);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mFragmentUi.addKeyDownListener(this);
        mBtnAddKeyDownListener.setOnClickListener(this);
        mBtnRemoveKeyDownListener.setOnClickListener(this);
    }

    @Override
    public boolean onKeyBackDownForFragment() {
        LogUtils.log(getClass(), "onKeyDown()->1");
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAddKeyDownListener:
                mFragmentUi.addKeyDownListener(this);
                break;
            case R.id.btnRemoveKeyDownListener:
                mFragmentUi.removeKeyDownListener();
                break;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读