社交性質的APP中勋又,應該都有用戶换帜。用戶信息展示地方的不同惯驼,會產生很多的樣式。用戶包含很多的信息祟牲、功能说贝。用戶需要展示頭像、名字乡恕,有些需要點擊以后跳轉到用戶詳情傲宜,有些還可以關注用戶,關注成功后其他頁面需要刷新關注狀態(tài)蛋哭。但是樣式的不同也使得展示的信息不同谆趾,有可能這個頁面需要關注按鈕,另一個地方卻只需要一個頭像沪蓬。這就使得碎片化變得很嚴重跷叉,如果要增加有些邏輯就有可能會漏地方营搅,并且數(shù)據(jù)設置也不夠統(tǒng)一梆砸、布局需要多處findviewById。接下來我會通過自己的方法來盡量避免這些問題休蟹,如果有不對的地方請指正日矫。
AUTH 聲明了各種權限哪轿,比如(是否顯示頭像、用戶名窃诉,點擊后是否進入個人主頁飘痛,是否實現(xiàn)關注功能)
M 暫時只是拿來放數(shù)據(jù)
V 實現(xiàn)findviewbyid,以及各種的setText setImage
P 實現(xiàn)onClickListener
實現(xiàn)的功能
1 設置用戶的頭像辅搬、名稱脖旱;
2 在RecyclerView中點擊后跳轉到個人主頁,在個人主頁則不需要跳轉溶褪;
3 實現(xiàn)關注用戶的功能践险;
4 登錄后隱藏自己個人主頁的關注按鈕;
class UserInfoAuth {
public static final int ICON = 1 << 0;
public static final int NAME = 1 << 1;
public static final int CLICK = 1 << 2;
public static final int FOLLOW = 1 << 3;
public static final int FOLLOW_DISPEAR = 1 << 4;
}
class UserInfoM {
private int auth;
private UserBean;
UserInfoM(int auth) {
this.auth = auth;
}
public int getAuth() {
return auth;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
public UserBean getUserBean() {
return userBean;
}
}
class UserBean {
private String name;
private String icon;
private boolean attention;
public void setName(String name) {
this.name = name;
}
public void getName() {
return name;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getIcon() {
return icon;
}
public void setAttention(boolean attention) {
this.attention = attention;
}
public boolean isAttention() {
return attention;
}
}
class UserInfoV {
TextView nameTV, followTV;
ImageView imageView;
public void bindView(int auth, View rootView, OnclickListener listener) {
if (checkAuth(UserInfoAuth.ICON)) {
imageView = (ImageView) rootView.findViewById(R.id.image_view)
}
if (checkAuth(UserInfoAuth.NAME)) {
nameTV = (ImageView) rootView.findViewById(R.id.name_TV)
}
if (checkAuth(UserInfoAuth.CLICK)) {
rootView.setOnclickListener(listener);
}
if (checkAuth(UserInfoAuth.FOLLOW) {
followTV.setOnClickListener(listener);
}
}
public boolean checkAuth(int auth, int check) {
return (auth & check) != 0;
}
}
class UserInfoP implements {
UserInfoM userInfoM;
UserInfoV userInfoV;
UserInfoP(int auth) {
userInfoM = new UserInfoM(auth);
userInfoV = new UserInfoV();
}
public void bindView(View rootView) {
userInfoV.bindView(rootView, userInfoM.getAuth(), this);
}
public void setData(UserBean userBean) {
userInfoV.setData(userBean);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
//gotoDetail
break;
}
}
}
demo 地址
https://github.com/Billxxxx/BankSMSReader/tree/develop