日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android头部固定悬停,Android开发上滑悬停且头部可刷新

發布時間:2024/10/8 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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开发上滑悬停且头部可刷新的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。