首页 编程技术Android向导框架(Wizard framework)的一种实现(需要源码的同学请留言)

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

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

做界面开发的兄弟姐妹都知道向导框架一般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 20121115
*
*/
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 2012116
*
*/
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)的一种实现(需要源码的同学请留言)插图

本文链接:https://www.yunweipai.com/2440.html

网友评论comments

回复 nemo222 取消回复

您的电子邮箱地址不会被公开。

  1. xiaoming说道:

    swing中好像也没注意到有这种向导框架?

  2. nicolas说道:

    看看源码

  3. johnny说道:

    可以给源码吗? 谢谢!

    • Simon说道:

      不好意思回复晚了,点这个链接下载:http://blog.tektea.com/archives/2440.html/share

  4. Rising说道:

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

  5. nemo222说道:

    可以给我发一份源码么,谢谢

  6. amy说道:

    可以给一份源码吗,谢谢

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部