RecyclerView.Adapter

Stella981
• 阅读 730

刚刚逛了一下github看到的,觉得很赞 所以记录分享一下 原文地址:https://github.com/nkzawa/socket.io-android-chat

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;


public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {

    private List<Message> mMessages;
    private int[] mUsernameColors;

    public MessageAdapter(Context context, List<Message> messages) {
        mMessages = messages;
        mUsernameColors = context.getResources().getIntArray(R.array.username_colors);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int layout = -1;
        switch (viewType) {
        case Message.TYPE_MESSAGE:
            layout = R.layout.item_message;
            break;
        case Message.TYPE_LOG:
            layout = R.layout.item_log;
            break;
        case Message.TYPE_ACTION:
            layout = R.layout.item_action;
            break;
        }
        View v = LayoutInflater
                .from(parent.getContext())
                .inflate(layout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        Message message = mMessages.get(position);
        viewHolder.setMessage(message.getMessage());
        viewHolder.setUsername(message.getUsername());
    }

    @Override
    public int getItemCount() {
        return mMessages.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mMessages.get(position).getType();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mUsernameView;
        private TextView mMessageView;

        public ViewHolder(View itemView) {
            super(itemView);

            mUsernameView = (TextView) itemView.findViewById(R.id.username);
            mMessageView = (TextView) itemView.findViewById(R.id.message);
        }

        public void setUsername(String username) {
            if (null == mUsernameView) return;
            mUsernameView.setText(username);
            mUsernameView.setTextColor(getUsernameColor(username));
        }

        public void setMessage(String message) {
            if (null == mMessageView) return;
            mMessageView.setText(message);
        }

        private int getUsernameColor(String username) {
            int hash = 7;
            for (int i = 0, len = username.length(); i < len; i++) {
                hash = username.codePointAt(i) + (hash << 5) - hash;
            }
            int index = Math.abs(hash % mUsernameColors.length);
            return mUsernameColors[index];
        }
    }
}
点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
uni
uniappVideoGitHub地址:https://github.com/Tzlibai/uniappvideo(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2FTzlibai%2Funiappvideo)
Easter79 Easter79
3年前
vue echarts vue
1、git地址https://github.com/ecomfe/vueecharts(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fecomfe%2Fvueecharts)2、使用(1)安装npminstallvueecha
Stella981 Stella981
3年前
Armbian编译以及定制
Armbian项目地址Github: https://github.com/armbian/build(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Farmbian%2Fbuild)ArmbianforTVBox项目地址Github: h
Stella981 Stella981
3年前
React Native 0.58 正式版中文更新日志
原文地址:https://github.com/reactnativecommunity/reactnativereleases/blob/master/CHANGELOG.md0580(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Freactna
Stella981 Stella981
3年前
Redis 5种数据结构及对应使用场景(面试必修课)
本文收录在GitHub地址https://github.com/chengxynds/SpringbootNotebook(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fchengxynds%2FSpringbootNotebook)
Stella981 Stella981
3年前
EasyExcel使用记录
GITHUB地址:https://github.com/alibaba/easyexcel(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Falibaba%2Feasyexcel)1.POM文件添加依赖:<dependency          
Wesley13 Wesley13
3年前
Swoole源码学习记录(十一)——Worker,Connection
Swoole版本:1.7.5stableGithub地址:https://github.com/LinkedDestiny/swoolesrcanalysis(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2
Stella981 Stella981
3年前
LMJDropdownMenu
LMJDropdownMenugithub地址:https://github.com/JerryLMJ/LMJDropdownMenu(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2FJerryLMJ%2FLMJDro
Stella981 Stella981
3年前
Android 输入校验库简介及使用教程
NextInputs项目地址:https://github.com/yoojia/NextInputs(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fyoojia%2FNextInputs)Android扩展项目地址:https://github
Stella981 Stella981
3年前
Android启动页面实现版本检查更新
1、引入模块CheckUpdateLibrary的github地址如下:https://github.com/qiangxi/CheckUpdateLibrary(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2Fqiangxi%2FCheckUpdate