-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shuangshuang.qin
committed
Aug 18, 2021
1 parent
0ca395a
commit 1f1cfb2
Showing
7 changed files
with
670 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
customview/src/main/java/com/aranandroid/customview/fragment/FragmentBottom.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.aranandroid.customview.fragment | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.graphics.Canvas | ||
import android.util.AttributeSet | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.LinearLayout | ||
import android.widget.RadioButton | ||
import android.widget.RadioGroup | ||
import androidx.core.view.children | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.fragment.app.FragmentPagerAdapter | ||
import androidx.viewpager.widget.ViewPager | ||
import androidx.viewpager.widget.ViewPager.OnPageChangeListener | ||
import com.aranandroid.customview.R | ||
|
||
class FragmentBottom(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) { | ||
|
||
var view:View | ||
|
||
var radio:RadioGroup | ||
|
||
var viewPager: ViewPager | ||
|
||
var fragments: LinkedHashMap<Int,Fragment>? = null | ||
set(value) { | ||
field = value | ||
radio.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId -> | ||
fragments?.keys?.let { | ||
for ((i, key) in it.withIndex()) { | ||
if(checkedId == key){ | ||
viewPager.setCurrentItem(i,true) | ||
} | ||
} | ||
} | ||
}) | ||
val fm: FragmentManager = (context as FragmentActivity).getSupportFragmentManager() | ||
val myFragmentPagerAdapter = | ||
fragments?.values?.toList()?.let { MyFragmentPagerAdapter(fm, it) } //new myFragmentPagerAdater记得带上两个参数 | ||
viewPager.adapter = myFragmentPagerAdapter | ||
viewPager.offscreenPageLimit = 3 | ||
viewPager.addOnPageChangeListener(object : OnPageChangeListener{ | ||
override fun onPageScrollStateChanged(state: Int) { | ||
} | ||
|
||
override fun onPageScrolled( | ||
position: Int, | ||
positionOffset: Float, | ||
positionoffsetpixels: Int | ||
) { | ||
} | ||
|
||
override fun onPageSelected(position: Int) { | ||
fragments?.keys?.let { | ||
for ((i, key) in it.withIndex()) { | ||
if(position == i){ | ||
radio.findViewById<RadioButton>(key).performClick() | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
init { | ||
view = LayoutInflater.from(context).inflate(R.layout.fragment_bottom, this, true) | ||
radio = view.findViewById(R.id.radio) | ||
viewPager = view.findViewById(R.id.pager) | ||
} | ||
|
||
constructor(context: Context?):this(context,null){ | ||
|
||
} | ||
|
||
override fun dispatchDraw(canvas: Canvas?) { | ||
|
||
super.dispatchDraw(canvas) | ||
// addChildrenForAccessibility() | ||
val children = children | ||
for (child in children) { | ||
if(child.id != R.id.fragment_top){ | ||
(child.parent as ViewGroup).removeView(child) | ||
radio.addView(child) | ||
} | ||
} | ||
} | ||
|
||
|
||
class MyFragmentPagerAdapter( | ||
fm: FragmentManager?, | ||
list: List<Fragment> | ||
) : | ||
FragmentPagerAdapter(fm!!,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { | ||
private val listfragment //创建一个List<Fragment> | ||
: List<Fragment> | ||
|
||
override fun getItem(arg0: Int): Fragment { | ||
return listfragment[arg0] //返回第几个fragment | ||
} | ||
|
||
override fun getCount(): Int { | ||
return listfragment.size //总共有多少个fragment | ||
} | ||
|
||
// 定义构造带两个参数 | ||
init { | ||
listfragment = list | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/fragment_top" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<RadioGroup | ||
android:id="@+id/radio" | ||
android:layout_width="match_parent" | ||
android:layout_height="50dp" | ||
android:layout_alignParentBottom="true" | ||
android:gravity="center_vertical" | ||
android:orientation="horizontal" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
tools:ignore="MissingConstraints"></RadioGroup> | ||
|
||
<androidx.viewpager.widget.ViewPager | ||
android:id="@+id/pager" | ||
android:layout_width="match_parent" | ||
android:layout_height="0dp" | ||
app:layout_constraintBottom_toTopOf="@id/radio" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"/> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |