riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cn.flightfeather.thirdapp.module.base
 
 
import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.Toolbar
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import cn.flightfeather.thirdapp.R
import kotlinx.android.synthetic.main.tool_bar_layout.*
import org.greenrobot.eventbus.EventBus
 
/**
 * @author riku
 * 2019.7.29
 * 基类fragment
 */
abstract class BaseFragment : Fragment(), ToolbarSetInterface {
 
    var mContext: Context? = null
 
    var mRootView: ViewGroup? = null
 
    private lateinit var contentView: View
 
    override fun getToolBar(): Toolbar = contentView.findViewById(R.id.tool_bar)
 
    override fun toolbarEnabled(): Boolean =false
 
    override fun getHeadIcon(): View = contentView.findViewById(R.id.img_back)
 
    override fun onHeadIconClick() { activity?.onBackPressed() }
 
    override fun getMyTitleTextView(): TextView? = contentView.findViewById(R.id.tv_main_title)
 
    override fun getTitleEnabled(): Boolean = true
 
    override fun getTitleText(): String? = ""
 
    override fun getMenus(): List<View>? = listOf(contentView.findViewById(R.id.img_menu_1), contentView.findViewById(R.id.img_menu_2), contentView.findViewById(R.id.img_menu_3))
 
    override fun getMenuVisibility(): List<Int?> = listOf(View.VISIBLE, View.VISIBLE, View.VISIBLE)
 
    override fun getMenuRes(): List<Drawable?> = listOf(null)
 
    override fun getMenuClickListener(): List<View.OnClickListener?> = listOf(null)
 
    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        mRootView = container
        contentView = inflater.inflate(getLayoutId(), container, false)
        onCreateView()
        initToolbar()
        return contentView
    }
 
    override fun onAttach(context: Context?) {
        super.onAttach(context)
        mContext = context
    }
 
    override fun onAttach(activity: Activity?) {
        super.onAttach(activity)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            mContext = context
        }
    }
 
    override fun onDestroyView() {
        super.onDestroyView()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
 
    open fun onBackPressed(onActivityBack: () -> Unit) {
        onActivityBack()
    }
 
    open fun onCreateView() = Unit
 
    abstract fun getLayoutId(): Int
 
    /**
     * 拓展fragment中隐藏软键盘的方法
     */
    fun Fragment.hideKeyboard() {
        this.activity?.window?.peekDecorView()?.let {
            val inputManager =
                    this.activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.hideSoftInputFromWindow(it.windowToken, 0)
        }
    }
 
}