Android с использованием Kotlin - PullRequest
0 голосов
/ 19 марта 2019

Я преобразовал свой существующий Java-проект в kotlin, но получаю ошибку.

Ошибка на OnItemClickListener: - закрытый внутренний класс DrawerItemClickListener (): ListView.OnItemClickListener, AdapterView.OnItemClickListener {

class MainActivity : AppCompatActivity() {

private var mNavigationDrawerItemTitles: Array<String>? = null
    private var mDrawerLayout: DrawerLayout? = null
    private var mDrawerList: ListView? = null
    private var DrawerLinear: LinearLayout? = null
    internal lateinit var toolbar: Toolbar
    private var mDrawerTitle: CharSequence? = null
    private var mTitle: CharSequence? = null
    internal lateinit var mDrawerToggle: android.support.v7.app.ActionBarDrawerToggle
    internal lateinit var menuRight: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mDrawerTitle = title
        mTitle = mDrawerTitle
        mNavigationDrawerItemTitles = resources.getStringArray(R.array.navigation_drawer_items_array)
        mDrawerLayout = findViewById(R.id.drawer_layout)
        mDrawerList = findViewById(R.id.right_drawer)
        DrawerLinear = findViewById(R.id.DrawerLinear)
        menuRight = findViewById(R.id.menuRight)

        val containers = findViewById<ShimmerFrameLayout>(R.id.shimmer_view_container)
        containers.startShimmer()

        val menuLeft = findViewById<ImageView>(R.id.menuLeft)
        menuLeft.visibility = View.GONE

        val fragment = HomeDashboardFragment()
        val ft = supportFragmentManager.beginTransaction()
        ft.replace(R.id.content_frame, fragment)
        ft.commit()

        setupToolbar()

        val drawerItem = arrayOfNulls<DataModel>(10)

        drawerItem[0] = DataModel(R.mipmap.home, "HOME")
        drawerItem[1] = DataModel(R.mipmap.profile, "MY PROFILE")
        drawerItem[2] = DataModel(R.mipmap.my_reward, "MY REWARDS")
        drawerItem[3] = DataModel(R.mipmap.observation, "OBSERVATION")
        drawerItem[4] = DataModel(R.mipmap.learning, "LEARNING")
        drawerItem[5] = DataModel(R.mipmap.medical, "MEDICAL EMERGENCY")
        drawerItem[6] = DataModel(R.mipmap.training_feedback, "TRAINING FEEDBACK")
        drawerItem[7] = DataModel(R.mipmap.employee, "EMPLOYEE ENGAGEMENT")
        drawerItem[8] = DataModel(R.mipmap.contact, "CONTACT")
        drawerItem[9] = DataModel(R.mipmap.logout, "LOGOUT")
        supportActionBar!!.setDisplayHomeAsUpEnabled(false)
        supportActionBar!!.setHomeButtonEnabled(true)

        val adapter = DrawerItemCustomAdapter(this, R.layout.listview_item, drawerItem)
        mDrawerList!!.adapter = adapter
        mDrawerList!!.setOnItemClickListener(DrawerItemClickListener())
        mDrawerLayout = findViewById<View>(R.id.drawer_layout) as DrawerLayout
        mDrawerLayout!!.setDrawerListener(mDrawerToggle)
        setupDrawerToggle()

        menuRight.setOnClickListener {
            if (mDrawerLayout!!.isDrawerOpen(GravityCompat.END)) {
                mDrawerLayout!!.closeDrawer(GravityCompat.END)
            } else {
                mDrawerLayout!!.openDrawer(GravityCompat.END)
            }
        }

    }

    private inner class DrawerItemClickListener() : ListView.OnItemClickListener, AdapterView.OnItemClickListener {//Error here On **OnItemClickListener**

        override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            selectItem(position)
        }

    }

    private fun selectItem(position: Int) {

        var fragment: Fragment? = null

        when (position) {
            0 -> fragment = HomeDashboardFragment()
            1 -> fragment = MyProfileFragment()
            2 -> fragment = MyRewardsFragment()
            3 -> fragment = HomeDashboardFragment()
            4 -> fragment = LearningModuleFragment()
            5 -> fragment = MedicalEmergencyModuleFragment()
            6 -> fragment = EmployeeEngagementFragment()
            7 -> fragment = EmployeeEngagementFragment()
            8 -> fragment = HomeDashboardFragment()
            9 -> fragment = HomeDashboardFragment()

            else -> {
            }
        }

        if (fragment != null) {
            val fragmentManager = supportFragmentManager
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit()

            mDrawerList!!.setItemChecked(position, true)
            mDrawerList!!.setSelection(position)
            setTitle(mNavigationDrawerItemTitles!![position])
            mDrawerLayout!!.closeDrawer(DrawerLinear!!)

        } else {
            Log.e("MainActivity", "Error in creating fragment")
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        return if (mDrawerToggle.onOptionsItemSelected(item)) {
            true
        } else super.onOptionsItemSelected(item)

    }

    override fun setTitle(title: CharSequence) {
        mTitle = title
        supportActionBar!!.setTitle(mTitle)
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        mDrawerToggle.syncState()
    }

    internal fun setupToolbar() {
        toolbar = findViewById<View>(R.id.toolbar) as Toolbar
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayShowHomeEnabled(true)
    }

    internal fun setupDrawerToggle() {
        mDrawerToggle = android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_name)
        //This is necessary to change the icon of the Drawer Toggle upon state change.
        mDrawerToggle.syncState()
    }
}

1 Ответ

0 голосов
/ 19 марта 2019

ListView не имеет OnItemClickListener, так что убери его и пусть только AdapterView.OnItemClickListener

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...