0

我想创建一个 ViewGroup,其中包含未知数量的其他视图作为子视图(如 LinearLayout):

<Wizard id=... width=... height=...>
    <WizardStep id=... nextButtonOnClick=... preButtonOnClick=... >
        <LinearLayout ...>
           ...
        </LinearLayout>
    </WIzardStep>

    <WizardStep id=... nextButtonOnClick=... preButtonOnClick=... >
        <RelativeLayout ...>
            ...
        </RelativeLayout>
    </WIzardStep>

    <WizardStep id=... nextButtonOnClick=... preButtonOnClick=... >
        ...
    </WIzardStep>
</Wizard>

这里的 Wizard 和 WizardStep 都是 ViewGroup。我不知道从哪里开始。我只需要扩展 ViewGroup 和所需的功能吗?我将感谢任何帮助、文档、博客等。

4

2 回答 2

0

我的解决方案:

向导视图类:

public class WizardView extends RelativeLayout {
Context context;

private final String KEY_LAST_STEP_PASSED = "KEY_LAST_STEP_PASSED";
private final String KEY_WIZARD_FINISHED = "KEY_WIZARD_FINISHED";

private int currentStep;
private boolean isWizardFinished;

private SharedPreferences preferences;

public WizardView(Context context) {
    super(context);
    this.context = context;
}

public WizardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
}

public WizardView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
}

public void startWizard() {
    preferences = context.getSharedPreferences("wizard_pref", Context.MODE_PRIVATE);

    isWizardFinished = preferences.getBoolean(KEY_WIZARD_FINISHED, false);
    if (!isWizardFinished) {
        currentStep = preferences.getInt(KEY_LAST_STEP_PASSED, 0);
        // Make all Steps (except current step) GONE
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (currentStep == i) {
                child.setVisibility(View.VISIBLE);
            } else {
                child.setVisibility(View.GONE);
            }
        }
    }
}

public void nextStep() {
    int count = getChildCount();
    getChildAt(currentStep).setVisibility(View.GONE);

    if (currentStep < (count - 1)) {
        currentStep++;
        preferences.edit().putInt(KEY_LAST_STEP_PASSED, currentStep).commit();
        getChildAt(currentStep).setVisibility(View.VISIBLE);
    } else {
        finishWizard();
    }
}

public void previousStep() {
    if (currentStep > 0) {
        getChildAt(currentStep).setVisibility(View.GONE);
        currentStep--;
        preferences.edit().putInt(KEY_LAST_STEP_PASSED, currentStep).commit();
        getChildAt(currentStep).setVisibility(View.VISIBLE);
    }
}

public void finishWizard() {
    isWizardFinished = true;
    preferences.edit().putBoolean(KEY_WIZARD_FINISHED, true).commit();
    setVisibility(View.GONE);
}  
}

WizardStep类:

public class WizardStepView extends RelativeLayout {

public WizardStepView(Context context) {
    super(context);
}

public WizardStepView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public WizardStepView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

}

带有向导的示例布局文件:

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

<com.example.wizardtest.WizardView 
    android:id="@+id/wizard"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.wizardtest.WizardStepView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="wizardNext"
            android:text="Next(2)" />
    </com.example.wizardtest.WizardStepView>

    <com.example.wizardtest.WizardStepView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="wizardNext"
            android:text="Next(3)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:onClick="wizardPrevious"
            android:text="Previous(1)" />
    </com.example.wizardtest.WizardStepView>

    <com.example.wizardtest.WizardStepView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:onClick="wizardPrevious"
            android:text="Previous(2)" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="wizardNext"
            android:text="Finish" />
    </com.example.wizardtest.WizardStepView>
</com.example.wizardtest.WizardView>

</RelativeLayout>

活动:

WizardView wizardView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wizardView = (WizardView) findViewById(R.id.wizard);
    wizardView.startWizard();
}
于 2013-09-26T08:09:01.647 回答
0

这可以通过ViewFlipper来实现。如果您想以编程方式进行,这将是一个开始。(注意:我没有测试这段代码,只是把它写在我的脑海中)

xml布局flip.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flippy"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ViewFlipper>

活动看起来像这样

public class MainActivity extends Activity{
    ViewFlipper flip;
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    setContentView(R.layout.flip);
    flip = (ViewFlipper)findViewById(R.id.flip);

}
private void addViews(){
    //This is where you would determine which views to add
    flip.add(****** your view 1 *****)
    flip.add(****** your view 2 *****)
}

每当您解析数据以动态创建它们时,您都会添加视图。然后导航您的视图,您可以使用:

flip.showNext()
flip.showPrevious()

你可以用它做更复杂的事情,但这应该会给你基础。

于 2013-09-25T20:33:13.670 回答