信息绑定MVP

2017-08-04  本文已影响0人  比尔王BillWang

社交性质的APP中,应该都有用户。用户信息展示地方的不同,会产生很多的样式。用户包含很多的信息、功能。用户需要展示头像、名字,有些需要点击以后跳转到用户详情,有些还可以关注用户,关注成功后其他页面需要刷新关注状态。但是样式的不同也使得展示的信息不同,有可能这个页面需要关注按钮,另一个地方却只需要一个头像。这就使得碎片化变得很严重,如果要增加有些逻辑就有可能会漏地方,并且数据设置也不够统一、布局需要多处findviewById。接下来我会通过自己的方法来尽量避免这些问题,如果有不对的地方请指正。

AUTH 声明了各种权限,比如(是否显示头像、用户名,点击后是否进入个人主页,是否实现关注功能)
M 暂时只是拿来放数据
V 实现findviewbyid,以及各种的setText setImage
P 实现onClickListener

实现的功能
1 设置用户的头像、名称;
2 在RecyclerView中点击后跳转到个人主页,在个人主页则不需要跳转;
3 实现关注用户的功能;
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

上一篇 下一篇

猜你喜欢

热点阅读