kimi
2021-02-18 749a5510a9f014446a3cd6ba57b3cb0cc8148dc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */
 
package cn.wildfire.chat.kit.channel;
 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
import java.util.List;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cn.wildfire.chat.kit.channel.viewholder.CategoryViewHolder;
import cn.wildfire.chat.kit.channel.viewholder.ChannelViewHolder;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfirechat.model.ChannelInfo;
 
public class ChannelListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ChannelInfo> createdChannels;
    private List<ChannelInfo> followedChannels;
    private OnChannelClickListener onChannelClickListener;
 
    public void setCreatedChannels(List<ChannelInfo> createdChannels) {
        this.createdChannels = createdChannels;
    }
 
    public void setFollowedChannels(List<ChannelInfo> followedChannels) {
        this.followedChannels = followedChannels;
    }
 
    public void setOnChannelClickListener(OnChannelClickListener listener) {
        this.onChannelClickListener = listener;
    }
 
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == R.layout.channel_item_category) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channel_item_category, parent, false);
            return new CategoryViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channel_item, parent, false);
            RecyclerView.ViewHolder holder = new ChannelViewHolder(view);
            view.setOnClickListener(v -> {
                if (onChannelClickListener != null) {
 
                    int position = holder.getAdapterPosition();
                    if (createdChannels == null || createdChannels.isEmpty()) {
                        onChannelClickListener.onChannelClick(followedChannels.get(position - 2));
                    } else {
                        if (position > createdChannels.size()) {
                            onChannelClickListener.onChannelClick(followedChannels.get(position - 2 - createdChannels.size()));
                        } else {
                            onChannelClickListener.onChannelClick(createdChannels.get(position - 1));
                        }
                    }
                }
            });
            return holder;
        }
    }
 
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == R.layout.channel_item_category) {
            if (position == 0) {
                ((CategoryViewHolder) holder).bind("我创建的频道");
            } else {
                ((CategoryViewHolder) holder).bind("我订阅的频道");
            }
        } else {
            if (createdChannels == null || createdChannels.isEmpty()) {
                ((ChannelViewHolder) holder).bind(followedChannels.get(position - 2));
            } else {
                if (position > createdChannels.size()) {
                    ((ChannelViewHolder) holder).bind(followedChannels.get(position - 2 - createdChannels.size()));
                } else {
                    ((ChannelViewHolder) holder).bind(createdChannels.get(position - 1));
                }
            }
        }
    }
 
 
    @Override
    public int getItemCount() {
        return 2 + (createdChannels == null ? 0 : createdChannels.size()) + (followedChannels == null ? 0 : followedChannels.size());
    }
 
    @Override
    public int getItemViewType(int position) {
        if (createdChannels == null || createdChannels.isEmpty()) {
            int type;
            switch (position) {
                case 0:
                case 1:
                    type = R.layout.channel_item_category;
                    break;
                default:
                    type = R.layout.channel_item;
                    break;
            }
            return type;
        } else {
            if (position == 0 || position == createdChannels.size() + 1) {
                return R.layout.channel_item_category;
            } else {
                return R.layout.channel_item;
            }
        }
    }
 
    public interface OnChannelClickListener {
        void onChannelClick(ChannelInfo channelInfo);
    }
}