Skip to content

Commit

Permalink
底部导航切换fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
shuangshuang.qin committed Aug 18, 2021
1 parent 0ca395a commit 1f1cfb2
Show file tree
Hide file tree
Showing 7 changed files with 670 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

458 changes: 458 additions & 0 deletions .idea/dbnavigator.xml

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_square_moreview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
>
</com.aranandroid.customview.squareview.SquareTextImage>

<com.aranandroid.customview.fragment.FragmentTop
<com.aranandroid.customview.fragment.FragmentBottom
android:id="@+id/fragment_top"
android:layout_width="match_parent"
android:layout_height="500dp">
Expand Down Expand Up @@ -131,7 +131,7 @@
android:text="H"
tools:ignore="MissingConstraints" />
</com.aranandroid.customview.squareview.SquareLinearLayout>
</com.aranandroid.customview.fragment.FragmentTop>
</com.aranandroid.customview.fragment.FragmentBottom>
</LinearLayout>
</ScrollView>
</com.aranandroid.customview.title.TitleLayout>
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
}
}

}
28 changes: 28 additions & 0 deletions customview/src/main/res/layout/fragment_bottom.xml
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>

0 comments on commit 1f1cfb2

Please sign in to comment.