android仿微信好友列表功能

android仿微信好友列表功能,第1张

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

androID studio实现微信好友列表功能,注意有一个jar包我没有放上来,请大家到MainActivity中的那个网址里面下载即可,然后把pinyin4j-2.5.0.jar@H_502_2@复制粘贴到项目的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","天山童姥","任我行","于万亭","陈家洛","韦小宝","","穆人清","陈圆圆","郭芙","郭襄","穆念慈","东方不败","梅超风","林平之","林远图","灭绝师太","段誉","鸠摩智"};    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好友列表适配器,在这里设置显示的@R_301_6964@和头像,并且添加点击事件  ContactAdapter

public class ContactAdapter extends RecyclerVIEw.Adapter<RecyclerVIEw.VIEwHolder>{  private LayoutInflater mLayoutInflater;  private Context mContext;  private String[] mContactnames; // 联系人名称字符串数组  private List<String> mContactList; // 联系人名称List(转换成拼音)  private List<Contact> resultList; // 最终结果(包含分组的字母)  private List<String> 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<String,String> map = new HashMap<>();    for (int i = 0; i < mContactnames.length; i++) {      String pinyin = Utils.getPingYin(mContactnames[i]);      map.put(pinyin,mContactnames[i]);      mContactList.add(pinyin);    }    Collections.sort(mContactList,new ContactComparator());    resultList = new ArrayList<>();    characterList = new ArrayList<>();    for (int i = 0; i < mContactList.size(); i++) {      String name = mContactList.get(i);      String character = (name.charat(0) + "").toupperCase(Locale.ENGliSH);      if (!characterList.contains(character)) {        if (character.hashCode() >= "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,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 ? 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 < resultList.size(); i++) {        if (resultList.get(i).getmname().equals(character)) {          return i;        }      }    }    return -1; // -1不会滑动  }}

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

public class ContactComparator implements Comparator<String> {  @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,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 < childCount; i++) {      final VIEw child = parent.getChildAt(i);      final RecyclerVIEw.LayoutParams params = (RecyclerVIEw.LayoutParams) child          .getLayoutParams();      final int top = child.getBottom() + params.bottommargin +          Math.round(VIEwCompat.getTranslationY(child));      final int bottom = top + mdivIDer.getIntrinsicHeight();      mdivIDer.setBounds(left,top,right,bottom);      mdivIDer.draw(c);    }  }  public voID drawHorizontal(Canvas c,RecyclerVIEw parent) {    final int top = parent.getpaddingtop();    final int bottom = parent.getHeight() - parent.getpaddingBottom();    final int childCount = parent.getChildCount();    for (int i = 0; i < childCount; i++) {      final VIEw child = parent.getChildAt(i);      final RecyclerVIEw.LayoutParams params = (RecyclerVIEw.LayoutParams) child          .getLayoutParams();      final int left = child.getRight() + params.rightmargin +          Math.round(VIEwCompat.getTranslationX(child));      final int right = left + mdivIDer.getIntrinsicHeight();      mdivIDer.setBounds(left,bottom);      mdivIDer.draw(c);    }  }  @OverrIDe  public voID getItemOffsets(Rect outRect,int itemposition,RecyclerVIEw parent) {    if (mOrIEntation == VERTICAL_List) {      outRect.set(0,mdivIDer.getIntrinsicHeight());    } else {      outRect.set(0,mdivIDer.getIntrinsicWIDth(),0);    }  }//  @OverrIDe//  public voID getItemOffsets(Rect outRect,VIEw vIEw,RecyclerVIEw.State state) {//    if (mOrIEntation == VERTICAL_List){//      outRect.set(0,mdivIDer.getIntrinsicHeight());//    }else {//      outRect.set(0,0);//    }//  }}

(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仿微信好友列表,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

您可能感兴趣的文章:Android中使用listview实现qq/微信好友列表Android UI仿QQ好友列表分组悬浮效果Android仿QQ好友列表分组实现增删改及持久化Android仿QQ好友列表实现列表收缩与展开 总结

以上是内存溢出为你收集整理的android仿微信好友列表功能全部内容,希望文章能够帮你解决android仿微信好友列表功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142788.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)