停止更改选项卡指示器颜色,直到和除非更改选项卡

时间:2018-05-14 08:03:56

标签: android android-fragments kotlin android-tablayout viewpagerindicator

我的活动中有3个标签,通过在标签lablayout上滑动或选择不会更改标签。但是,即使我通过单击停止更改标签,单击标签时标签指示器颜色也会发生变化。以下是我代表标签布局的代码。

XML文件:

<android.support.design.widget.TabLayout
    android:id="@+id/tlCreateAccount"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:tabIndicatorColor="@android:color/white"
    app:tabIndicatorHeight="3dp"
    app:tabSelectedTextColor="@android:color/white"


    app:tabTextColor="@android:color/white"></android.support.design.widget.TabLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/vpCreateAccount"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"></android.support.v4.view.ViewPager>

KT文件:

class CreateAccountActivity : BaseActivity(), CreateAccountDetailsFragment.OnCheckChanged, ChangeTabs {

lateinit var verificationDetailsFragment: CreateVerificationDetailsFragment
lateinit var accountDetailsFragment: CreateAccountDetailsFragment
lateinit var bankDetailsFragment: CreateBankDetailsFragment

private var account: Account? = null

private lateinit var accountRepo: AccountRepo
private val accountObserverval = Observer<ApiResponse<Account>> { response: ApiResponse<Account>? ->
    updateProgressBar(false)
    if (response != null) {
        if (response.throwable == null) {
            if (response.isResponseOk() && response.responseBody != null) {
                val intent = Intent()
                setResult(RESULT_OK , intent)
                onBackPressed()
            } else {
                val errorBody = response.errorBody
                try {
                    val gson = GsonBuilderHelper.instance.getGson()
                    val accountError = gson.fromJson<ErrorMessage>(errorBody, ErrorMessage::class.java)
                    showErrorSnackBar(this, rootView, accountError.MessageDetail)
                } catch (e: Exception) {
                    ExceptionHelper.displayException(this, rootView, e)
                }
            }
        } else {
            ExceptionHelper.displayException(this, rootView, response.throwable!!)
        }
    }
}


var tabAdapter: TabAdapter? = null

var name = ""

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_create_account)

    updateProgressBar(false)

    val intent: Intent = getIntent()
    account = intent.getSerializableExtra(ConstantHelper.BUNDLE_ACCOUNT_DETAILS) as? Account


    tabAdapter = TabAdapter(supportFragmentManager)


    accountRepo = AccountRepo(this)
    accountRepo.accountLiveData.observe(this, accountObserverval)

    setupViewPager(vpCreateAccount!!)
    tlCreateAccount!!.setupWithViewPager(vpCreateAccount)

    tlCreateAccount.clearOnTabSelectedListeners()

    vpCreateAccount.setOnTouchListener(object : View.OnTouchListener {
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {

            val Page = vpCreateAccount.currentItem

            if (vpCreateAccount.currentItem == Page) {

                vpCreateAccount.setCurrentItem(Page - 1, false)
                vpCreateAccount.setCurrentItem(Page, false)
                return true

            }

            return false
        }
    })

}

inner class TabAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

    private val fragmentList = ArrayList<Fragment>()
    private val fragmentTitleList = ArrayList<String>()


    override fun getItem(position: Int): Fragment {
        return fragmentList.get(position)
    }

    override fun getCount(): Int {
        return fragmentList.size
    }


    fun addFragment(fragment: Fragment, title: String) {
        fragmentList.add(fragment)
        fragmentTitleList.add(title)
    }

    fun removeTabPage(position: Int) {
        if (!fragmentTitleList.isEmpty() && position < fragmentList.size) {
            fragmentList.removeAt(2)
            fragmentList.removeAt(position)
            notifyDataSetChanged()

        }
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return fragmentTitleList.get(position)
    }

    fun getTabView(position: Int): View {
        val view = LayoutInflater.from(this@CreateAccountActivity).inflate(R.layout.layout_tab_view, null)
        view.tvBsTitle.text = fragmentTitleList[position]
        return view

    }


}

private fun setupViewPager(viewPager: ViewPager) {
    accountDetailsFragment = CreateAccountDetailsFragment(this, this , account)
    tabAdapter?.addFragment(accountDetailsFragment, "Create Account")


    viewPager.adapter = tabAdapter
    tabAdapter?.notifyDataSetChanged()

}

}

选项卡不会更改,除非我在我的片段中按下按钮(正确地进行了wotking)。唯一的问题是标签指示器在点击标签时仍然在变化。有没有办法在标签更改时更改指标?

0 个答案:

没有答案