问题描述
我创建了一个继承 appbarlayout 的组件,当该组件添加到活动 xml 布局文件时,OnOffsetChangedListener 不起作用。 当我将组件布局文件直接放入活动的 xml 布局文件时,代码就可以工作了。我想了解为什么组件不设置侦听器但活动设置了。我想要标题 appbarLayout 在向下滚动时隐藏并在顶部滚动时显示。
xml 布局组件
<android.support.design.widget.AppBarLayout android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/component_light_white"
app:elevation="0dp"
xmlns:tools="http://schemas.android.com/tools">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/component_custom_collapsing_appbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
app:collapsedTitleGravity="start"
app:collapsedTitleTextAppearance="@style/component.CustomToolbar.Title.Collapsed"
app:expandedTitleGravity="start|bottom"
app:expandedTitleMarginBottom="0dp"
app:expandedTitleMarginStart="0dp"
app:expandedTitleMarginTop="10dp"
app:expandedTitleTextAppearance="@style/component.CustomToolbar.Title.Expanded"
app:scrimAnimationDuration="600">
<LinearLayout
android:id="@+id/component_custom_appbar_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:gravity="start|bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/component_padding_medium"
android:paddingRight="@dimen/component_padding_medium"
app:layout_collapseMode="parallax"
>
<TextView
android:id="@+id/component_custom_appbar_title"
style="@style/component.CustomToolbar.FontRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:fontFamily="@font/component_text"
android:importantForAccessibility="yes"
android:textColor="000"
app:layout_collapseMode="parallax"
android:textSize="14sp" />
<TextView
android:id="@+id/component_custom_appbar_subtitle"
style="@style/component.CustomToolbar.FontBold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:textColor="000"
android:textSize="18sp"
app:layout_collapseMode="parallax"
/>
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/component_custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enteralways"
android:focusableInTouchMode="true"
app:elevation="0dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<TextView
android:id="@+id/component_custom_toolbar_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="color"
android:textSize="14sp"
/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
组件代码
class ComponentCustomToolbar: AppBarLayout {
lateinit var toolbar: Toolbar
private lateinit var txtTitletoolbarCollapsed: TextView
private lateinit var collapsingToolbarLayout: CollapsingToolbarLayout
private lateinit var txtTitleAppBar: TextView
private lateinit var txtSubtitleAppBar: TextView
private lateinit var viewContainer : LinearLayout
private lateinit var listener: OnOffsetChangedListener
private var appbarTitle = ""
private var appBarSubtitle = ""
var isTitleCentered = false
var disableCollapsing = false
var appBarExpanded = true
private val test = 1
private lateinit var layout: View
constructor(context: Context) : super(context) {
initView()
}
constructor(context: Context,attributeSet: AttributeSet?) : super(context,attributeSet) {
initView()
}
private fun initView() {
//val layout = View.inflate(context,R.layout.component_custom_toolbar,parent as ViewGroup)
layout = LayoutInflater.from(context).inflate(R.layout.component_custom_toolbar,null)
txtTitleAppBar = layout.findViewById(R.id.component_custom_appbar_title)
txtSubtitleAppBar = layout.findViewById(R.id.component_custom_appbar_subtitle)
toolbar = layout.findViewById(R.id.component_custom_toolbar)
txtTitletoolbarCollapsed = layout.findViewById(R.id.component_custom_toolbar_title)
collapsingToolbarLayout = layout.findViewById(R.id.component_custom_collapsing_appbar)
viewContainer = layout.findViewById(R.id.component_custom_appbar_container)
initListener()
setupAppBar()
}
private fun initListener() {
listener = object : OnOffsetChangedListener {
var isShow = false
var scrollRange = -1
override fun onOffsetChanged(appBarLayout: AppBarLayout,verticalOffset: Int) {
val maxScroll = appBarLayout.totalScrollRange
val percentage = abs(verticalOffset).toFloat() / maxScroll.toFloat()
val holderAlpha = 1f - percentage
viewContainer.alpha = holderAlpha
if (scrollRange == -1) {
scrollRange = appBarLayout.totalScrollRange
}
if (scrollRange + verticalOffset == 0) {
if (collapsingToolbarLayout.title.isNullOrEmpty() &&
txtTitletoolbarCollapsed?.text.isNullOrEmpty()) {
collapsingToolbarLayout.title = appbarTitle
txtTitletoolbarCollapsed?.text = appbarTitle
}
isShow = true
} else if (isShow) {
collapsingToolbarLayout.title = ""
txtTitletoolbarCollapsed?.text = null
setToolbarTitle()
isShow = false
}
}
}
}
fun initCustomToolbar(title: String,subtitle: String,test: Int = 1) {
appbarTitle = title
appBarSubtitle = subtitle
setupToolbar(test)
setToolbarTitle()
(parent as ViewGroup).addView(layout)
}
private fun setupToolbar(test: Int) {
val backArrow = ContextCompat.getDrawable(context,R.drawable.component_ic)
val color = if (test!= test) {
ContextCompat.getColor(context,R.color.component_color)
} else {
ContextCompat.getColor(context,R.color.component_color)
}
backArrow?.setColorFilter(color,PorterDuff.Mode.SRC_ATOP)
toolbar.navigationIcon = backArrow
}
private fun setupAppBar() {
if (!disableCollapsing) {
addOnOffsetChangedListener(listener)
}
setupExpanded()
setupCollapsing()
}
private fun setToolbarTitle() {
if (!TextUtils.isEmpty(appbarTitle)) {
txtTitleAppBar.text = appbarTitle
}
if (!TextUtils.isEmpty(appBarSubtitle)) {
txtSubtitleAppBar.text = appBarSubtitle
}
}
private fun setupExpanded() {
setExpanded(appBarExpanded)
viewContainer.visibility = if (appBarExpanded) View.VISIBLE else View.GONE
}
private fun setupCollapsing() {
if (disableCollapsing) {
collapsingToolbarLayout.disableScroll()
}
if (isTitleCentered) {
collapsingToolbarLayout.isTitleEnabled = false
txtTitletoolbarCollapsed?.visibility = View.VISIBLE
} else {
collapsingToolbarLayout.isTitleEnabled = true
txtTitletoolbarCollapsed?.visibility = View.GONE
}
}
活动代码
class CustomToolbaractivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_component_custom_toolbar)
val componentCustomToolbar = findViewById<ComponentCustomToolbar>(R.id.component_custom_app_bar)
componentCustomToolbar.initCustomToolbar("myTitle","MySubtitle",1)
componentCustomToolbar.toolbar.setNavigationOnClickListener { onBackpressed() }
setSupportActionBar(componentCustomToolbar.toolbar)
supportActionBar?.setdisplayHomeAsUpEnabled(true)
supportActionBar?.setdisplayShowTitleEnabled(false)
}
}
活动 xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.ComponentCustomToolbar
android:id="@+id/component_custom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.widget.nestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/red"
android:layout_marginBottom="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/red"
android:layout_marginBottom="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/red"
android:layout_marginBottom="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/red"
android:layout_marginBottom="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/red"
android:layout_marginBottom="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/red"
android:layout_marginBottom="50dp"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/red"
android:layout_marginBottom="50dp"></LinearLayout>
</LinearLayout>
</android.support.v4.widget.nestedScrollView>
</android.support.design.widget.CoordinatorLayout>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)