




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(fā)技術】怎么在android中仿微信好友列表功能
這期內容當中在下將會給大家?guī)碛嘘P怎么在android中仿微信好友列表功能,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。(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
{
//參考網(wǎng)址:/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好友列表適配器,在這里設置顯示的用戶名和頭像,并且添加點擊事件
ContactAdapterpublic
class
ContactAdapter
extends
RecyclerView.Adapter<RecyclerView.ViewHolder>{
private
LayoutInflater
mLayoutInflater;
private
Context
mContext;
private
String[]
mContactNames;
//
聯(lián)系人名稱字符串數(shù)組
private
List<String>
mContactList;
//
聯(lián)系人名稱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,
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
?
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)DividerItemDecorationpublic
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
<
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,
top,
right,
bottom);
mDivider.draw(c);
}
}
@Override
public
void
getItemOffsets(Rect
outRect,
int
itemPosition,
RecyclerView
parent)
{
if
(mOrientation
==
VERTICAL_LIST)
{
outRect.set(0,
0,
0,
mDivider.getIntrinsicHeight());
}
else
{
outRect.set(0,
0,
mDivider.getIntrinsicWidth(),
0);
}
}
//
@Override
//
public
void
getItemOffsets(Rect
outRect,
View
view,
RecyclerView
parent,
RecyclerView.State
state)
{
//
if
(mOrientation
==
VERTICAL_LIST){
//
outRect.set(0,0,0,mDivider.getIntrinsicHeight());
//
}else
{
//
outRect.set(0,0,mDivider.getIntrinsicWidth(),0);
//
}
//
}
}(6)LetterViewpublic
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)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 元旦節(jié)親子活動策劃方案
- 光明行動活動策劃方案
- 2025至2030年中國竹片炭行業(yè)投資前景及策略咨詢報告
- 2025至2030年中國鹽漬大蘿卜行業(yè)投資前景及策略咨詢報告
- 兔年拔蘿卜活動方案
- 黨建五一活動策劃方案
- 零售庫存抵押典當貸款協(xié)議
- 黨日活動西點活動方案
- 茶場設施租賃與茶葉種植管理承包協(xié)議
- 黨校端午活動方案
- 2025-2030年可調節(jié)高度臺球桿行業(yè)跨境出海戰(zhàn)略研究報告
- 歡樂購物街第2課時 買賣我做主(說課稿)-2024-2025學年 一年級數(shù)學下冊人教版
- 合作成果與未來展望模板
- 初中生物2021年初專題周練-血液循環(huán)訓練題(一)【含詳解】
- BMS電池管理系統(tǒng)
- 四川省成都市(2024年-2025年小學六年級語文)部編版小升初模擬(上學期)試卷及答案
- 智能樓宇管理員題庫含答案
- SCMP練習試卷附答案(一)
- 江蘇省蘇州市(2024年-2025年小學六年級語文)部編版小升初真題(下學期)試卷及答案
- 證據(jù)法學復習資料
- 【MOOC】人格與精神障礙-學做自己的心理醫(yī)生-暨南大學 中國大學慕課MOOC答案
評論
0/150
提交評論