Android自定義側滑Item

源碼地址:https://github.com/LanSeLianMa/CustomizeView/tree/master/cehuaitem

使用方式一:XML布局中直接使用

<?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=".MainActivity"><com.test.festec.cehuaitem.widget.SideslipContainerandroid:id="@+id/sideslip_container01"android:layout_width="match_parent"android:layout_height="70dp"app:option_width="65dp"><com.test.festec.cehuaitem.widget.SideslipContentandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:background="@color/d5"android:paddingTop="20dp"android:textAlignment="center"android:textSize="20sp"android:text="content"android:layout_width="match_parent"android:layout_height="match_parent" /></com.test.festec.cehuaitem.widget.SideslipContent><com.test.festec.cehuaitem.widget.SideslipOptionandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/blue"android:tag="add"android:text="增加" /><com.test.festec.cehuaitem.widget.SideslipOptionandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/orange"android:tag="edit"android:text="編輯" /><com.test.festec.cehuaitem.widget.SideslipOptionandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/red"android:tag="delete"android:text="刪除" /></com.test.festec.cehuaitem.widget.SideslipContainer></LinearLayout>

Activity中監聽

class MainActivity : Activity() {private lateinit var binding: ActivityMainBindingprivate var options = mutableListOf("增加", "編輯", "刪除")private var optionBg = mutableListOf(R.color.blue, R.color.orange, R.color.red)@SuppressLint("UseCompatLoadingForDrawables")@RequiresApi(Build.VERSION_CODES.LOLLIPOP)override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityMainBinding.inflate(layoutInflater)setContentView(binding.root)binding.sideslipContainer01.addOnClickListener(object : SideslipContainer.SideslipContainerOnClick {override fun optionOnClick(optionTag: Any) {Toast.makeText(this@MainActivity,"optionTag:$optionTag", Toast.LENGTH_SHORT).show()binding.sideslipContainer01.sideslipRecover()}override fun contentOnClick() {Toast.makeText(this@MainActivity,"content", Toast.LENGTH_SHORT).show()binding.sideslipContainer01.sideslipRecover()}})}}

使用方式二:代碼方式使用

先定義一個容器

<?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=".MainActivity"><LinearLayoutandroid:id="@+id/container"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical" /></LinearLayout>

Activity中動態添加

class MainActivity : Activity() {private lateinit var binding: ActivityMainBindingprivate var options = mutableListOf("增加", "編輯", "刪除")private var optionBg = mutableListOf(R.color.blue, R.color.orange, R.color.red)@SuppressLint("UseCompatLoadingForDrawables")@RequiresApi(Build.VERSION_CODES.LOLLIPOP)override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityMainBinding.inflate(layoutInflater)setContentView(binding.root)// 創建側滑容器,配置基礎參數val sideslip = SideslipContainer(this, DensityUtil.dp2px(this, 65f))val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,DensityUtil.dp2px(this, 70f))sideslip.layoutParams = layoutParams// 創建側滑內容val content = SideslipContent(this).apply {background = resources.getDrawable(R.color.black,null)}// 加入側滑容器中sideslip.addView(content)// 創建選項卡,并加入側滑容器中options.forEachIndexed { index, str ->val option = SideslipOption(this,str)option.text = stroption.background = resources.getDrawable(optionBg[index], null)sideslip.addView(option)}// 點擊監聽sideslip.addOnClickListener(object : SideslipContainer.SideslipContainerListener {override fun optionOnClick(optionTag: Any) {Log.e("TAG","optionTag:$optionTag")}override fun contentOnClick() {Log.e("TAG","content")}})binding.container.addView(sideslip)}}

使用方式三:結合RecyclerView使用

重寫LayoutManager

// 重寫LayoutManager,動態讓RecyclerView 禁止/恢復 Y軸滾動
open class CustomLinerLayoutManager(context: Context) : LinearLayoutManager(context) {private var isScrollEnabled = truefun getEnabled(): Boolean {return isScrollEnabled}fun setScrollEnabled(flag: Boolean) {isScrollEnabled = flag}override fun canScrollVertically(): Boolean {return isScrollEnabled && super.canScrollVertically();}}

重寫RecyclerView

class MyRecyclerView : RecyclerView {constructor(context: Context) : super(context)constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)init {addOnScrollListener(object : RecyclerView.OnScrollListener() {/**** public static final int SCROLL_STATE_IDLE = 0;  :  RecyclerView 當前未滾動。** public static final int SCROLL_STATE_DRAGGING = 1;  :  RecyclerView 當前正在被外部輸入(例如用戶觸摸輸入)拖動。** public static final int SCROLL_STATE_SETTLING = 2;  :  RecyclerView 當前正在動畫到最終位置,而不是在外部控制。*/override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {childrenRecover()}}})}// view初始化private var viewInit = false// 上一會觸摸的子Viewvar originalChild: SideslipContainer? = null// 當前觸摸的子Viewvar currentChild: SideslipContainer? = nullprivate var customLayoutManager: CustomLinerLayoutManager? = nullprivate var childMoveCallback = object : ChildOnTouchCallback {override fun currentChildMove() {childrenRecover()}override fun originalChild(originalSideslip: SideslipContainer?) {originalChild = originalSideslip}override fun currentChild(currentContainer: SideslipContainer?) {currentChild = currentContainer}override fun listStopYScroll() {// Log.e("TAG", "List停止滾動")if (customLayoutManager!!.getEnabled()) {customLayoutManager?.setScrollEnabled(false)}}override fun listRecoverYScroll() {// Log.e("TAG", "List恢復滾動")if (!(customLayoutManager!!.getEnabled())) {customLayoutManager?.setScrollEnabled(true)}}}// 子View恢復private fun childrenRecover() {children.forEach {(it as SideslipContainer).sideslipRecover()}}override fun onViewAdded(child: View?) {// Log.e("TAG","onViewAdded")val sideslipContainer = (child as SideslipContainer)sideslipContainer.addOnChildMoveCallback(childMoveCallback)}// 當復用item,徹底超出屏幕,不可見時執行override fun onViewRemoved(child: View?) {// Log.e("TAG","onViewRemoved")(child as SideslipContainer).sideslipStateRest()}override fun onWindowFocusChanged(hasWindowFocus: Boolean) {// Log.e("TAG","onWindowFocusChanged")super.onWindowFocusChanged(hasWindowFocus)if (!viewInit) {customLayoutManager = (layoutManager as CustomLinerLayoutManager)}viewInit = true}interface ChildOnTouchCallback {// 有子View側滑了fun currentChildMove()// 上一個觸摸的子Viewfun originalChild(originalSideslip: SideslipContainer?)// 當前觸摸的子Viewfun currentChild(currentContainer: SideslipContainer?)// 列表停止Y軸滾動fun listStopYScroll()// 列表恢復Y軸滾動fun listRecoverYScroll()}}

RecyclerView適配器

class MyListAdapter(var context: Context,var data: MutableList<Info>
) : RecyclerView.Adapter<MyListAdapter.MyViewHolder>() {private var options = mutableListOf("增加", "編輯", "刪除")private var optionBg =mutableListOf(R.color.blue, R.color.orange, R.color.red)@RequiresApi(Build.VERSION_CODES.LOLLIPOP)override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {val itemView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false)val sideslip = sideslipContainer(itemView)return MyViewHolder(sideslip)}@SuppressLint("UseCompatLoadingForDrawables", "ResourceAsColor")@RequiresApi(Build.VERSION_CODES.LOLLIPOP)override fun onBindViewHolder(holder: MyViewHolder, position: Int) {val sideslip = holder.itemView as SideslipContainer// 根據不同權限,添加不同的選項卡val optionsView = mutableListOf<SideslipOption>()if (data[position].level == 0) {optionsView.clear()} else if (data[position].level == 1) {val option = SideslipOption(context, options[1])option.text = options[1]option.background = context.resources.getDrawable(optionBg[1], null)optionsView.add(option)} else {options.forEachIndexed { index, str ->val option = SideslipOption(context, str)option.text = stroption.background = context.resources.getDrawable(optionBg[index], null)optionsView.add(option)}}sideslip.addMultipleOption(optionsView)// 點擊回調sideslip.addOnClickListener(object : SideslipContainer.SideslipContainerOnClick {override fun optionOnClick(optionTag: Any) {Toast.makeText(context,"${holder.adapterPosition} -- optionTag:$optionTag",Toast.LENGTH_SHORT).show()sideslip.sideslipRecover()}override fun contentOnClick() {Toast.makeText(context,"${holder.adapterPosition} - content",Toast.LENGTH_SHORT).show()sideslip.sideslipRecover()}})holder.idTv.text = data[position].id.toString()holder.titleTv.text = data[position].titleholder.describeTv.text = data[position].describe}override fun getItemCount(): Int {return data.size}@SuppressLint("UseCompatLoadingForDrawables")@RequiresApi(Build.VERSION_CODES.LOLLIPOP)private fun sideslipContainer(itemView: View): SideslipContainer {// 創建側滑容器,配置基礎參數val sideslip = SideslipContainer(context)sideslip.setOptionWidth(DensityUtil.dp2px(context, 65f))val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,DensityUtil.dp2px(context, 70f))sideslip.layoutParams = layoutParams// 創建側滑內容val content = SideslipContent(context)content.addView(itemView)// 加入側滑容器中sideslip.addView(content)// 創建選項卡,并加入側滑容器中// options.forEachIndexed { index, str ->//    val option = SideslipOption(context, str)//    option.text = str//    option.background = context.resources.getDrawable(optionBg[index], null)//    sideslip.addView(option)// }return sideslip}class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {var idTv: TextViewvar titleTv: TextViewvar describeTv: TextViewinit {idTv = itemView.findViewById(R.id.id_tv)titleTv = itemView.findViewById(R.id.title_tv)describeTv = itemView.findViewById(R.id.describe_tv)}}
}

Activity中綁定數據

class ListActivity : Activity() {private lateinit var binding: ListLayoutBindingprivate val data: MutableList<Info> = mutableListOf(Info(0, "title", "content", 2),Info(1, "title", "content", 1),Info(2, "title", "content", 2),Info(3, "title", "content", 2),Info(4, "title", "content", 2),Info(5, "title", "content", 1),Info(6, "title", "content", 1),Info(7, "title", "content", 2),Info(8, "title", "content", 2),Info(9, "title", "content", 1),Info(10, "title", "content", 1),Info(11, "title", "content", 2),Info(12, "title", "content", 2),Info(13, "title", "content", 1),Info(14, "title", "content", 1),Info(15, "title", "content", 2),Info(16, "title", "content", 2),Info(17, "title", "content", 2),Info(18, "title", "content", 2),Info(19, "title", "content", 1),Info(20, "title", "content", 2),Info(21, "title", "content", 1),Info(22, "title", "content", 2),Info(23, "title", "content", 2),Info(24, "title", "content", 2),Info(25, "title", "content", 1),Info(26, "title", "content", 2),Info(27, "title", "content", 1),Info(28, "title", "content", 2),Info(29, "title", "content", 2),Info(30, "title", "content", 2),Info(31, "title", "content", 1),Info(32, "title", "content", 1),Info(33, "title", "content", 2),Info(34, "title", "content", 2),Info(35, "title", "content", 1),)override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ListLayoutBinding.inflate(layoutInflater)setContentView(binding.root)val adapter = MyListAdapter(this, data)val linearLayoutManager = CustomLinerLayoutManager(this)binding.root.layoutManager = linearLayoutManagerbinding.root.adapter = adapter}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/35860.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/35860.shtml
英文地址,請注明出處:http://en.pswp.cn/news/35860.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python爬蟲 爬取圖片

在我們日常上網瀏覽網頁的時候&#xff0c;經常會看到一些好看的圖片&#xff0c;我們就希望把這些圖片保存下載&#xff0c;或者用戶用來做桌面壁紙&#xff0c;或者用來做設計的素材。 我們最常規的做法就是通過鼠標右鍵&#xff0c;選擇另存為。但有些圖片鼠標右鍵的時候并沒…

CVPR 2023 | 用戶可控的條件圖像到視頻生成方法(基于Diffusion)

注1:本文系“計算機視覺/三維重建論文速遞”系列之一&#xff0c;致力于簡潔清晰完整地介紹、解讀計算機視覺&#xff0c;特別是三維重建領域最新的頂會/頂刊論文(包括但不限于 Nature/Science及其子刊; CVPR, ICCV, ECCV, NeurIPS, ICLR, ICML, TPAMI, IJCV 等)。 本次介紹的論…

使用chatGPT-4 暢聊量子物理學(二)

Omer 量子力學的主導哲學或模型或解釋是什么&#xff1f; ChatGPT 量子力學是一門描述微觀世界中粒子行為的物理學理論&#xff0c;但它的解釋和哲學觀點在學術界存在多種不同的觀點和爭議。以下是幾種主要的哲學觀點或解釋&#xff1a; 哥本哈根解釋&#xff1a;這是最為廣泛…

Windows 11清除無效、回收站、過期、緩存、補丁更新文件

Windows 11與之前的Windows版本類似&#xff0c;也需要定期清理無效、垃圾、過期、緩存文件來保持系統性能和存儲空間的優化。以下是在Windows 11中進行這些清理操作的一些建議方法&#xff1a; 磁盤清理工具 Windows 11內置了磁盤清理工具&#xff0c;可以幫助你刪除臨時文件…

Node.js學習筆記-03

七、網絡編程 1. 構建 TCP 服務 TCP 是面向連接的協議&#xff0c;顯著特征 在傳輸之前需要3次握手形成會話。 客戶端 ——請求連接——> 服務器端 ——響應——> 客戶端 ——開始傳輸——> 服務器端。 2. 構建 UDP 服務 3. 構建 HTTP 服務 http模塊 在node中HTT…

《Java面向對象程序設計》學習筆記——第 7 章 面向對象設計的基本原則

?專欄&#xff1a;《Java面向對象程序設計》學習筆記 ?# 第 7 章 面向對象設計的基本原則 7.1 UML 類圖簡介 類的 UML 圖 長方形垂直地分為三層。 第 1 層是名字層。 名字是常規字形&#xff0c;表明該類是具體類&#xff0c;如果類的名字是斜體字形&#xff0c;表明該類…

C語言可變數組 嵌套的可變數組,翻過了山跨過了河 又掉進了坑

可變數組 ?專欄內容&#xff1a; postgresql內核源碼分析 手寫數據庫toadb 并發編程 個人主頁&#xff1a;我的主頁 座右銘&#xff1a;天行健&#xff0c;君子以自強不息&#xff1b;地勢坤&#xff0c;君子以厚德載物. 概述 數組中元素是順序存放&#xff0c;這一特性讓我們…

【IC萌新虛擬項目】spt_core模塊基于dc的綜合環境搭建與面積時序優化

關于整個虛擬項目,請參考: 【IC萌新虛擬項目】Package Process Unit項目全流程目錄_尼德蘭的喵的博客-CSDN博客 前言 當驗證的同學正在瘋狂寫測試點,補充測試用例各種找茬找bug時候,設計的同學也要進入到跑綜合修時序優化面積的階段了。 還是老樣子,關于芯片綜合的知識就…

Redis_緩存3_緩存異常(數據不一致、雪崩、擊穿、穿透)

14.6緩存異常 四個方面 緩存中數據和數據庫不一致緩存雪崩緩存擊穿緩存穿透 14.6.1數據不一致&#xff1a; 一致性包括兩種情況 緩存中有數據&#xff0c;需要和數據庫值相同緩存中沒有數據&#xff0c;數據庫中的數據是最新值 如果不符合以上兩種情況&#xff0c;則出現…

Linux tee

tee 是一個命令行工具&#xff0c;它可以從標準輸入讀取數據&#xff0c;并將其同時輸出到標準輸出和指定的文件中。tee 命令非常實用&#xff0c;特別是在需要同時查看輸出內容和將其保存到文件中的情況下。 tee 命令的基本語法如下&#xff1a; command | tee [options] [f…

Mysql 搭建MHA高可用架構,實現自動failover,完成主從切換

目錄 自動failover MHA&#xff1a; MHA 服務 項目&#xff1a;搭建Mysql主從復制、MHA高可用架構 實驗項目IP地址配置&#xff1a; MHA下載地址 項目步驟&#xff1a; 一、修改主機名 二、編寫一鍵安裝mha node腳本和一鍵安裝mha mangaer腳本&#xff0c;并執行安裝 …

docker容器限定ip訪問

docker容器限定ip訪問 一、測試所需環境&#xff1a;二、使用docker的 iptables 策略三、Docker使用iptables 與系統Firewalld之間的關系四、沖突解決方案 一、測試所需環境&#xff1a; 主機1&#xff1a; ip&#xff1a;192.168.3.117 環境配置&#xff1a;docker、httpd(do…

你真的了解ORM嗎?通過一個簡單的例子來學習ORM

什么是ORM ORM&#xff08;Object-Relational Mapping&#xff09;是一種將面向對象程序數據模型與關系數據庫之間進行映射的技術。 比如數據庫表user&#xff0c;它有id、name、age字段映射到Java實體類就是User類&#xff0c;有id、name、age屬性。 CREATE TABLE user (id…

2023國賽 高教社杯數學建模ABCDE題思路匯總分析

文章目錄 0 賽題思路1 競賽信息2 競賽時間3 建模常見問題類型3.1 分類問題3.2 優化問題3.3 預測問題3.4 評價問題 4 建模資料 0 賽題思路 &#xff08;賽題出來以后第一時間在CSDN分享&#xff09; https://blog.csdn.net/dc_sinor?typeblog 1 競賽信息 全國大學生數學建模…

echarts加釣魚島赤尾嶼(vue)(親測有效)

1.首先引入json文件&#xff0c;node_modules/echarts中就有 import chinaData from "../../node_modules/echarts/map/json/china.json" 2.初始化地圖&#xff0c;在初始化地圖的時候加入釣魚島和赤尾嶼的數據&#xff0c;在chinaData下的features中加入即可&#x…

Design-Pattern設計模式

Design-Pattern設計模式 圖說設計模式 圖說設計模式 在線書籍 軟件模式是將模式的一般概念應用于軟件開發領域&#xff0c;即軟件開發的 總體指導思路或參照樣板。軟件模式并非僅限于設計模式&#xff0c;還包括 架構模式、分析模式和過程模式等&#xff0c;實際上&#xff…

FFmpeg常見命令行(四):FFmpeg流媒體

前言 在Android音視頻開發中&#xff0c;網上知識點過于零碎&#xff0c;自學起來難度非常大&#xff0c;不過音視頻大牛Jhuster提出了《Android 音視頻從入門到提高 - 任務列表》&#xff0c;結合我自己的工作學習經歷&#xff0c;我準備寫一個音視頻系列blog。本文是音視頻系…

leetcode做題筆記77組合

給定兩個整數 n 和 k&#xff0c;返回范圍 [1, n] 中所有可能的 k 個數的組合。 你可以按 任何順序 返回答案。 思路一&#xff1a;直接求出組合數將每個組合放進數組中 int** combine(int n, int k, int* returnSize, int** returnColumnSizes) {int size 0, num 1, i;in…

Rust中的智能指針:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak<T>

Rust中的智能指針是什么 智能指針&#xff08;smart pointers&#xff09;是一類數據結構&#xff0c;是擁有數據所有權和額外功能的指針。是指針的進一步發展 指針&#xff08;pointer&#xff09;是一個包含內存地址的變量的通用概念。這個地址引用&#xff0c;或 ” 指向”…

UML 類圖的畫法

1.類圖的畫法 類 整體是個矩形&#xff0c;第一層類名&#xff0c;第二層屬性&#xff0c;第三層方法。 &#xff1a;public- : private# : protected空格: 默認的default 對應的類寫法。 public class Student {public String name;public Integer age;protected I…