求DialogFragment的实现
weituotian opened this issue · 24 comments
网上都说dialogfragment 比 dialog好,能不能改为dialogfragment?
DialogFragment好在是有生命周期,DialogFragment内部也是Dialog。它提供有可以自定义Dialog的方法,用HappyBubble替换内部默认的就行了。
原来可以这样。
嗯,如果没有必要的话不必要弄的那么复杂哦
重写dialogfragment时遇到一个问题
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_timeline_item_more, null);
mViewHolder = new ViewHolder(rootView);
return new BubbleDialog(getActivity())
.calBar(true)
.setPosition(BubbleDialog.Position.TOP)
.softShowUp()
.autoPosition(true)
.setThroughEvent(false, true)
.addContentView(rootView);
}
重写dialogfragment 的 onCreateDialog 这个方法
没法调用 BubbleDialog 的 setClickedView 方法去设置是从哪个view点击的?怎么办?
而且dialogfragment的初始化方法要求setArguments(bundle), bundle要求put实现了Parcelable的类,然后变复杂了。
你可以写一个类继承BubbleDialog,在这个类里面想怎么操作都行。然后直接返回这个类的给这个方法就行了
为什么会有要求setArguments呢?显示是这样显示的
mDialogFragment.show(getFragmentManager(), "DialogFragment");
Bundle arguments = new Bundle();
arguments.putString(KEY_LOADING_TEXT, loadingText);
MyDialogFragment = new MyDialogFragment ();
dialog.setArguments(arguments);
要把被点击的view 作为参数 传给 dialogfragment,
diafragment 初始化 BubbleDialog的时候可以getArguments(), 获得被点击的view,然后BubbleDialog可以显示在被点击的view的上方。
我就想传这么大一个view不太好吧
我的Dialog继承DialogFragment ,内部是可以自由操作BubbleDialog的
public class MyDialogFragment extends DialogFragment {
private Context mContext;
private View rootView;
private ViewHolder mViewHolder;
private BubbleDialog bubbleDialog;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_timeline_item_more, null);
mViewHolder = new ViewHolder(rootView);
bubbleDialog = new BubbleDialog(getActivity())
.calBar(true)
.setPosition(BubbleDialog.Position.TOP)
.softShowUp()
.autoPosition(true)
.setThroughEvent(false, true)
.addContentView(rootView);
return bubbleDialog;
}
private static class ViewHolder
{
Button btn13, btn14, btn15;
public ViewHolder(View rootView)
{
btn13 = (Button) rootView.findViewById(R.id.button13);
btn14 = (Button) rootView.findViewById(R.id.button14);
btn15 = (Button) rootView.findViewById(R.id.button15);
}
}
}
原来是这个意思!你可以试试这样。
通过提前调用setClickedView()方法把被点击的view放进去
public class MyDialogFragment extends DialogFragment {
private Context mContext;
private View rootView;
private ViewHolder mViewHolder;
private BubbleDialog bubbleDialog;
private View mClickedView;//new code
public void setClickedView(View clickView)//new code
{
mClickedView = clickView;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
rootView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_timeline_item_more, null);
mViewHolder = new ViewHolder(rootView);
bubbleDialog = new BubbleDialog(getActivity())
.calBar(true)
.setPosition(BubbleDialog.Position.TOP)
.softShowUp()
.autoPosition(true)
.setClickedView(mClickedView)//new code
.setThroughEvent(false, true)
.addContentView(rootView);
return bubbleDialog;
}
private static class ViewHolder
{
Button btn13, btn14, btn15;
public ViewHolder(View rootView)
{
btn13 = (Button) rootView.findViewById(R.id.button13);
btn14 = (Button) rootView.findViewById(R.id.button14);
btn15 = (Button) rootView.findViewById(R.id.button15);
}
}
}
如果我这样调用的话
MyDialogFragment dialog=new MyDialogFragment();
dialog.setClickedView(view);
有一点不能保证的是
DialogFragment 里面的onCreateDialog 先执行
还是setClickedView先执行
万一onCreateDialog 里面先执行,mClickedView可能为空。
这样写因该是没有问题的,因为onCreateDialog调用始终会在之后
嗯,有一个潜在的问题是横竖屏切换后,mClickedView会为空,
我也很纠结这个问题。
切换横竖屏的时候再相关的生命周期中重新初始化比如说在onStart()中进行初始化
或者说在这里setClickedView,可以试试看
确实可以这么做。
如果BubbleDialog 可以在dialog显示后,
提供方法,转移到其它任意view上显示箭头就好了
就更好了(=・ω・=)
这个想法不错,因为我用的时候都是不同的dialog样式,没想到过这需求
还需要你帮忙试一试,通过setClickedView就可以直接重新更新位置
happy-bubble-release.zip
@xujiaji 好的,不过我这边有点下载不下来
我这边已经出门了,要不你加我一下QQ吧!回去发给你。
QQ号:624719201
@weituotian 你好!有试过有什么问题吗?
@xujiaji 很棒,非常好用!我写了个demo。
好的,那我晚上在上传上去。你这里面的测试代码可以让我拷贝进来吗?(≧▽≦)
嗯,随便用。(⌒▽⌒)
@weituotian 新版本以更新,可以直接引用了。并添加新方法“setRelativeOffset(int)”设置dialog相对于被点击view的偏移量