Commit adbfb9d6 authored by shiyl's avatar shiyl

搜索类目开发完成

parent 1745a1f7
package com.yidian.common
class XEventConfig {
companion object{
companion object {
const val OPEN_GALLERY = "openGallery"
const val OPEN_PUSH = "openPush"
const val UPDATE_FLUTTER_PUSH_STATUS = "updateFlutterPushStatus"
......@@ -16,5 +16,8 @@ class XEventConfig {
const val GIF_DATA: String = "gifData"
const val CHOOSE_CAREER: String = "chooseCareer"
const val LIFE_ACCOUNT_NAME_MODIFY = "lifeAccountNameModify"
// 在搜索页面选择成功关闭当前页面
const val CATEGORY_SEARCH_SUCCESS = "category_search_success"
}
}
......@@ -29,6 +29,13 @@ abstract class BaseViewModel : ViewModel() {
_goNextEvent.value = null
}
/**
* 跳转到指定的导航目的地
*/
fun postNextEvent() {
_goNextEvent.value = Unit
}
/**
* 跳转到指定的导航目的地
*/
......
package com.yidian.common.utils
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import java.util.regex.Pattern
/**
* 字符串相关的工具类
*/
object StringUtils {
/**
* 筛选关键字并设置变色
*/
fun matcherSearchText(color: Int, text: String, keyword: String): SpannableStringBuilder {
val ss = SpannableStringBuilder(text)
val matcher = Pattern.compile(keyword).matcher(ss)
while (matcher.find()) {
val start = matcher.start()
val end = matcher.end()
ss.setSpan(ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
return ss
}
interface RichClickListener {
fun onClick()
}
/**
* 筛选关键字并设置颜色和点击事件
*/
fun setRichText(textView: TextView, content: String, target:String, color: Int, listener: RichClickListener) {
val stringBuilder = SpannableStringBuilder(content)
val matcher = Pattern.compile(target).matcher(content)
while (matcher.find()) {
val start = matcher.start()
val end = matcher.end()
stringBuilder.setSpan(object : ClickableSpan() {
override fun onClick(widget: View) {
listener.onClick()
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
ds.color = color
}
}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
textView.text = stringBuilder
textView.movementMethod = LinkMovementMethod.getInstance()
}
}
......@@ -4,5 +4,8 @@
<application>
<activity android:name=".ui.coupon.ChooseCategoryActivity" />
<activity
android:name=".ui.coupon.CategorySearchActivity"
android:windowSoftInputMode="adjustPan" />
</application>
</manifest>
\ No newline at end of file
package com.yidian.shenghuoquan.commodity.ui.coupon
import android.graphics.Color
import android.widget.TextView
import androidx.activity.viewModels
import com.yidian.common.XEventConfig
import com.yidian.common.extensions.initTitleBar
import com.yidian.common.mvvm.BaseMvvmActivity
import com.yidian.common.services.XEventService
import com.yidian.common.utils.StringUtils
import com.yidian.common.utils.ToastUtils
import com.yidian.shenghuoquan.commodity.R
import com.yidian.shenghuoquan.commodity.databinding.CommodityActivityCategorySearchBinding
import com.yidian.shenghuoquan.commodity.dto.SelectCategoryDto
import com.yidian.shenghuoquan.commodity.ui.coupon.adapter.CategorySearchListAdapter
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xarc.xevent.XEventManager
/**
* 搜索类目
*/
class CategorySearchActivity : BaseMvvmActivity<CommodityActivityCategorySearchBinding, CategorySearchViewModel>() {
private val adapter = CategorySearchListAdapter()
override val layoutId: Int = R.layout.commodity_activity_category_search
override val vm: CategorySearchViewModel by viewModels()
override fun initView() {
initTitleBar(binding.include.toolbar, binding.include.tvTitle, "选择类目")
setupRecyclerView()
subscribeUiEvent()
}
private fun setupRecyclerView() {
binding.rvCategoryList.adapter = adapter
adapter.setEmptyView(R.layout.empty_view)
adapter.setOnItemClickListener { adapter, _, position ->
val selectCategoryDto = adapter.getItem(position) as SelectCategoryDto
// 关闭选择类目页面
XEventManager.post(XBaseEvent(XEventConfig.CATEGORY_SEARCH_SUCCESS, null))
// todo 选择成功后,回传H5
ToastUtils.showShortSafe("选择${selectCategoryDto.firstCategoryName} > ${selectCategoryDto.secondCategoryName}")
}
}
private fun subscribeUiEvent() {
vm.sendKeywordEvent.observe(this, {
adapter.setKeyword(it)
})
vm.searchSuccessEvent.observe(this, {
if (it.isNullOrEmpty()) {
adapter.setList(null)
} else {
adapter.setList(it)
}
})
vm.clearEditEvent.observe(this, {
binding.etSearchKeyword.setText("")
})
}
}
package com.yidian.shenghuoquan.commodity.ui.coupon
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.yidian.common.mvvm.BaseViewModel
import com.yidian.common.utils.ToastUtils
import com.yidian.shenghuoquan.commodity.dto.CategoryListDto
import com.yidian.shenghuoquan.commodity.dto.SelectCategoryDto
import com.yidian.shenghuoquan.commodity.http.CouponDataSource
import com.yidian.shenghuoquan.commodity.http.callback.ICategoryListCallback
class CategorySearchViewModel : BaseViewModel() {
var searchKeyword = MutableLiveData<String?>()
/**
* 清空输入框
*/
private val _clearEditEvent = MutableLiveData<Unit>()
val clearEditEvent: LiveData<Unit> = _clearEditEvent
/**
* 发送搜索关键字
*/
private val _sendKeywordEvent = MutableLiveData<String>()
val sendKeywordEvent: LiveData<String> = _sendKeywordEvent
/**
* 搜索成功 Event
*/
private val _searchSuccessEvent = MutableLiveData<MutableList<SelectCategoryDto>?>()
val searchSuccessEvent: LiveData<MutableList<SelectCategoryDto>?> = _searchSuccessEvent
fun toSearch() {
if (searchKeyword.value.isNullOrBlank()) {
ToastUtils.showShortSafe("请输入关键词搜索商品类目")
return
}
requestCategoryList()
}
/**
* 请求类目列表
*/
private fun requestCategoryList() {
val paramsMap = HashMap<String, String?>()
paramsMap["category_name"] = searchKeyword.value
CouponDataSource.getCategoryList(requestCategoryListCallBack, paramsMap)
}
private val requestCategoryListCallBack = object : ICategoryListCallback {
override fun getCategoryListSuccess(dto: MutableList<CategoryListDto>?) {
if (dto.isNullOrEmpty()) {
_searchSuccessEvent.value = null
} else {
// 向adapter中传入搜索关键字
_sendKeywordEvent.value = searchKeyword.value
// 转换列表
val resultList = mutableListOf<SelectCategoryDto>()
dto.forEachIndexed { index, categoryListDto ->
if (!dto[index].sub_list.isNullOrEmpty()) {
dto[index].sub_list.forEach {
val selectCategoryDto = SelectCategoryDto(categoryListDto.category_id, categoryListDto.name, it.category_id, it.name)
resultList.add(selectCategoryDto)
}
}
}
_searchSuccessEvent.value = resultList
}
}
}
fun toClear() {
_clearEditEvent.value = Unit
}
}
package com.yidian.shenghuoquan.commodity.ui.coupon
import android.content.Intent
import androidx.activity.viewModels
import com.yidian.common.XEventConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.common.extensions.initTitleBar
import com.yidian.common.mvvm.BaseMvvmActivity
......@@ -11,7 +13,11 @@ import com.yidian.shenghuoquan.commodity.dto.CategoryListDto
import com.yidian.shenghuoquan.commodity.dto.SubCategory
import com.yidian.shenghuoquan.commodity.ui.coupon.adapter.CategoryListAdapter
import com.yidian.shenghuoquan.commodity.ui.coupon.adapter.CategorySubListAdapter
import com.yidian.xarc.xevent.XBaseEvent
import com.yidian.xarc.xevent.XEventManager
import com.yidian.xpage.XPageViewProtocol
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
/**
......@@ -32,6 +38,7 @@ class ChooseCategoryActivity : BaseMvvmActivity<CommodityActivityChooseCategoryB
}
override fun initView() {
XEventManager.register(this)
initTitleBar(binding.include.toolbar, binding.include.tvTitle, "选择类目")
setupRecyclerView()
subscribeUiEvent()
......@@ -75,6 +82,24 @@ class ChooseCategoryActivity : BaseMvvmActivity<CommodityActivityChooseCategoryB
vm.showSelectFirstLevelName()
}
})
vm.goNextEvent.observe(this, {
startActivity(Intent(this, CategorySearchActivity::class.java))
})
}
@Subscribe(sticky = false, threadMode = ThreadMode.MAIN)
fun onEvent(event: XBaseEvent?) {
when (event?.name) {
// 在搜索页面选择成功关闭当前页面
XEventConfig.CATEGORY_SEARCH_SUCCESS -> {
this.finish()
}
}
}
override fun onDestroy() {
super.onDestroy()
XEventManager.unRegister(this)
}
}
......@@ -45,7 +45,7 @@ class ChooseCategoryViewModel : BaseViewModel() {
}
fun gotoSearch() {
ToastUtils.showShortSafe("类目搜索")
postNextEvent()
}
fun showSelectFirstLevelName(name: String = "") {
......
package com.yidian.shenghuoquan.commodity.ui.coupon.adapter
import android.graphics.Color
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yidian.common.utils.StringUtils
import com.yidian.shenghuoquan.commodity.R
import com.yidian.shenghuoquan.commodity.dto.SelectCategoryDto
/**
* 类目搜索列表适配器
*/
class CategorySearchListAdapter : BaseQuickAdapter<SelectCategoryDto, BaseViewHolder>(R.layout.commodity_layout_category_search_list_item) {
private var keyword: String = ""
/**
* 设置搜索关键字
*/
fun setKeyword(str: String) {
keyword = str
}
override fun convert(holder: BaseViewHolder, item: SelectCategoryDto) {
holder.getView<TextView>(R.id.tv_first_category_name).text = item.firstCategoryName
val matcherSearchText = if (keyword.isNotBlank()) {
StringUtils.matcherSearchText(Color.parseColor("#1852F1"), item.secondCategoryName, keyword)
} else {
item.secondCategoryName
}
holder.getView<TextView>(R.id.tv_second_category_name).text = matcherSearchText
}
}
......@@ -24,6 +24,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.coupon.CategorySearchActivity"
android:windowSoftInputMode="adjustPan" />
</application>
</manifest>
\ No newline at end of file
<?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">
<data>
<variable
name="vm"
type="com.yidian.shenghuoquan.commodity.ui.coupon.CategorySearchViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<include
android:id="@+id/include"
layout="@layout/layout_common_toolbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp60"
android:gravity="center_vertical"
android:orientation="horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="@dimen/dp40"
android:layout_marginStart="@dimen/dp20"
android:layout_marginTop="@dimen/dp10"
android:layout_marginBottom="@dimen/dp10"
android:layout_weight="1"
android:background="@drawable/shape_gray_bg">
<ImageView
android:id="@+id/iv_search"
android:layout_width="@dimen/dp24"
android:layout_height="@dimen/dp24"
android:layout_marginStart="@dimen/dp10"
android:src="@drawable/icon_search"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_search_keyword"
android:layout_width="0dp"
android:layout_height="@dimen/dp40"
android:background="@null"
android:hint="请输入关键词搜索商品类目"
android:text="@={vm.searchKeyword}"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp16"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/iv_clear"
app:layout_constraintStart_toEndOf="@id/iv_search"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_clear"
android:layout_width="@dimen/dp24"
android:layout_height="@dimen/dp24"
android:layout_marginStart="@dimen/dp10"
android:layout_marginEnd="@dimen/dp12"
android:onClick="@{() -> vm.toClear()}"
android:src="@drawable/icon_clear"
app:isShow="@{!vm.searchKeyword.empty}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp8"
android:onClick="@{() -> vm.toSearch()}"
android:padding="@dimen/dp12"
android:text="搜索"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp14" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_category_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@null"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="@layout/commodity_layout_category_search_list_item" />
</LinearLayout>
</layout>
\ No newline at end of file
......@@ -22,7 +22,7 @@
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/sp40"
android:layout_height="@dimen/dp40"
android:layout_marginStart="@dimen/dp20"
android:layout_marginTop="@dimen/dp10"
android:layout_marginEnd="@dimen/dp20"
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="@dimen/dp60"
android:paddingStart="@dimen/dp20"
android:paddingEnd="@dimen/dp20">
<TextView
android:id="@+id/tv_first_category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp16"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="服务" />
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="@dimen/dp24"
android:layout_height="@dimen/dp24"
android:src="@drawable/icon_next_arrow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/tv_first_category_name"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_second_category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/color_333333"
android:textSize="@dimen/sp16"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/iv_arrow"
app:layout_constraintTop_toTopOf="parent"
tools:text="你好nj" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -133,11 +133,6 @@ class LoginLifeCircleActivity : BaseActivity<ActivityLoginBinding>(), IGetLifeAc
urlMap[ProtocolActivity.ProtocolUrlKey] = AppConfig.privacyPolicyStatement
XPageManager.push(XRouterPathConstants.PROTOCOL, urlMap)
}
// todo 测试代码
viewBind.tvCategory.clickAntiShake {
XPageManager.push(XRouterPathConstants.chooseCategoryActivity, null)
}
}
private fun changeLoginButtonStatus() {
......
......@@ -49,7 +49,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/dp10"
android:src="@mipmap/icon_clear"
android:src="@drawable/icon_clear"
android:visibility="gone" />
</LinearLayout>
......@@ -166,13 +166,4 @@
</LinearLayout>
<Button
android:id="@+id/tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="选择类目"
android:textColor="#1852f1"
android:textSize="14sp" />
</LinearLayout>
\ No newline at end of file
......@@ -118,7 +118,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/dp10"
android:src="@mipmap/icon_clear"
android:src="@drawable/icon_clear"
android:visibility="gone" />
</LinearLayout>
......
......@@ -71,7 +71,7 @@
android:layout_height="23dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:src="@mipmap/icon_clear"
android:src="@drawable/icon_clear"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
......
......@@ -33,7 +33,7 @@
android:id="@+id/iv_clear"
android:layout_width="23dp"
android:layout_height="23dp"
android:src="@mipmap/icon_clear"
android:src="@drawable/icon_clear"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@id/et_content"
app:layout_constraintEnd_toEndOf="parent"
......
......@@ -39,7 +39,7 @@
android:id="@+id/iv_clear"
android:layout_width="23dp"
android:layout_height="23dp"
android:src="@mipmap/icon_clear"
android:src="@drawable/icon_clear"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
......
......@@ -25,7 +25,7 @@
android:layout_height="23dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="2dp"
android:src="@mipmap/icon_clear"
android:src="@drawable/icon_clear"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="@id/iv_content"
app:layout_constraintTop_toTopOf="@id/iv_content" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment