当前位置: 首页 > wzjs >正文

江西网络推广外包seo网站管理

江西网络推广外包,seo网站管理,网站有时打不开,网站制作教程网站文章目录 Android 自定义View之底部导航栏概述代码定义TabIndex定义Tab定义TabView定义NavigationBarFragmentSwitchHelper管理类使用 源码下载 Android 自定义View之底部导航栏 概述 封装一个通用的底部导航栏控件。 代码 定义TabIndex Retention(AnnotationRetention.SOU…

文章目录

  • Android 自定义View之底部导航栏
    • 概述
    • 代码
      • 定义TabIndex
      • 定义Tab
      • 定义TabView
      • 定义NavigationBar
      • FragmentSwitchHelper管理类
      • 使用
    • 源码下载

Android 自定义View之底部导航栏

概述

封装一个通用的底部导航栏控件。

在这里插入图片描述

代码

定义TabIndex

@Retention(AnnotationRetention.SOURCE)
@IntDef(ONE_INDEX, TWO_INDEX, THREE_INDEX, FOUR_INDEX)
annotation class TabIndex {companion object {const val ONE_INDEX = 0const val TWO_INDEX = 1const val THREE_INDEX = 2const val FOUR_INDEX = 3}
}

定义Tab

data class Tab(@TabIndex val index: Int,val label: String,@DrawableRes val tabIconDefault: Int,@DrawableRes val tabIconSelected: Int,@ColorRes val tabTextColorDefault: Int = R.color.tab_unselect_color,@ColorRes val tabTextColorSelected: Int = R.color.tab_selected_color
)

定义TabView

class TabView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {private val paddingVertical = context.resources.getDimension(R.dimen.tab_padding_vertical).toInt()private val iconSize = context.resources.getDimension(R.dimen.tab_icon_size).toInt()private val fontSize = context.resources.getDimension(R.dimen.tab_text_size)init {layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT).apply {weight = 1Forientation = VERTICAL}gravity = Gravity.CENTERsetPadding(0, paddingVertical, 0, paddingVertical)}fun setData(tab: Tab) {removeAllViews()addViews(tab)}private fun addViews(tab: Tab) {addView(createIcon(tab.tabIconDefault, tab.tabIconSelected))addView(createText(tab.label, tab.tabTextColorDefault, tab.tabTextColorSelected))}private fun createIcon(@DrawableRes tabIconDefault: Int,@DrawableRes tabIconSelected: Int): ImageView {val drawable = StateListDrawable().apply {addState(intArrayOf(android.R.attr.state_selected),ContextCompat.getDrawable(context, tabIconSelected))addState(StateSet.NOTHING, ContextCompat.getDrawable(context, tabIconDefault))}return ImageView(context).apply {layoutParams = LayoutParams(iconSize, iconSize)setImageDrawable(drawable)isSelected = false}}private fun createText(text: String,@ColorRes textColorDefault: Int,@ColorRes textColorSelected: Int): TextView {val states = arrayOf(intArrayOf(android.R.attr.state_selected),intArrayOf())val colors = intArrayOf(ContextCompat.getColor(context, textColorSelected),ContextCompat.getColor(context, textColorDefault))val colorStateList = ColorStateList(states, colors)return TextView(context).apply {layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)setText(text)setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)setTextColor(colorStateList)isSelected = false}}fun selected(isSelected: Boolean) {children.forEach {it.isSelected = isSelected}}
}

定义NavigationBar

class NavigationBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), View.OnClickListener {private val lineView by lazy { createLine() }private val containerView by lazy { createContainer() }private var onItemSelectedListener: ((Int) -> Unit)? = nullprivate var onItemReselectListener: ((Int) -> Unit)? = nullprivate var currentIndex: Int = ONE_INDEXinit {addView(containerView, 0)addView(lineView, 1)}fun setData(tabs: List<Tab>, defaultIndex: Int = ONE_INDEX) {containerView.removeAllViews()addViews(tabs)currentIndex = defaultIndex(containerView.getChildAt(currentIndex) as TabView).selected(true)}private fun addViews(tabs: List<Tab>) {tabs.let {for (tab in it) {containerView.addView(createTabView(tab))}}}private fun createTabView(tab: Tab): TabView {return TabView(context).apply {tag = tab.indexsetData(tab)setOnClickListener(this@NavigationBar)}}private fun createContainer(): LinearLayout {return LinearLayout(context).apply {layoutParams =LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT).apply {orientation = HORIZONTAL}gravity = Gravity.CENTER}}private fun createLine(): View {return View(context).apply {setBackgroundColor(ContextCompat.getColor(context, R.color.tab_line))layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 1.dp, Gravity.TOP)}}override fun onClick(view: View) {if (view is TabView) {clickTabView(view)}}private var lock = falseprivate fun clickTabView(view: View) {if (lock) {return}lock = trueval selectIndex = view.tag as Intif (selectIndex == currentIndex) {onItemReselectListener?.invoke(selectIndex)} else {(containerView.getChildAt(currentIndex) as TabView).selected(false)(containerView.getChildAt(selectIndex) as TabView).selected(true)onItemSelectedListener?.invoke(selectIndex)currentIndex = selectIndex}lock = false}fun setOnItemSelectedListener(onItemSelectedListener: ((Int) -> Unit)) {this.onItemSelectedListener = onItemSelectedListener}fun setOnItemReselectListener(onItemReselectListener: ((Int) -> Unit)) {this.onItemReselectListener = onItemReselectListener}}

FragmentSwitchHelper管理类

class FragmentSwitchHelper(private val fragmentManager: FragmentManager,@IdRes private val containerId: Int
) {private var currentFragment: Fragment? = nullprivate var currentIndex: Int? = nullfun switchTo(@TabIndex index: Int) {if (currentIndex == index) {return}val transaction = fragmentManager.beginTransaction()currentFragment?.let {transaction.hide(it)}val fragment = Factory.getOrCreateFragment(index, fragmentManager)if (fragment.isAdded) {transaction.show(fragment)} else {transaction.add(containerId,fragment,index.toString())}transaction.commit()currentIndex = indexcurrentFragment = fragment}fun getCurrentIndex() = currentIndexfun getCurrentFragment() = currentFragmentobject Factory {fun getOrCreateFragment(@TabIndex index: Int, fragmentManager: FragmentManager) =when (index) {ONE_INDEX -> fragmentManager.findFragmentByTag(index.toString())?: SimpleFragment.newInstance("ONE")TWO_INDEX -> fragmentManager.findFragmentByTag(index.toString())?: SimpleFragment.newInstance("TWO")THREE_INDEX -> fragmentManager.findFragmentByTag(index.toString())?: SimpleFragment.newInstance("THREE")FOUR_INDEX -> fragmentManager.findFragmentByTag(index.toString())?: SimpleFragment.newInstance("FOUR")else -> throw IllegalStateException("非法参数")}}
}

使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".navigationbar.NavigationBarActivity"><androidx.fragment.app.FragmentContainerViewandroid:id="@+id/fragment_container"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><com.example.widgets.navigationbar.NavigationBarandroid:id="@+id/navigation_bar"android:layout_width="match_parent"android:layout_height="wrap_content" />
</LinearLayout>
class NavigationBarActivity : BaseActivity() {private lateinit var navigationBar: NavigationBarprivate val fragmentHelper by lazy {FragmentSwitchHelper(supportFragmentManager,R.id.fragment_container)}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_navigation_bar)navigationBar = findViewById(R.id.navigation_bar)val tabs = listOf<Tab>(Tab(ONE_INDEX,"ONE",R.drawable.tab_home_unselect,R.drawable.tab_home_selected),Tab(TWO_INDEX,"TWO",R.drawable.tab_friends_unselect,R.drawable.tab_friends_selected),Tab(THREE_INDEX,"THREE",R.drawable.tab_find_unselect,R.drawable.tab_find_selected),Tab(FOUR_INDEX,"FOUR",R.drawable.tab_setting_unselect,R.drawable.tab_setting_selected))navigationBar.setData(tabs)navigationBar.setOnItemSelectedListener {Log.e("TAG", "点击:$it")fragmentHelper.switchTo(it)}navigationBar.setOnItemReselectListener {Log.e("TAG", "重复点击:$it")}fragmentHelper.switchTo(ONE_INDEX)}
}

源码下载

http://www.dtcms.com/wzjs/347559.html

相关文章:

  • 网站开发方向的工作百度网盘网页版登录入口
  • 超市网站建设武汉seo顾问
  • 武汉网站建设十强企业目前最火的推广平台
  • 眉山网站建设网站视频
  • 网站被k什么意思万网阿里云域名查询
  • 网站建设工作基本流程互联网广告投放代理公司
  • 2015wordpress相册插件济南seo优化公司助力排名
  • 企业网站建设思路四川seo优化
  • 县城做二手车网站seo网站优化建议
  • 东莞抖音推广合作潍坊网站seo
  • 有没有做家居服设计师看的网站关键词排名优化系统
  • 企业形象成品网站最好看免费观看高清视频了
  • 东莞樟木头做网站哪家好windows优化大师官网
  • 动画设计就业前景优化营商环境个人心得
  • 淘宝上做网站权重磁力宝最佳搜索引擎入口
  • 网站搜索引擎优化怎么做建设企业营销型网站
  • 做网站需要服务器么苏州seo按天扣费
  • 企业cms建站站长之家网站排行榜
  • go 语言 做网站谷歌浏览器下载安装2021最新版
  • 网页浏览器是windows系统自带的是资源网站优化排名软件
  • 设计公司logo网站徐州百度seo排名优化
  • 坐什么网站能用到html5百度广告怎么收费标准
  • 做网站用虚拟服务器可以吗云速seo百度点击
  • 广东微信网站制作价格百度seo和谷歌seo有什么区别
  • 久霸高端网页版网站首页关键词如何优化
  • 深圳美食教学网站制作网站推广的方式有哪些
  • 涉县手机网站建设营销型网站建设实训总结
  • 怎么做网站上翻译泰剧企业网络规划与设计
  • 一万元做网站自助建站申请
  • 做设计的网站定制放单平台