简体   繁体   English

使用不同的片段进行步骤

[英]use different fragments for steps

Im using android-material-stepper library for step implementation but here i can use one fragment and it is showing 3 steps.我使用android-material-stepper库进行步骤实现,但在这里我可以使用一个片段,它显示了 3 个步骤。

i need different fragments for different steps, how to use like that?我需要不同步骤的不同片段,如何使用?

i have different views like Fragment 1 is having calendar, Fragment 2 is having buttons, fragment 3 is having input boxes.我有不同的观点,比如片段 1 有日历,片段 2 有按钮,片段 3 有输入框。 i want to use all 3 fragments for each 3 steps respectively.我想分别为每 3 个步骤使用所有 3 个片段。

i implemented as shown in that GitHub page and there they are using only 1 fragment for 3 steps.我按照该 GitHub 页面中所示进行了实施,并且他们在 3 个步骤中仅使用了 1 个片段。 is there any way to use 3 fragments?有没有办法使用3个片段? if not how can i differentiate 3 functions for each steps in same fragment?如果不是,我如何区分同一片段中每个步骤的 3 个功能?

please help!请帮忙!

You need to implement this behaviour in the StepperAdapters createStep() function.您需要在StepperAdapters createStep()函数中实现此行为。

You could do it like this:你可以这样做:

@Override
public Step createStep(int position) {

    Bundle b = new Bundle();
    b.putInt(CURRENT_STEP_POSITION_KEY, position);

    switch (position){
        case 0:
            MyCalendarStepFragment calendarFragment = new MyCalendarStepFragment();
            calendarFragment.setArguments(b);
            return  calendarFragment;
        case 1:
            MyButtonStepFragment buttonFragment = new MyButtonStepFragment();
            buttonFragment.setArguments(b);
            return  buttonFragment;
        case 2:
            MyInputStepFragment inputFragment = new MyInputStepFragment();
            inputFragment.setArguments(b);
            return  inputFragment;
    }

    return null;
}

you can put any value in "CURRENT_STEP_POSITION_KEY" I tried the same process but my first step is empty here is my adapter您可以在“CURRENT_STEP_POSITION_KEY”中输入任何值我尝试了相同的过程但我的第一步是空的这是我的适配器

private val pages = SparseArray<Step>()

override fun createStep(@IntRange(from = 0)position: Int): Step {
    //val step = StepFragmentSample()
    //val b = Bundle()
    //b.putInt(CURRENT_STEP_POSITION_KEY, position)
    //step.setArguments(b)
    //return step
    val b = Bundle()
    val CURRENT_STEP_POSITION_KEY = "CURRENT_STEP_POSITION_KEY"
    b.putInt(CURRENT_STEP_POSITION_KEY, position)
    return when (position) {
        2 -> {
            //FormAddBashDocumentFragment()
            val addBashDocumentFragment = FormAddBashDocumentFragment()
            addBashDocumentFragment.setArguments(b)
            return addBashDocumentFragment

        }
        1 -> {
            //FormAddClaimsFragment()
            val addClaimsFragment = FormAddClaimsFragment()
            addClaimsFragment.setArguments(b)
            return addClaimsFragment
        }
        0 -> {
            //FormAddClaimBatchesFragment()
            val addClaimBatchesFragment = FormAddClaimBatchesFragment()
            addClaimBatchesFragment.setArguments(b)
            return addClaimBatchesFragment
        }

        else -> throw IllegalArgumentException("Unsupported position: " + position)

    }
}

override fun getCount(): Int {
    return 3
}

override fun findStep(position: Int): Step? {
    return if (pages.size() > 0) pages.get(position) else null
}

override fun instantiateItem(container: ViewGroup, position: Int): View {
    var step: Step? = pages.get(position)
    if (step == null) {
        step = createStep(position)
        pages.put(position, step)
    }

    val stepView = step as View
    container.addView(stepView)

    return stepView
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
    container.removeView(`object` as View)
}

override fun getViewModel(@IntRange(from = 0)position: Int): StepViewModel {
    //Override this method to set Step title for the Tabs, not necessary for other stepper types
    val builder = StepViewModel.Builder(context)
        .setTitle("New batch")
    when (position) {
        0 -> builder
            .setTitle("Batch")
            .setEndButtonLabel("claim")
            .setBackButtonLabel("Cancel")
            .setBackButtonStartDrawableResId(StepViewModel.NULL_DRAWABLE)
        1 -> builder
            .setTitle("claim")
            .setBackButtonLabel("Back to batch")
        2 -> builder
            .setTitle("Documents")
            .setBackButtonLabel("Back to claim")
            .setEndButtonLabel("I'm done!")
        else -> throw java.lang.IllegalArgumentException("Unsupported position: $position")
    }
    return builder.create()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DialogFragment显示具有不同“片段”的步骤 - DialogFragment show steps with different “fragments” 使用片段的不同方式 - Different ways to use Fragments getSupportLoaderManager()在不同的片段中使用相同的ID? - getSupportLoaderManager() use same ids in different fragments? 如何使用viewpager在不同片段中使用活动组件 - How to use an activity component in different fragments with viewpager 如何在两个不同的片段中使用存储库数据 - How to use Repository data in two different fragments 如何在两个不同的片段中使用相同的数据 - How to use same data in two different fragments 如何在不同情况下使用fragmentAdapter接收不同的片段 - How to use fragmentAdapter to recieve different fragments in different situations 如何在Android的单个活动中为不同的片段使用不同的工具栏? - How to use different toolbar for different fragments in a single activity in android? 如何使用ViewPager在片段中使用不同的列表视图 - How can i use different listviews in fragments with viewpager 如何在片段中使用不同内容的相同基本布局? - How to use the same basic layout with different contents in fragments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM