Skip to content

Commit 7eed14f

Browse files
Merge remote-tracking branch 'origin/main' into main
2 parents 7824bd1 + 9a92ddd commit 7eed14f

File tree

8 files changed

+703
-3
lines changed

8 files changed

+703
-3
lines changed

.idea/codeStyles/Project.xml

Lines changed: 48 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/java/com/aranandroid/customview/ui/SquareMoreActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
44
import android.app.Activity
55
import android.os.Bundle
66
import android.os.PersistableBundle
7+
import android.util.Log
78
import android.widget.RadioButton
89
import android.widget.Toast
910
import androidx.appcompat.app.AppCompatActivity
@@ -30,7 +31,9 @@ class SquareMoreActivity : FragmentActivity() {
3031
Toast.makeText(this ,"click",Toast.LENGTH_SHORT).show()
3132
}
3233
fragment_top.fragments = linkedMapOf(Pair(R.id.f, AFragment()),Pair(R.id.h, BFragment()))
33-
34+
fragment_top.changeItme={group, checkedId ->
35+
Log.e("TAG", "onCreate: "+checkedId )
36+
}
3437
}
3538

3639

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: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
31+
var changeItme : (group: RadioGroup, checkedId: Int) -> Unit = {group, checkedId -> }
32+
33+
var fragments: LinkedHashMap<Int, Fragment>? = null
34+
set(value) {
35+
field = value
36+
radio.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
37+
fragments?.keys?.let {
38+
for ((i, key) in it.withIndex()) {
39+
if (checkedId == key) {
40+
viewPager.setCurrentItem(i, true)
41+
}
42+
}
43+
}
44+
changeItme(group,checkedId)
45+
})
46+
val fm: FragmentManager = (context as FragmentActivity).getSupportFragmentManager()
47+
val myFragmentPagerAdapter =
48+
fragments?.values?.toList()
49+
?.let { MyFragmentPagerAdapter(fm, it) } //new myFragmentPagerAdater记得带上两个参数
50+
viewPager.adapter = myFragmentPagerAdapter
51+
viewPager.offscreenPageLimit = 3
52+
viewPager.addOnPageChangeListener(object : OnPageChangeListener {
53+
override fun onPageScrollStateChanged(state: Int) {
54+
}
55+
56+
override fun onPageScrolled(
57+
position: Int,
58+
positionOffset: Float,
59+
positionoffsetpixels: Int
60+
) {
61+
}
62+
63+
override fun onPageSelected(position: Int) {
64+
fragments?.keys?.let {
65+
for ((i, key) in it.withIndex()) {
66+
if (position == i) {
67+
radio.findViewById<RadioButton>(key).performClick()
68+
}
69+
}
70+
}
71+
}
72+
})
73+
}
74+
75+
init {
76+
view = LayoutInflater.from(context).inflate(R.layout.fragment_bottom, this, true)
77+
radio = view.findViewById(R.id.radio)
78+
viewPager = view.findViewById(R.id.pager)
79+
}
80+
81+
constructor(context: Context?) : this(context, null) {
82+
83+
}
84+
85+
override fun dispatchDraw(canvas: Canvas?) {
86+
87+
super.dispatchDraw(canvas)
88+
// addChildrenForAccessibility()
89+
val children = children
90+
for (child in children) {
91+
if (child.id != R.id.fragment_top) {
92+
(child.parent as ViewGroup).removeView(child)
93+
radio.addView(child)
94+
}
95+
}
96+
}
97+
98+
99+
class MyFragmentPagerAdapter(
100+
fm: FragmentManager?,
101+
list: List<Fragment>
102+
) :
103+
FragmentPagerAdapter(fm!!, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
104+
private val listfragment //创建一个List<Fragment>
105+
: List<Fragment>
106+
107+
override fun getItem(arg0: Int): Fragment {
108+
return listfragment[arg0] //返回第几个fragment
109+
}
110+
111+
override fun getCount(): Int {
112+
return listfragment.size //总共有多少个fragment
113+
}
114+
115+
// 定义构造带两个参数
116+
init {
117+
listfragment = list
118+
}
119+
}
120+
121+
}
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)