一共有两个比较复杂的地方
1. 列表需要自动解析首字母,并且按照字母排序,并且兼容特殊字符2. 侧边的字母导航栏
中文转拼音
从中文转拼音可以使用 pinyin4j implementation 'com.github.open-android:pinyin4j:2.5.0'
初始化
private var format = HanyuPinyinOutputFormat().apply {
caseType = HanyuPinyinCaseType.UPPERCASE // 输出拼音全部大写
toneType = HanyuPinyinToneType.WITHOUT_TONE // 不带声调
vCharType = HanyuPinyinVCharType.WITH_V
}
使用
/**
* 根据中国字,获取首字母大写拼音,如果不符合中国字,则根据条件返回
* 中 -> Z
* ❤ -> #
* a -> A
*/
private fun getFirstLatter(chinese: Char): String
{
return when
{
chinese.toString().matches("[\u4e00-\u9fa5]+".toRegex()) ->
{
val firstPinyin = PinyinHelper.toHanyuPinyinStringArray(chinese, format)?.get(0)?: chinese.toString()
firstPinyin[0].toString()
}
isEngWorld(chinese) -> chinese.toString().toUpperCase()
else -> "#"
}
}
判断是否常见的字符,中文,或者英文
/**
* 判断是否常见的字符,中文,或者英文
*/
fun isNormalWorld(world: Char): Boolean
{
return (world.toString().matches("[\u4e00-\u9fa5]+".toRegex()) || world.toString().matches("[a-z]+".toRegex()) || world.toString().matches("[A-Z]+".toRegex()))
}
判断字符是不是英文
/** * 判断字符是不是英文 */
fun isEngWorld(world: Char): Boolean{
return (world.toString().matches("[a-z]+".toRegex()) || world.toString().matches("[A-Z]+".toRegex()))
}
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.teccyc.module_main.Beans.CycleBrandBean
import com.teccyc.module_main.R
import com.xiaolei.library.Base.BaseAdapter
import com.xiaolei.library.Base.BaseHolder
import com.xiaolei.library.Exts.gone
import com.xiaolei.library.Exts.show
import net.sourceforge.pinyin4j.PinyinHelper
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType
import java.util.*
class CycleBrandAdapter : BaseAdapter<CycleBrandBean>()
{
var isSearch = false
set
private val newList = ArrayList<CycleBrandBean>()
private var format = HanyuPinyinOutputFormat().apply {
caseType = HanyuPinyinCaseType.UPPERCASE // 输出拼音全部大写
toneType = HanyuPinyinToneType.WITHOUT_TONE // 不带声调
vCharType = HanyuPinyinVCharType.WITH_V
}
override fun onCreate(parent: ViewGroup, viewType: Int): BaseHolder
{
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.item_cycle_brand_list, parent, false)
return BaseHolder(view)
}
override fun onBindView(holder: BaseHolder, data: CycleBrandBean, position: Int)
{
val first_letter = holder.get<TextView>(R.id.first_letter)
val name_layout = holder.get<View>(R.id.name_layout)
val name_text = holder.get<TextView>(R.id.name_text)
val check_view = holder.get<View>(R.id.check_view)
first_letter.gone()
name_layout.gone()
if (data.isWord)
{
first_letter.show()
first_letter.text = data.name
} else
{
name_layout.show()
name_text.text = data.name
if (data.cheked)
{
check_view.setBackgroundResource(R.drawable.icon_cycle_brand_check)
} else
{
check_view.setBackgroundColor(Color.TRANSPARENT)
}
}
}
override fun notifyDataSetChanged()
{
list.removeAll { it.name.isEmpty() }
list.sortWith(Comparator { o1, o2 ->
val o1_first = o1.name[0]
val o2_first = o2.name[0]
val firstIsNormal = isNormalWorld(o1_first)
val secendIsNormal = isNormalWorld(o2_first)
val result = if (firstIsNormal && !secendIsNormal)
{
-1
} else if (!firstIsNormal && secendIsNormal)
{
1
} else if (!firstIsNormal && !secendIsNormal)
{
0
} else
{
val isFirstEng = isEngWorld(o1_first)
val isSecondEng = isEngWorld(o2_first)
val firstLatter = if (isFirstEng)
{
getFirstLatter(o1_first.toString().toUpperCase()[0])[0]
} else
{
getFirstLatter(o1_first)[0]
}
val secondLatter = if (isSecondEng)
{
getFirstLatter(o2_first.toString().toUpperCase()[0])[0]
} else
{
getFirstLatter(o2_first)[0]
}
if (firstLatter == secondLatter)
{
if (isFirstEng && !isSecondEng)
{
-1
} else if (!isFirstEng && isSecondEng)
{
1
} else
{
firstLatter - secondLatter
}
} else
{
firstLatter - secondLatter
}
}
result
})
if (!isSearch)
{
list.forEach { bean ->
if (!bean.isWord)
{
val firstLatter = getFirstLatter(bean.name[0])
if (!newList.any { it.name == firstLatter && it.isWord })
{
newList.add(CycleBrandBean(firstLatter, true))
}
newList.add(bean)
}
}
list.clear()
list.addAll(newList)
newList.clear()
}
super.notifyDataSetChanged()
}
/**
* 根据中国字,获取首字母大写拼音,如果不符合中国字,则根据条件返回
* 中 -> Z
* ❤ -> #
* a -> A
*/
private fun getFirstLatter(chinese: Char): String
{
return when
{
chinese.toString().matches("[\u4e00-\u9fa5]+".toRegex()) ->
{
val firstPinyin = PinyinHelper.toHanyuPinyinStringArray(chinese, format)?.get(0) ?: chinese.toString()
firstPinyin[0].toString()
}
isEngWorld(chinese) -> chinese.toString().toUpperCase()
else -> "#"
}
}
/**
* 判断是否常见的字符,中文,或者英文
*/
fun isNormalWorld(world: Char): Boolean
{
return (world.toString().matches("[\u4e00-\u9fa5]+".toRegex()) | world.toString().matches("[a-z]+".toRegex()) || world.toString().matches("[A-Z]+".toRegex()))
}
/**
* 判断字符是不是英文
*/
fun isEngWorld(world: Char): Boolean
{
return (world.toString().matches("[a-z]+".toRegex()) || world.toString().matches("[A-Z]+".toRegex()))
}
}