private void setAvatarUrlAndShow(AndroidData mAndroidData) { List<Android> androidList = mAndroidData.getData(); final int size = androidList.size(); final List<AndroidWrapper> wrapperList = new ArrayList<AndroidWrapper>(size); for (int i = 0; i < size; i++) { final Android android = androidList.get(i); String url = android.getUrl(); if (url.contains("https://github.com/")) { setGitHubAvatar(url, android, wrapperList, size); } else if (url.contains("http://www.jianshu.com")) { setJianShuAvatar(url, android, wrapperList, size); } else if (url.contains("http://android.jobbole.com")) { setJobboleAvatar(url, android, wrapperList, size); } else { AndroidWrapper wrapper = new AndroidWrapper(android, null); wrapperList.add(wrapper); } } }
@Override public void onBindViewHolder(GankHolder holder, int position) { Android android = mAndroidWrappers.get(position).getAndroid(); String author = android.getWho() == null ? "无名好汉" : android.getWho(); holder.author.setText(author); holder.title.setText(android.getDesc()); setOnClickListener(holder.itemView, android.getUrl()); // 给每篇干货 设置标签 setTag(holder.tagGroup, android.getDesc(), android.getUrl()); // 作者的头像 setAvatar(holder, mAndroidWrappers.get(position)); }
private void setGitHubAvatar( String url, final Android android, final List<AndroidWrapper> wrapperList, final int size) { String author; int start = url.indexOf("/", 19); if (start == -1) { author = url.substring(19); } else { author = url.substring(19, start); } android.setWho(author); mGitHubService .getAvatar(author, GitHubService.clientID, GitHubService.clientSecret) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( new Subscriber<GitUser>() { @Override public void onCompleted() { if (wrapperList.size() == size) { mGankView.showMore(wrapperList); mGankView.finishRefresh(); } } @Override public void onError(Throwable e) { Log.e("TAG", e.toString()); } @Override public void onNext(GitUser gitUser) { AndroidWrapper wrapper = new AndroidWrapper(android, gitUser.getImageUrl()); wrapperList.add(wrapper); } }); }