前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发之 仿微信通讯录 (一) 列表

Android开发之 仿微信通讯录 (一) 列表

作者头像
Xiaolei123
发布2019-03-20 11:25:31
2.5K0
发布2019-03-20 11:25:31
举报
文章被收录于专栏:肖蕾的博客肖蕾的博客

Android开发之 仿微信通讯录

一共有两个比较复杂的地方

代码语言:javascript
复制
1. 列表需要自动解析首字母,并且按照字母排序,并且兼容特殊字符2. 侧边的字母导航栏

1. 列表数据解析

中文转拼音

代码语言:javascript
复制
从中文转拼音可以使用 pinyin4j implementation 'com.github.open-android:pinyin4j:2.5.0'

初始化

代码语言:javascript
复制
private var format = HanyuPinyinOutputFormat().apply {      
  caseType = HanyuPinyinCaseType.UPPERCASE    // 输出拼音全部大写        
  toneType = HanyuPinyinToneType.WITHOUT_TONE // 不带声调        
  vCharType = HanyuPinyinVCharType.WITH_V    
}

使用

代码语言:javascript
复制
/** 
 * 根据中国字,获取首字母大写拼音,如果不符合中国字,则根据条件返回 
 * 中 -> 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 -> "#"    
  }
}

判断是否常见的字符,中文,或者英文

代码语言:javascript
复制
 /** 
  * 判断是否常见的字符,中文,或者英文 
  */
 fun isNormalWorld(world: Char): Boolean
 {  
   return (world.toString().matches("[\u4e00-\u9fa5]+".toRegex()) ||  world.toString().matches("[a-z]+".toRegex()) || world.toString().matches("[A-Z]+".toRegex()))
 }

判断字符是不是英文

代码语言:javascript
复制
/** * 判断字符是不是英文 */
fun isEngWorld(world: Char): Boolean{  
  return (world.toString().matches("[a-z]+".toRegex()) || world.toString().matches("[A-Z]+".toRegex()))
}

完整使用

代码语言:javascript
复制
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()))    
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-3-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Android开发之 仿微信通讯录
    • 1. 列表数据解析
      • 完整使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档