iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 7
0
自我挑戰組

Android的學習歷程系列 第 7

[Day 7] Android程式設計-使用OpenData資料製作app(五)(kotlin)

  • 分享至 

  • xImage
  •  

今天又要回到專案的部分,完成了頁面間的切換,接下來要來做返回的功能,我們要使用手機內建的返回功能鍵來做頁面的返回,這個按鍵在觸發時會運行onBackPressed()的方法。

首先來做幾個抽象類別,一樣先創建兩個新檔案。
https://ithelp.ithome.com.tw/upload/images/20190923/20121080gTxrHrycZ0.png

BaseFragment:

abstract class BaseFragment : Fragment() {
    private var mActivity: BaseActivity? = null

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        if (context is BaseActivity) {
            this.mActivity = context
        }
    }

    override fun onDetach() {
        mActivity = null
        super.onDetach()
    }

    interface Callback {
        fun onFragmentDetached(arg: Bundle?, vararg tag: String)
    }
}

BaseActivity:

abstract class BaseActivity : AppCompatActivity(), BaseFragment.Callback {

    override fun onFragmentDetached(arg: Bundle?, vararg tag: String) {

    }
}

接下來讓MainActivity來繼承上面的BaseActivity,我們需要在onFragmentDetached中做切換片段的設定。
MainActivity:

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        CommonUtils.TransFragment(this, R.id.main_container, MainFragment(), MainFragment().TAG, null)
    }

    override fun onFragmentDetached(arg: Bundle?, vararg tag: String) {
        super.onFragmentDetached(arg, *tag)
        val fragmentManager = supportFragmentManager
        val fragment: Fragment = fragmentManager.findFragmentByTag(tag[0])!!
        fragmentManager
                .beginTransaction()
                .disallowAddToBackStack()
                .remove(fragment)
                .commitNow()
    }

    //返回頁面
    override fun onBackPressed() {
        val fragmentManager = supportFragmentManager
        when {
            null != fragmentManager.findFragmentByTag(NoteFragment().TAG) -> onFragmentDetached(null, NoteFragment().TAG)
            null != fragmentManager.findFragmentByTag(SelectFragment().TAG) -> onFragmentDetached(null, SelectFragment().TAG)
            else -> super.onBackPressed()
        }
    }
}

如次便能夠統一在MainActivity下來完成返回的功能,未來有新增的fragment依照上面的onBackPressed內容做新增便能夠完成切換的工作,下面統整其他前面所做的內容。

NoteFragment:

class NoteFragment : Fragment() {

    companion object val TAG = NoteFragment::class.java.simpleName

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_note,container,false)

    }
}

SelectFragment:

class SelectFragment : Fragment() {

    companion object

    val TAG = SelectFragment::class.java.simpleName

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_select, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }
}

CommonUtils:

object CommonUtils {

    fun TransFragment(context: Context, id: Int, fragment: Fragment?, tag: String, bundle: Bundle?) {
        if (fragment == null) return
        fragment.arguments = bundle
        val appCompatActivity = context as AppCompatActivity

        appCompatActivity.supportFragmentManager
                .beginTransaction()
                .disallowAddToBackStack()
                .add(id, fragment, tag)
                .commit()
    }

}

MainFragment:

class MainFragment : Fragment() {

    companion object

    val TAG = MainFragment::class.java.simpleName

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    private fun initViews() {
        select.setOnClickListener {
            CommonUtils.TransFragment(context!!, R.id.main_container, SelectFragment(), SelectFragment().TAG, null)
        }
        note.setOnClickListener {
            CommonUtils.TransFragment(context!!, R.id.main_container, NoteFragment(), NoteFragment().TAG, null)
        }
    }
}

最後的package內容
https://ithelp.ithome.com.tw/upload/images/20190923/20121080H6Arz65Gfv.png


上一篇
[Day 6] Android-Fragment的基本使用方法
下一篇
[Day 8] 簡單說明xml文件
系列文
Android的學習歷程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言