BottomFragment

底部弹出控件 - Fragment 实现

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
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.dench.baselib.R
import com.dench.baselib.databinding.FragmentBottomBinding

class BottomFragment : Fragment() {
companion object {
fun start(fm: FragmentManager, fragment: Fragment): BottomFragment {
val bottomFragment = BottomFragment().apply {
setFragment(fragment)
}
fm.beginTransaction()
.setCustomAnimations(
R.anim.fragment_bottom_enter,
0,
0,
R.anim.fragment_bottom_exit
)
.add(android.R.id.content, bottomFragment)
.addToBackStack(null)
.commitAllowingStateLoss()
return bottomFragment
}
}

private lateinit var fragment: Fragment

private fun setFragment(fragment: Fragment) {
this.fragment = fragment
}

private lateinit var binding: FragmentBottomBinding
private val TAG = "BottomFragment"

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentBottomBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.run {
binding.bottomFragmentRl.setOnClickListener(View.OnClickListener {
Log.d(TAG, "bottom root view click.")
dismissSelf()
})

/** add fragment */
childFragmentManager.beginTransaction()
.add(R.id.container, fragment)
.commitAllowingStateLoss()
}
}

private fun dismissSelf() {
parentFragmentManager.popBackStack()
}
}
作者

Dench

发布于

2021-05-19

更新于

2021-05-19

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×