stepstone-tech / android-material-stepper

This library allows to use Material steppers inside Android applications.
Apache License 2.0
1.78k stars 261 forks source link

Overlapping Fragments #118

Closed seaskyways closed 7 years ago

seaskyways commented 7 years ago

Hello. This is my first time using this library so I may be missing something. What happened is the following: I had to fragments , implement Step, an extension of AbstractFragmentStepAdapter. And the result was two tabs, two pages, two steps, and two fragments overlapping in the first tab (1st fragment on top). Here is some code, written in Kotlin, Anko, and XML. Fragment 1:

class ProductFormMainInfoStepFragment : Fragment(), Step {

    val ui = ProductFormMainInfoStepFragmentUI()

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return uiFromAnkoComponent(ui)
    }

    override fun onSelected() { }

    override fun verifyStep(): VerificationError? = null

    override fun onError(error: VerificationError) { }
}

Fragment 2:

class ProductFormSourceItemsSelectorStepFragment : RecyclerViewFragment(), Step {

    override fun onSelected() {
        ui.startLoading("Hello")
    }

    override fun verifyStep(): VerificationError? = null

    override fun onError(error: VerificationError) { }
}

Adapter:

class ProductFormStepperAdapter(fm: FragmentManager, val ctx: Context) : AbstractFragmentStepAdapter(fm, ctx) {
    override fun isViewFromObject(view: View?, `object`: Any?): Boolean = true

    @Suppress("IMPLICIT_CAST_TO_ANY")
    override fun createStep(position: Int): Step {
        val newStep = when (position) {
            0 -> ProductFormMainInfoStepFragment()
            1 -> ProductFormSourceItemsSelectorStepFragment()
            else -> throw IndexOutOfBoundsException()
        } as Step
        return newStep
    }

    override fun getCount(): Int = 2

    override fun getViewModel(position: Int): StepViewModel {
        return when (position) {
            0 -> StepViewModel.Builder(ctx)
                    .setTitle(product_info)
                    .setNextButtonLabel(source_items)
                    .create()
            else -> super.getViewModel(position)
        }
    }
}

product_form_stepper.xml (included in my layout)

<com.stepstone.stepper.StepperLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/product_form_stepper_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:ms_stepperFeedbackType="tabs"
    app:ms_stepperType="tabs"/>

Result is as follows: Step 1: device-2017-05-15-170951

Step 2: device-2017-05-15-171025

Go back to step 1: device-2017-05-15-171037

zawadz88 commented 7 years ago

Hi @seaskyways, This line smells a bit funky: override fun isViewFromObject(view: View?,object: Any?): Boolean = true

Maybe this is the problem? See https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#isViewFromObject(android.view.View,%20java.lang.Object)

seaskyways commented 7 years ago

Ahh it worked. It totally is my problem. I overridden this method since I thought I was required to. Now it works , thanks man