為什么使用DialogFragment
- 可以在屏幕旋轉(zhuǎn)或按下返回鍵可以更好的處理其生命周期
- 在平板中可以將其嵌入在UI中,類似傳統(tǒng)的fragment的使用
怎么使用DialogFragment
- 重寫onCreateView()方法
```
public class MyDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_dialog, container);
return view;
}
}
```
- 重寫onCreateDialog()方法
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(),R.style.theme);
builder.setTitle("對(duì)話框");
View view = inflater.inflate(R.layout.fragment_dialog, container);
builder.setView(view);
builder.setPositiveButton("確定", null);
builder.setNegativeButton("取消", null);
return builder.create();
}
}
- 調(diào)用方式
MyDialogFragment fragment= new MyDialogFragment ();
fragment.show(getFragmentManager(), "MyDialogment");
- 可根據(jù)屏幕尺寸決定將fragment顯示為對(duì)話框還是全屏
public void showDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
CustomDialogFragment newFragment = new CustomDialogFragment();
if (mIsLargeLayout) {
// The device is using a large layout, so show the fragment as a dialog
newFragment.show(fragmentManager, "dialog");
} else {
// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = fragmentManager.beginTransaction();
// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null).commit();
}
}
事件傳遞
- 宿主為activity
官方指南中是通過fragment的onAttach()方法,將activity強(qiáng)轉(zhuǎn)為回調(diào)listener.
```
public class NoticeDialogFragment extends DialogFragment {
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
}
```
> 宿主通過實(shí)現(xiàn)listener,來完成回調(diào)
```
public class MainActivity extends FragmentActivity
implements NoticeDialogFragment.NoticeDialogListener{
public void showNoticeDialog() {
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
}
}
```
- 宿主為fragment
> 通過fragment的onActivityResult方法來進(jìn)行回調(diào),需要設(shè)置targetFragment
宿主設(shè)置
```
public class HostFragment extends Fragment{
//開啟dialog
public void showDialog{
MyDialogFragment fragment= new MyDialogFragment();
fragment.setTargetFragment(context, REQUEST_CODE);
fragment.show(getFragmentManager(), "MyDialogment");
}
//宿主回調(diào)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
//do something
break;
}
}
}
}
```
調(diào)用回調(diào)
```
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, data);
```
- 混合情況.作為一個(gè)common的dialog
> 參考[Android DialogFragment 在頁面銷毀下的使用方式 \- 簡(jiǎn)書](http://www.reibang.com/p/5ba9f36a4a90)
疑難雜癥
- 調(diào)用show方法的時(shí)候會(huì)出現(xiàn)IllegalStateException
具體原因可見 記一個(gè)DialogFragment.show()的Bug - 簡(jiǎn)書
```
//重寫show方法加入try/catch
public class BaseDialogFragment extends AppCompatDialogFragment {
@Override
public void show(FragmentManager manager, String tag) {
try {
super.show(manager, tag);
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
```