Android低代码开发 - 直接创建一个下拉刷新列表界面

看了我Android低代码开发 - 让IDE帮你写代码这篇文章的小伙伴,大概都对Dora全家桶开发框架有基本的认识了吧。本篇文章将会讲解如何使用dora-studio-plugin快捷创建一个下拉刷新列表界面。

效果演示

请添加图片描述

这样直接通过图形界面的方式就创建好了下拉刷新上拉加载+空态界面+列表的基础代码,接下来开发起来就方便了。

依赖库

截屏2024-06-13 10.39.15.png

DoraTitleBar:建议用最新版本1.37

DoraEmptyLayout:必须用1.12版本

SwipeLayout和PullableRecyclerView:用1.0版本就好

IntelliJ IDEA插件1.4版本更新内容

生成布局文件的模板
/** Copyright (C) 2022 The Dora Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.dorachat.templates.recipes.app_package.res.layoutfun swipeLayoutActivityXml(packageName: String,activityClass: String
) = """
<?xml version="1.0" encoding="utf-8"?>
<layout 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"tools:context="${packageName}.${activityClass}"><data></data><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><dora.widget.DoraTitleBarandroid:id="@+id/titleBar"android:layout_width="match_parent"android:layout_height="50dp"app:dview_title="@string/app_name"android:background="@color/colorPrimary"/><dora.widget.DoraEmptyLayoutandroid:id="@+id/emptyLayout"android:layout_width="match_parent"android:layout_height="match_parent"><dora.widget.pull.SwipeLayoutandroid:id="@+id/swipeLayout"android:layout_width="match_parent"android:layout_height="match_parent"><include layout="@layout/layout_swipe_layout_header" /><dora.widget.pull.PullableRecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPanelBg" /><include layout="@layout/layout_swipe_layout_footer" /></dora.widget.pull.SwipeLayout></dora.widget.DoraEmptyLayout></LinearLayout>
</layout>
"""
生成Java和Kotlin代码的模板
/** Copyright (C) 2022 The Dora Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.dorachat.templates.recipes.app_package.srcfun swipeLayoutActivityKt(applicationPackage: String,packageName: String,activityClass: String,bindingName: String,layoutName: String
) = """
package ${packageName}import android.os.Bundleimport dora.BaseActivity
import dora.widget.pull.SwipeLayoutimport ${applicationPackage}.R
import ${applicationPackage}.databinding.${bindingName}class ${activityClass} : BaseActivity<${bindingName}>() {override fun getLayoutId(): Int {return R.layout.${layoutName}}override fun initData(savedInstanceState: Bundle?, binding: ${bindingName}) {TODO("Not yet implemented")// For Example:// binding.swipeLayout.setOnSwipeListener(object : SwipeLayout.OnSwipeListener {////     override fun onRefresh(swipeLayout: SwipeLayout) {//     }////     override fun onLoadMore(swipeLayout: SwipeLayout) {//     }// })}
}
"""fun swipeLayoutActivity(applicationPackage: String,packageName: String,activityClass: String,bindingName: String,layoutName: String
) = """
package ${packageName};import android.os.Bundle;
import androidx.annotation.Nullable;import dora.BaseActivity;
import dora.widget.pull.SwipeLayout;import ${applicationPackage}.R;
import ${applicationPackage}.databinding.${bindingName};public class ${activityClass} extends BaseActivity<${bindingName}> {@Overrideprotected int getLayoutId() {return R.layout.${layoutName};}@Overridepublic void initData(@Nullable Bundle savedInstanceState, ${bindingName} binding) {// TODO: Not yet implemented// For Example:// binding.swipeLayout.setOnSwipeListener(new SwipeLayout.OnSwipeListener() {////     @Override//     public void onRefresh(SwipeLayout swipeLayout) {//     }//     //     @Override//     public void onLoadMore(SwipeLayout swipeLayout) {//     }// });}
}
"""
DoraTemplateRecipe.kt新增生成代码的方法

fun RecipeExecutor.swipeLayoutActivityRecipe(moduleData: ModuleTemplateData,activityClass: String,activityTitle: String,layoutName: String,packageName: String
) {val (projectData, srcOut, resOut) = moduleDatagenerateManifest(moduleData = moduleData,activityClass = activityClass,packageName = packageName,isLauncher = false,hasNoActionBar = false,generateActivityTitle = false)if (projectData.language == Language.Kotlin) {save(swipeLayoutActivityKt(projectData.applicationPackage ?: packageName, packageName, activityClass,buildBindingName(layoutName), layoutName), srcOut.resolve("${activityClass}.${projectData.language.extension}"))}if (projectData.language == Language.Java) {save(swipeLayoutActivity(projectData.applicationPackage ?: packageName, packageName, activityClass,buildBindingName(layoutName), layoutName), srcOut.resolve("${activityClass}.${projectData.language.extension}"))}save(swipeLayoutActivityXml(packageName, activityClass), resOut.resolve("layout/${layoutName}.xml"))open(resOut.resolve("layout/${layoutName}.xml"))}
新增一个向导界面模板
/** Copyright (C) 2022 The Dora Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.dorachat.templates.recipesimport com.android.tools.idea.wizard.template.*
import com.android.tools.idea.wizard.template.impl.activities.common.MIN_API
import java.io.Fileobject SwipeLayoutActivityTemplate : Template {override val category: Categoryget() = Category.Activityoverride val constraints: Collection<TemplateConstraint>get() = emptyList()     // AndroidX, kotlinoverride val description: Stringget() = "创建一个dora.BaseActivity,来自https://github.com/dora4/dora"override val documentationUrl: String?get() = nulloverride val formFactor: FormFactorget() = FormFactor.Mobileoverride val minSdk: Intget() = MIN_APIoverride val name: Stringget() = "SwipeLayout DataBinding Activity"override val recipe: Recipeget() = {swipeLayoutActivityRecipe(it as ModuleTemplateData,activityClassInputParameter.value,activityTitleInputParameter.value,layoutNameInputParameter.value,packageName.value)}override val uiContexts: Collection<WizardUiContext>get() = listOf(WizardUiContext.ActivityGallery, WizardUiContext.MenuEntry, WizardUiContext.NewProject, WizardUiContext.NewModule)override val useGenericInstrumentedTests: Booleanget() = falseoverride val useGenericLocalTests: Booleanget() = falseoverride val widgets: Collection<Widget<*>>get() = listOf(TextFieldWidget(activityTitleInputParameter),TextFieldWidget(activityClassInputParameter),TextFieldWidget(layoutNameInputParameter),PackageNameWidget(packageName),LanguageWidget())override fun thumb(): Thumb {return Thumb { findResource(this.javaClass, File("template_activity.png")) }}val activityClassInputParameter = stringParameter {name = "Activity Name"default = "MainActivity"help = "The name of the activity class to create"constraints = listOf(Constraint.CLASS, Constraint.UNIQUE, Constraint.NONEMPTY)suggest = { layoutToActivity(layoutNameInputParameter.value) }}var layoutNameInputParameter: StringParameter = stringParameter {name = "Layout Name"default = "activity_main"help = "The name of the layout to create for the activity"constraints = listOf(Constraint.LAYOUT, Constraint.UNIQUE, Constraint.NONEMPTY)suggest = { activityToLayout(activityClassInputParameter.value) }}val activityTitleInputParameter = stringParameter {name = "Title"default = "Main"help = "The name of the activity. For launcher activities, the application title"visible = { false }constraints = listOf(Constraint.NONEMPTY)suggest = { buildClassNameWithoutSuffix(activityClassInputParameter.value, "Activity") }}val packageName = defaultPackageNameParameter
}
将向导界面模板添加到向导模板提供者
/** Copyright (C) 2022 The Dora Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.dorachat.templates.recipesimport com.android.tools.idea.wizard.template.WizardTemplateProviderclass DoraTemplateWizardProvider: WizardTemplateProvider() {override fun getTemplates() = listOf(DataBindingActivityTemplate,DataBindingFragmentTemplate,MenuPanelActivityTemplate,SwipeLayoutActivityTemplate,MVVMActivityTemplate,MVVMFragmentTemplate)
}
更新版本日志
patchPluginXml {version.set("${project.version}")sinceBuild.set("213")untilBuild.set("223.*")changeNotes.set("""<h3>1.4</h3>新增对SwipeLayout的支持<br/><h3>1.3</h3>新增对MenuPanel的支持<br/><h3>1.2</h3>新增对BaseVMActivity和BaseVMFragment的支持<br/><h3>1.1</h3>initData()方法中增加databinding参数<br/><h3>1.0</h3>初始版本,能够创建Java和Kotlin版本的MVVM Activiy和MVVM Fragment<br/>""")
}

代码讲解

DoraEmptyLayout为什么可以识别SwipeLayout里面的RecyclerView?
open fun showContent() {runMain {if (contentView is RecyclerView) {if ((contentView as RecyclerView).adapter == null ||(contentView as RecyclerView).adapter!!.itemCount == 0) {showEmpty()return@runMain}}// 1.12开始支持遍历容器,确保一个EmptyLayout里面只能放一个RecyclerViewif (contentView is ViewGroup) {for (i in 0 until childCount) {val view = getChildAt(i)if (view is RecyclerView) {if (view.adapter == null ||view.adapter!!.itemCount == 0) {showEmpty()return@runMain}}}}val view = showStateView(STATE_CONTENT)this.content?.invoke(view)}
}

我们可以看到DoraEmptyLayout类从1.11升级到1.12版本的过程中,新增了以下代码来支持SwipeLayout。

if (contentView is ViewGroup) {for (i in 0 until childCount) {val view = getChildAt(i)if (view is RecyclerView) {if (view.adapter == null ||view.adapter!!.itemCount == 0) {showEmpty()return@runMain}}}
}        

这样就不难理解了,如果遇到了刷新布局,如SwipeLayout,就再解析一层找RecyclerView。当然,一个DoraEmptyLayout里面只能放一个RecyclerView。

SwipeLayout是何方神圣?
  • 支持暗色模式
  • 支持英语、阿拉伯语、德语、西班牙语、法语、意大利语、日语、韩语、葡萄牙语、俄语、泰语、越南语、简体中文和繁体中文等世界10几个主流语种
  • 支持自定义可拉动的内容布局
  • 支持插件创建
  • 与DoraEmptyLayout空态布局完美兼容(需要使用v1.12以上版本)
  • 界面丝滑
通过PullableRecyclerView源码来看怎么自定义可拉动的内容布局
package dora.widget.pullimport android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import dora.widget.swipelayout.Rclass PullableRecyclerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null,defStyle: Int = 0): RecyclerView(context, attrs, defStyle), Pullable {private var canPullDown = trueprivate var canPullUp = trueinit {val a = context.obtainStyledAttributes(attrs, R.styleable.PullableRecyclerView, defStyle, 0)canPullDown = a.getBoolean(R.styleable.PullableRecyclerView_dview_canPullDown, canPullDown)canPullUp = a.getBoolean(R.styleable.PullableRecyclerView_dview_canPullUp, canPullUp)a.recycle()}fun setCanPullDown(canPullDown: Boolean) {this.canPullDown = canPullDown}fun setCanPullUp(canPullUp: Boolean) {this.canPullUp = canPullUp}override fun canPullDown(): Boolean {return if (canPullDown) {val layoutManager = layoutManager as LinearLayoutManager?val adapter = adapterif (adapter != null) {return if (adapter.itemCount == 0) {false} else layoutManager!!.findFirstVisibleItemPosition() == 0&& getChildAt(0).top >= 0}false} else {false}}override fun canPullUp(): Boolean {if (canPullUp) {val layoutManager = layoutManager as LinearLayoutManagerif (adapter != null && adapter?.itemCount!! == 0) {return false} else if (layoutManager.findLastVisibleItemPosition() == ((adapter as Adapter).itemCount - 1)) {// 滑到底部了if (getChildAt(layoutManager.findLastVisibleItemPosition() - layoutManager.findFirstVisibleItemPosition()) != null&& getChildAt(layoutManager.findLastVisibleItemPosition()- layoutManager.findFirstVisibleItemPosition()).bottom <= measuredHeight) {return true}}return false} else {return false}}
}

它实现了一个顶层接口Pullable,通过canPullDown()canPullUp()两个方法来在运行时动态判断可不可以下拉刷新和上拉加载。

private var canPullDown = true
private var canPullUp = true

里面提供了两个属性,表示是否有下拉和上拉能力,如果设置为false,则无论条件达成与否都不能进行刷新和加载。

<dora.widget.pull.PullableRecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPanelBg"app:dview_canPullDown="true"app:dview_canPullUp="false"/>

可以通过属性设置这两个变量,比如不需要上拉加载就把上拉的设置为false,app:dview_canPullUp=“false”。

设置完成刷新和加载的监听
binding.swipeLayout.setOnSwipeListener(new SwipeLayout.OnSwipeListener() {@Overridepublic void onRefresh(SwipeLayout swipeLayout) {}@Overridepublic void onLoadMore(SwipeLayout swipeLayout) {}
});

通过调用swipeLayout的refreshFinish(state)loadMoreFinish(state)来结束刷新和加载状态。

const val SUCCEED = 0
const val FAIL = 1

有成功和失败两种状态可以设置。所以,你在onRefresh()或onLoadMore()的最后一行调用刷新状态即可。

源码链接

下拉刷新:https://github.com/dora4/dview-swipe-layout

空态布局:https://github.com/dora4/dview-empty-layout

代码生成插件:https://github.com/dora4/dora-studio-plugin

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/851738.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

旅游行业电商平台:数字化转型的引擎与未来发展趋势

引言 旅游行业数字化转型的背景和重要性 随着信息技术的飞速发展&#xff0c;数字化转型成为各行业发展的必然趋势。旅游行业&#xff0c;作为一个高度依赖信息和服务的领域&#xff0c;数字化转型尤为重要。通过数字化手段&#xff0c;旅游行业能够实现资源的高效配置、服务的…

用映射对比ab俩个数组 , 并把a的参数传递给b

项目背景 : react ant 需求 : 在项目进行表头设置时,根据aaa中的key和bbb中的name对应 , 并将sort值插入到bbb中 其中 a b 结构如下 具体实现 aaa[ { key: "orderNumber", orderNumber: "工单编号", sort: 1 } ... ]bbb [ { name: "orderNumber…

Jenkins For Windows编译构建C#项目环境搭建(完整版)

安装Jenkins 下载Windows安装包 官方下载地址 选择稳定版&#xff0c;这里下载的是最新版&#xff0c;如需下载指定版本点击 以前的发行版 配置java环境 下载 java jdk 17 jdk17官方下载链接 这里下载的是msi版本的安装包 安装jdk17 双击运行安装包&#xff0c;一直下…

3D数字化营销——电商行业提升转化率,降低退货率的杀手锏!

随着3D、AR等前沿技术的蓬勃发展&#xff0c;电商行业正迎来一场3D数字化营销的革命。这种创新的营销方式不仅极大地丰富了商品的展现形式&#xff0c;还成功弥补了传统电商在临场感和体验感方面的不足&#xff0c;从而显著提升了消费者的购物体验。 51建模网为电商行业提供3D…

WebSocket 快速入门 与 应用

WebSocket 是一种在 Web 应用程序中实现实时、双向通信的技术。它允许客户端和服务器之间建立持久性的连接&#xff0c;以便可以在两者之间双向传输数据。 以下是 WebSocket 的一些关键特点和工作原理&#xff1a; 0.特点&#xff1a; 双向通信&#xff1a;WebSocket 允许服务…

[AI Stability] 开源AI新利器:Stable Diffusion 3 Medium震撼发布!文本到图像再升级!

Stable Diffusion 3 Medium(SD3) 开源了&#xff0c;我们来看下。 关键要点 Stable Diffusion 3 Medium 是 Stability AI 迄今为止最先进的文本到图像开源模型。该模型的体积小巧&#xff0c;非常适合在消费级 PC 和笔记本电脑上运行&#xff0c;也适合在企业级 GPU 上运行。…

产品经理简要介绍主流电商平台商品API接口的重要应用价值

主流电商平台&#xff1a; 截至目前&#xff08;2024年6月&#xff09;&#xff0c;主流电商平台通常指的是国内外知名的在线购物平台&#xff0c;包括但不限于以下几家&#xff1a; 1. 中国电商平台&#xff1a; - 淘宝网&#xff08;taobao.com&#xff09; - 天猫商…

端午消费数据:“下沉”“价跌”延续

端午假期消费延续了五一的“下沉”与“价跌”趋势。一是人均旅游支出的恢复程度&#xff0c;仍不及2019年&#xff08;恢复至89.5%&#xff09;&#xff1b;二是三线以下城市继续成为旅游新增长点&#xff0c;其平台订单热度高于部分一线和新一线城市。三是国内、国际机票酒店价…

自动同步库数据——kettle开发36

kettle中的那些人工智能。 一、kettle的AI能力目录 跨库同步 2.自动开发 3.自动优化 二、AI实例 1、跨库同步 sqlsever表同步至oracle数据库 1.1源库sqlserver 1.2目标库oracle 1.3可视化跨库同步 使用多表复制向导 选择跨库的表&#xff0c;下一步下一步&#xff0c;即可…

Javaweb避坑指北(持续更新)

内容较多可按CtrlF搜索 0.目录 1.获取插入数据后自增长主键的值 2.Controller中返回给ajax请求字符串/json会跳转到xxx.jsp 3.ajax请求获得的json无法解析 4.在Controller中使用ServletFileUpload获取的上传文件为null 5.莫名其妙报service和dao里方法的错误 6.ajax请求拿…

DeepSpeed Monitoring Comm. Logging

Monitoring 支持多种后端&#xff1a;Tensorboard、WandB、Comet、CSV文件&#xff1b; TensorBoard例子&#xff1a; 自动监控&#xff1a;DeepSpeed自动把重要metric记录下来。只需在配置文件里enable相应的看板后端即可&#xff1a; {"tensorboard": {"enabl…

Linux-笔记 全志平台OTG虚拟 串口、网口、U盘笔记

前言&#xff1a; 此文章方法适用于全志通用平台&#xff0c;并且三种虚拟功能同一时间只能使用一个&#xff0c;原因是此3种功能都是内核USB Gadget precomposed configurations的其中一个选项&#xff0c;只能单选&#xff0c;不能多选&#xff0c;而且不能通过修改配置文件去…

湖北省七普分乡、镇、街道数据,shp、excel格式 需要自取

数据名称: 湖北省七普分乡、镇、街道数据 数据格式: Shp、excel 数据几何类型: 面 数据坐标系: WGS84 数据时间&#xff1a;2020年 数据来源&#xff1a;网络公开数据 数据可视化.

Vue27-内置指令04:v-once指令

一、需求 二、v-once指令 获取初始值&#xff1a; 三、小结

VBA实战(Excel)(6):报错处理

前言 在运行VBA的过程中会有一些报错&#xff0c;例如类型不匹配等运行错误。On Error错误处理程序能直接有效的解决报错。但是当程序逻辑复杂时&#xff0c; 使用On Error会掩盖其他的未知错误&#xff0c;所以应尽可能想办法排除错误&#xff0c;或在已知功能范围内使用。 …

[NCTF 2018]flask真香

打开题目后没有提示框&#xff0c;尝试扫描后也没有什么结果&#xff0c;猜想是ssti。所以尝试寻找ssti的注入点并判断模版。 模版判断方式&#xff1a; 在url地址中输入{7*7} 后发现不能识别执行。 尝试{{7*7}} ,执行成功&#xff0c;继续往下走注入{{7*7}}&#xff0c;如果执…

【设计模式深度剖析】【7】【行为型】【观察者模式】

&#x1f448;️上一篇:中介者模式 设计模式-专栏&#x1f448;️ 文章目录 观察者模式英文原文直译如何理解&#xff1f; 观察者模式的角色类图代码示例 观察者模式的应用观察者模式的优点观察者模式的缺点观察者模式的使用场景 观察者模式 观察者模式&#xff08;Observer…

俄罗斯发迹史:起源于莫斯科公国,沙俄时期获取大量地盘

俄罗斯是一个不可战胜的民族&#xff1f;这句话的根据主要来自法国的拿破仑和德国的希特勒。 没办法&#xff0c;这两位带领的大军&#xff0c;基本上横扫了整个欧洲大陆&#xff0c;可每次进军俄国的领土&#xff0c;都是灰头土脸地回去的。因此俄罗斯便留下了“战斗民族”、…

敏捷项目管理工具排行榜:打造高效敏捷开发环境的利器

最常见的敏捷项目管理工具包括&#xff1a;Leangoo领歌、Trello、Asana、ClickUp等 在敏捷开发的世界里&#xff0c;项目管理工具如同指挥棒&#xff0c;引领着团队快速响应变化&#xff0c;持续交付价值。介绍几款业内领先的敏捷项目管理工具&#xff0c;帮组大家选择最适合自…

Jmeter接口请求之 :multipart/form-data 参数请求

参考教程 Jmeter压测之&#xff1a;multipart/form-data_jmeter form-data-CSDN博客 1、通过fiddler对接口进行抓取&#xff0c;接口信息如下图所示 2、获取到接口后 在fiddler右侧点击Inspectors-Raw中可以看到如下图所示信息&#xff0c;上半部分为默认请求头信息内容&#…