Skip to content

Commit 1f1cfb2

Browse files
author
shuangshuang.qin
committed
底部导航切换fragment
1 parent 0ca395a commit 1f1cfb2

File tree

7 files changed

+670
-2
lines changed

7 files changed

+670
-2
lines changed

.idea/codeStyles/Project.xml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dbnavigator.xml

Lines changed: 458 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/res/layout/activity_square_moreview.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
>
9595
</com.aranandroid.customview.squareview.SquareTextImage>
9696

97-
<com.aranandroid.customview.fragment.FragmentTop
97+
<com.aranandroid.customview.fragment.FragmentBottom
9898
android:id="@+id/fragment_top"
9999
android:layout_width="match_parent"
100100
android:layout_height="500dp">
@@ -131,7 +131,7 @@
131131
android:text="H"
132132
tools:ignore="MissingConstraints" />
133133
</com.aranandroid.customview.squareview.SquareLinearLayout>
134-
</com.aranandroid.customview.fragment.FragmentTop>
134+
</com.aranandroid.customview.fragment.FragmentBottom>
135135
</LinearLayout>
136136
</ScrollView>
137137
</com.aranandroid.customview.title.TitleLayout>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.aranandroid.customview.fragment
2+
3+
import android.app.Activity
4+
import android.content.Context
5+
import android.graphics.Canvas
6+
import android.util.AttributeSet
7+
import android.view.LayoutInflater
8+
import android.view.View
9+
import android.view.ViewGroup
10+
import android.widget.LinearLayout
11+
import android.widget.RadioButton
12+
import android.widget.RadioGroup
13+
import androidx.core.view.children
14+
import androidx.fragment.app.Fragment
15+
import androidx.fragment.app.FragmentActivity
16+
import androidx.fragment.app.FragmentManager
17+
import androidx.fragment.app.FragmentPagerAdapter
18+
import androidx.viewpager.widget.ViewPager
19+
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
20+
import com.aranandroid.customview.R
21+
22+
class FragmentBottom(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
23+
24+
var view:View
25+
26+
var radio:RadioGroup
27+
28+
var viewPager: ViewPager
29+
30+
var fragments: LinkedHashMap<Int,Fragment>? = null
31+
set(value) {
32+
field = value
33+
radio.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
34+
fragments?.keys?.let {
35+
for ((i, key) in it.withIndex()) {
36+
if(checkedId == key){
37+
viewPager.setCurrentItem(i,true)
38+
}
39+
}
40+
}
41+
})
42+
val fm: FragmentManager = (context as FragmentActivity).getSupportFragmentManager()
43+
val myFragmentPagerAdapter =
44+
fragments?.values?.toList()?.let { MyFragmentPagerAdapter(fm, it) } //new myFragmentPagerAdater记得带上两个参数
45+
viewPager.adapter = myFragmentPagerAdapter
46+
viewPager.offscreenPageLimit = 3
47+
viewPager.addOnPageChangeListener(object : OnPageChangeListener{
48+
override fun onPageScrollStateChanged(state: Int) {
49+
}
50+
51+
override fun onPageScrolled(
52+
position: Int,
53+
positionOffset: Float,
54+
positionoffsetpixels: Int
55+
) {
56+
}
57+
58+
override fun onPageSelected(position: Int) {
59+
fragments?.keys?.let {
60+
for ((i, key) in it.withIndex()) {
61+
if(position == i){
62+
radio.findViewById<RadioButton>(key).performClick()
63+
}
64+
}
65+
}
66+
}
67+
})
68+
}
69+
70+
init {
71+
view = LayoutInflater.from(context).inflate(R.layout.fragment_bottom, this, true)
72+
radio = view.findViewById(R.id.radio)
73+
viewPager = view.findViewById(R.id.pager)
74+
}
75+
76+
constructor(context: Context?):this(context,null){
77+
78+
}
79+
80+
override fun dispatchDraw(canvas: Canvas?) {
81+
82+
super.dispatchDraw(canvas)
83+
// addChildrenForAccessibility()
84+
val children = children
85+
for (child in children) {
86+
if(child.id != R.id.fragment_top){
87+
(child.parent as ViewGroup).removeView(child)
88+
radio.addView(child)
89+
}
90+
}
91+
}
92+
93+
94+
class MyFragmentPagerAdapter(
95+
fm: FragmentManager?,
96+
list: List<Fragment>
97+
) :
98+
FragmentPagerAdapter(fm!!,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
99+
private val listfragment //创建一个List<Fragment>
100+
: List<Fragment>
101+
102+
override fun getItem(arg0: Int): Fragment {
103+
return listfragment[arg0] //返回第几个fragment
104+
}
105+
106+
override fun getCount(): Int {
107+
return listfragment.size //总共有多少个fragment
108+
}
109+
110+
// 定义构造带两个参数
111+
init {
112+
listfragment = list
113+
}
114+
}
115+
116+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/fragment_top"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent">
8+
9+
<RadioGroup
10+
android:id="@+id/radio"
11+
android:layout_width="match_parent"
12+
android:layout_height="50dp"
13+
android:layout_alignParentBottom="true"
14+
android:gravity="center_vertical"
15+
android:orientation="horizontal"
16+
app:layout_constraintBottom_toBottomOf="parent"
17+
tools:ignore="MissingConstraints"></RadioGroup>
18+
19+
<androidx.viewpager.widget.ViewPager
20+
android:id="@+id/pager"
21+
android:layout_width="match_parent"
22+
android:layout_height="0dp"
23+
app:layout_constraintBottom_toTopOf="@id/radio"
24+
app:layout_constraintLeft_toLeftOf="parent"
25+
app:layout_constraintRight_toRightOf="parent"
26+
app:layout_constraintTop_toTopOf="parent"/>
27+
28+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)