【问题标题】:use different fragments for steps使用不同的片段进行步骤
【发布时间】:2018-04-19 18:44:15
【问题描述】:

我使用android-material-stepper 库进行步骤实现,但在这里我可以使用一个片段,它显示了 3 个步骤。

我需要不同的片段用于不同的步骤,如何使用?

我有不同的视图,例如片段 1 有日历,片段 2 有按钮,片段 3 有输入框。我想分别为每 3 个步骤使用所有 3 个片段。

我按照 GitHub 页面所示的方式实现了,他们只使用 1 个片段进行 3 个步骤。有什么方法可以使用 3 个片段吗?如果不是,我如何区分同一片段中每个步骤的 3 个功能?

请帮忙!

【问题讨论】:

  • 请在此处发布一些相关代码,没有人可能会下载该库来帮助您

标签: android android-fragments material-design stepper


【解决方案1】:

您需要在 StepperAdapters createStep() 函数中实现此行为。

你可以这样做:

@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;
}

【讨论】:

    【解决方案2】:

    您可以在“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()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多