热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

android仿微信好友列表功能

这篇文章主要介绍了android仿微信好友列表功能,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下

android studio实现微信好友列表功能,注意有一个jar包我没有放上来,请大家到MainActivity中的那个网址里面下载即可,然后把pinyin4j-2.5.0.jar复制粘贴到项目的app/libs文件夹里面,然后clean项目就可以使用了

实现效果图:

(1)在build.gradle中引用第三方的类库

compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile files('libs/pinyin4j-2.5.0.jar')

(2)在MainActivity:

public class MainActivity extends AppCompatActivity {
  //参考网址:https://github.com/JanecineJohn/WeChatList
  private RecyclerView contactList;
  private String[] contactNames;
  private LinearLayoutManager layoutManager;
  private LetterView letterView;
  private ContactAdapter adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cOntactNames= new String[] {"安然","奥兹","德玛","张三丰", "郭靖", "黄蓉", "黄老邪", "赵敏", "123", "天山童姥", "任我行", "于万亭", "陈家洛", "韦小宝", "$6", "穆人清", "陈圆圆", "郭芙", "郭襄", "穆念慈", "东方不败", "梅超风", "林平之", "林远图", "灭绝师太", "段誉", "鸠摩智"};
    cOntactList= (RecyclerView) findViewById(R.id.contact_list);
    letterView = (LetterView) findViewById(R.id.letter_view);
    layoutManager = new LinearLayoutManager(this);
    adapter = new ContactAdapter(this, contactNames);
    contactList.setLayoutManager(layoutManager);
    contactList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    contactList.setAdapter(adapter);
    letterView.setCharacterListener(new LetterView.CharacterClickListener() {
      @Override
      public void clickCharacter(String character) {
        layoutManager.scrollToPositionWithOffset(adapter.getScrollPosition(character),0);
      }
      @Override
      public void clickArrow() {
        layoutManager.scrollToPositionWithOffset(0,0);
      }
    });
  }
}
(3)Contact类
public class Contact implements Serializable {
  private String mName;
  private int mType;
  public Contact(String name, int type) {
    mName = name;
    mType = type;
  }
  public String getmName() {
    return mName;
  }
  public int getmType() {
    return mType;
  }
}

(3)listview好友列表适配器,在这里设置显示的用户名和头像,并且添加点击事件  ContactAdapter

public class ContactAdapter extends RecyclerView.Adapter{
  private LayoutInflater mLayoutInflater;
  private Context mContext;
  private String[] mContactNames; // 联系人名称字符串数组
  private List mContactList; // 联系人名称List(转换成拼音)
  private List resultList; // 最终结果(包含分组的字母)
  private List characterList; // 字母List
  public enum ITEM_TYPE {
    ITEM_TYPE_CHARACTER,
    ITEM_TYPE_CONTACT
  }
  public ContactAdapter(Context context, String[] contactNames) {
    mCOntext= context;
    mLayoutInflater = LayoutInflater.from(context);
    mCOntactNames= contactNames;
    handleContact();
  }
  private void handleContact() {
    mCOntactList= new ArrayList<>();
    Map map = new HashMap<>();
    for (int i = 0; i ();
    characterList = new ArrayList<>();
    for (int i = 0; i = "A".hashCode() && character.hashCode() <= "Z".hashCode()) { // 是字母
          characterList.add(character);
          resultList.add(new Contact(character, ITEM_TYPE.ITEM_TYPE_CHARACTER.ordinal()));
        } else {
          if (!characterList.contains("#")) {
            characterList.add("#");
            resultList.add(new Contact("#", ITEM_TYPE.ITEM_TYPE_CHARACTER.ordinal()));
          }
        }
      }
      resultList.add(new Contact(map.get(name), ITEM_TYPE.ITEM_TYPE_CONTACT.ordinal()));
    }
  }
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == ITEM_TYPE.ITEM_TYPE_CHARACTER.ordinal()) {
      return new CharacterHolder(mLayoutInflater.inflate(R.layout.item_character, parent, false));
    } else {
      return new ContactHolder(mLayoutInflater.inflate(R.layout.item_contact, parent, false));
    }
  }
  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof CharacterHolder) {
      ((CharacterHolder) holder).mTextView.setText(resultList.get(position).getmName());
    } else if (holder instanceof ContactHolder) {
      ((ContactHolder) holder).mTextView.setText(resultList.get(position).getmName());
    }
  }
  @Override
  public int getItemViewType(int position) {
    return resultList.get(position).getmType();
  }
  @Override
  public int getItemCount() {
    return resultList == null &#63; 0 : resultList.size();
  }
  public class CharacterHolder extends RecyclerView.ViewHolder {
    TextView mTextView;
    CharacterHolder(View view) {
      super(view);
      mTextView = (TextView) view.findViewById(R.id.character);
    }
  }
  public class ContactHolder extends RecyclerView.ViewHolder {
    TextView mTextView;
    ContactHolder(View view) {
      super(view);
      mTextView = (TextView) view.findViewById(R.id.contact_name);
    }
  }
  public int getScrollPosition(String character) {
    if (characterList.contains(character)) {
      for (int i = 0; i 

(4)ContactComparator  做英文字母的判断

public class ContactComparator implements Comparator {
  @Override
  public int compare(String o1, String o2) {
    int c1 = (o1.charAt(0) + "").toUpperCase().hashCode();
    int c2 = (o2.charAt(0) + "").toUpperCase().hashCode();
    boolean c1Flag = (c1 <"A".hashCode() || c1 > "Z".hashCode()); // 不是字母
    boolean c2Flag = (c2 <"A".hashCode() || c2 > "Z".hashCode()); // 不是字母
    if (c1Flag && !c2Flag) {
      return 1;
    } else if (!c1Flag && c2Flag) {
      return -1;
    }
    return c1 - c2;
  }
}

(5)DividerItemDecoration

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
  private static final int[] ATTRS = new int[]{
      android.R.attr.listDivider
  };
  public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
  public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
  private Drawable mDivider;
  private int mOrientation;
  public DividerItemDecoration(Context context, int orientation) {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    setOrientation(orientation);
  }
  private void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
      throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
  }
  @Override
  public void onDraw(Canvas c, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
      drawVertical(c, parent);
    } else {
      drawHorizontal(c, parent);
    }
  }
//  @Override
//  public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//    //super.onDraw(c, parent, state);
//    if (mOrientation == VERTICAL_LIST) {
//      drawVertical(c, parent);
//    } else {
//      drawHorizontal(c, parent);
//    }
//  }
  public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();
    final int childCount = parent.getChildCount();
    for (int i = 0; i 

(6)LetterView

public class LetterView extends LinearLayout{
  private Context mContext;
  private CharacterClickListener mListener;
  public LetterView(Context context,AttributeSet attrs) {
    super(context, attrs);
    mCOntext= context;//接收传进来的上下文
    setOrientation(VERTICAL);
    initView();
  }
  private void initView(){
    addView(buildImageLayout());
    for (char i = 'A';i<='Z';i++){
      final String character = i + "";
      TextView tv = buildTextLayout(character);
      addView(tv);
    }
    addView(buildTextLayout("#"));
  }
  private TextView buildTextLayout(final String character){
    LinearLayout.LayoutParams layoutParams =
        new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1);
    TextView tv = new TextView(mContext);
    tv.setLayoutParams(layoutParams);
    tv.setGravity(Gravity.CENTER);
    tv.setClickable(true);
    tv.setText(character);
    tv.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        if (mListener != null){
          mListener.clickCharacter(character);
        }
      }
    });
    return tv;
  }
  private ImageView buildImageLayout() {
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
    ImageView iv = new ImageView(mContext);
    iv.setLayoutParams(layoutParams);
    iv.setBackgroundResource(R.mipmap.ic_launcher);
    iv.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mListener != null) {
          mListener.clickArrow();
        }
      }
    });
    return iv;
  }
  public void setCharacterListener(CharacterClickListener listener) {
    mListener = listener;
  }
  public interface CharacterClickListener {
    void clickCharacter(String character);
    void clickArrow();
  }
}

(7)Utils

public class Utils {
  public static String getPingYin(String inputString) {
    HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
    format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    format.setVCharType(HanyuPinyinVCharType.WITH_V);
    char[] input = inputString.trim().toCharArray();
    String output = "";
    try {
      for (char curchar : input) {
        if (java.lang.Character.toString(curchar).matches("[\\u4E00-\\u9FA5]+")) {
          String[] temp = PinyinHelper.toHanyuPinyinStringArray(curchar, format);
          output += temp[0];
        } else {
          output += java.lang.Character.toString(curchar);
        }
      }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
      e.printStackTrace();
    }
    return output;
  }
}

总结

以上所述是小编给大家介绍的android仿微信好友列表,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


推荐阅读
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 【Windows】实现微信双开或多开的方法及步骤详解
    本文介绍了在Windows系统下实现微信双开或多开的方法,通过安装微信电脑版、复制微信程序启动路径、修改文本文件为bat文件等步骤,实现同时登录两个或多个微信的效果。相比于使用虚拟机的方法,本方法更简单易行,适用于任何电脑,并且不会消耗过多系统资源。详细步骤和原理解释请参考本文内容。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • EPICS Archiver Appliance存储waveform记录的尝试及资源需求分析
    本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 学习笔记(34):第三阶段4.2.6:SpringCloud Config配置中心的应用与原理第三阶段4.2.6SpringCloud Config配置中心的应用与原理
    立即学习:https:edu.csdn.netcourseplay29983432482?utm_sourceblogtoedu配置中心得核心逻辑springcloudconfi ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
author-avatar
mR_woManh
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有