Android向导框架(Wizard framework)的一种实现(需要源码的同学请留言)

做界面开发的兄弟姐妹都知道向导框架一般GUI库都有提供,而android的界面库中,却没有向导框架这个东东。
不要惊讶,你要是不相信的话可以到谷歌查询,我在stackoverflow看到一些提问:如何实现向导式的交互,在android中:how to implement a wizard like interaction in android,回答是有两种方法:
1)使用多个activity,每个page一个activity,上一步或下一步的响应就是切换activity。
2)使用fragment,每个page一个fragment,上一步或下一步的响应就是切换fragment。
如果每个page之间非常独立,则用第一种方式;否则建议用第二种。向导框架源码

这里我们介绍使用第二种方式的实现。

1. 实现思路
向导是一个fragment activity,使用fragment layout,layout由两部分组成:顶部的fragment和底部的上一步,下一步按钮。
上一步,下一步按钮,切换对应的fragment。
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
android:id="@+id/wizard_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

                <Button
                 android:id="@+id/wizard_finish_btn"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_alignParentRight="true"
                 android:text="Finish" />

                <Button
                 android:id="@+id/wizard_next_btn"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_toLeftOf="@+id/wizard_finish_btn"
                 android:text="Next" />

                <Button
                 android:id="@+id/wizard_previous_btn"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_toLeftOf="@+id/wizard_next_btn"
                 android:text="Previous" />
        
</RelativeLayout>

</RelativeLayout>

2. 关键的类说明

1)WizardActivity
向导框架的activity类,是一个抽象类,子类实现其抽象方法:
protected abstract List<TabInfo> getPageList();,要求子类返回所有的向导页(page)。
**
* This is the wizard framework.
* We implements wizard like this:
* 1. Wizard is an activity, the activity main UI contains one replacable Fragment and 2 buttons(previous, next).
* 2. When user press previous or next button, current fragment will be replaced.
* The sub class should implements:
* 1. Implements hasNext() method to tell the framework whether there are next page or not, if no next page, the next button will change to finish button.
* 2. Provide wizard data, the wizard data is a Map, the Map key is the page identifier(e.g name) and the value is the fragment resource id.
* 3. Implements the doPrevious(), doNext(), doFinish(), init() methods.
* @since 2012-11-15
*
*/
public abstract class WizardActivity extends FragmentActivity implements WizardOperation{

2)TabFragment
实现Fragment的子类,在其onCreateView函数中,会初始化所有的界面动作

public class TabFragment extends Fragment{

        
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

                View v = inflater.inflate(
layoutResource, container, false);
                Bundle args =
this.getArguments();
                String action_factory_class = args.getString(
"actionFactoryClassName");
                String tag = args.getString(
"tag");
                
if (action_factory_class == null || "".equals(action_factory_class)){
                        
return v;
                }
                
                
//set the UI elements actions
                try {
                        AbstractActionFactory factory = (AbstractActionFactory)Class.forName(action_factory_class).newInstance();
                        factory.setupActions(tag, v,
this.getActivity());

3)AbstractActionFactory
初始化界面action的工厂类,每个page里面的界面元素,使用对应的factory去初始化。
public abstract class AbstractActionFactory {
        
//The resource id and view mapping, used for this factory to manange the relationship.
        private static Map<String, AbstractActionFactory> factories = new HashMap<String, AbstractActionFactory>();
        
        
public abstract void setupActions(String tag, View view, Activity activity);
        
        
public static AbstractActionFactory getActionFactory(Class<?> factoryClass){


/**
* The UI actions of task. All class in this package is acted as the Control role.
* @since 2012-11-6
*
*/
public class CreateTaskActionFactory extends AbstractActionFactory{

        @Override
        public void setupActions(String tag, View view, Activity activity) {
                
//setup the new task action
                Button new_btn = (Button)view.findViewById(R.id.task_panel_new_btn);
                new_btn.setOnClickListener(
new OperateActivity.NewTaskAction(activity));
        }

4)TaskCreationActivity
向导实现类,实现上一步,下一步的响应,以及提供page list
public class TaskCreationActivity extends WizardActivity{

3. 效果图

转载请注明:运维派 » Android向导框架(Wizard framework)的一种实现(需要源码的同学请留言)

1
3.2k
10
  1. 希望可以发我一分源码,谢谢! 最近在弄google map 与tabhost的结合,tab切换一直会出问题,希望这个框架可以解决