android头部固定悬停,Android开发上滑悬停且头部可刷新
需求:上滑列表后推薦,小島,專題置頂,可左右切換。因為頭部有重要內容,所有頭部出現且滑到頂之后,再下來可刷新頭部內容
效果圖:
scroll1.jpg
Screenshot_20200627_134124_com.cong.coordinatorla.jpg
Screenshot_20200627_134130_com.cong.coordinatorla.jpg
實現思路:
首先上滑懸停想到的是協調布局CoordinatorLayout,
第二用刷新控件包裹著協調布局,我用的刷新控件是refreshlayout.RefreshLayout
第三在代碼中app_bar_layout.addOnOffsetChangedListener判斷刷新布局什么時候可用,什么時候不可用
下面是實現文檔
步驟一:布局
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:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="頭部刷新"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="20sp"
android:background="@color/white"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tv_title"
>
android:id="@+id/mRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
app:layout_constraintTop_toTopOf="parent"
>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:background="@color/white"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
android:id="@+id/common_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="pin" />
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/stl_ceo_data"
android:layout_width="match_parent"
android:layout_height="40dp"
app:layout_constraintTop_toTopOf="parent"
app:tl_indicator_color="@color/color_D5100A"
app:tl_indicator_height="2dp"
app:tl_indicator_width="21dp"
app:tl_tab_space_equal="true"
app:tl_textBold="BOTH"
app:tl_textSelectColor="@color/color_D5100A"
app:tl_textUnselectColor="@color/c_33"
app:tl_textsize="16sp" />
android:layout_width="0dp"
android:layout_height="1px"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@color/c_f2efef"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/stl_ceo_data" />
android:id="@+id/vp_pager_ceo_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
步驟二:使用示例
class OutRefreshActivity : AppCompatActivity() {
private var context: Context? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_out_refresh)
context = this
setRefreshLayout(mRefreshLayout)
common_recycler.layoutManager = LinearLayoutManager(context)
val headView = LayoutInflater.from(context).inflate(R.layout.layout_out_refresh_head, common_recycler, false)
val headAdapter = OutRefreshHeadAdapter()
common_recycler?.adapter = headAdapter
headAdapter.addHeaderView(headView)
//關鍵,什么時候RefreshLayout可用
app_bar_layout.addOnOffsetChangedListener(object :AppBarLayout.OnOffsetChangedListener{
override fun onOffsetChanged(p0: AppBarLayout?, verticalOffset: Int) {
mRefreshLayout.isEnabled = verticalOffset >=0
//只能下拉
mRefreshLayout?.direction = RefreshLayoutDirection.TOP
}
})
val fragments: MutableList = ArrayList()
fragments.add(AFragment())
fragments.add(BFragment())
fragments.add(CFragment())
val titles: MutableList = ArrayList()
titles.add("推薦")
titles.add("小島")
titles.add("專題")
val baseFragmentAdapter = BaseFragmentAdapter(supportFragmentManager, fragments, titles)
vp_pager_ceo_data.setAdapter(baseFragmentAdapter)
stl_ceo_data.setViewPager(vp_pager_ceo_data)
}
protected fun setRefreshLayout(refreshLayout: RefreshLayout) {
refreshLayout.direction = RefreshLayoutDirection.BOTH
refreshLayout.setColorSchemeResources(R.color.m_red_two, R.color.m_charcoal_grey, R.color.m_purple, R.color.m_green, R.color.m_blue)
refreshLayout.setOnRefreshListener(object : RefreshLayout.OnRefreshListener {
override fun onPullDownToRefresh() {
//做刷新操作,模擬請求接口
Handler().postDelayed(object :Runnable{
override fun run() {
refreshLayout.isRefreshing = false //隱藏刷新圈圈
}
},1000)
}
override fun onPullUpToRefresh() {
//加載更多
}
})
}
}
上面有OutRefreshHeadAdapter,BaseFragmentAdapter,AFragment沒給出,就是普通的操作類,相信你們都會寫或者項目中就有。
重點是上面的關鍵代碼
app_bar_layout.addOnOffsetChangedListener(object :AppBarLayout.OnOffsetChangedListener{
override fun onOffsetChanged(p0: AppBarLayout?, verticalOffset: Int) {
mRefreshLayout.isEnabled = verticalOffset >=0
//只能下拉
mRefreshLayout?.direction = RefreshLayoutDirection.TOP
}
})
ok,這樣的效果就做好了。
總結
以上是生活随笔為你收集整理的android头部固定悬停,Android开发上滑悬停且头部可刷新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android7.1开机监听广播,And
- 下一篇: android.mk编译动态库,安卓之A