package com.tejia.lijin.app.ui.message;
|
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.Color;
|
import android.graphics.Paint;
|
import android.graphics.Rect;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.text.Spannable;
|
import android.text.SpannableString;
|
import android.text.Spanned;
|
import android.text.style.ForegroundColorSpan;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
|
import com.ali.auth.third.core.util.StringUtil;
|
import com.bumptech.glide.Glide;
|
import com.tejia.lijin.app.util.JumpActivityUtil;
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.wpc.library.util.common.StringUtils;
|
import com.wpc.library.widget.ResizableImageView;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.entity.MainMessage;
|
import com.tejia.lijin.app.entity.MessageContent;
|
import com.tejia.lijin.app.entity.UserMessage;
|
import com.tejia.lijin.app.ui.dialog.AddZFBInfoDialog2;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
import com.tejia.lijin.app.util.GlideCircleTransform;
|
import com.tejia.lijin.app.util.GlideRoundTransform;
|
import com.tejia.lijin.app.util.RecordImageTextVideo;
|
import com.tejia.lijin.app.util.umengCustomEvent.MessageCustomEvent;
|
|
import java.util.Iterator;
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2017/12/5.
|
*/
|
|
public class MainMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
private RecyclerView mRecyclerView;
|
|
private List<MainMessage> mList;
|
private Context mContext;
|
|
private View VIEW_FOOTER;
|
private View VIEW_HEADER;
|
|
//Type
|
private int TYPE_HEADER = 1001;
|
private int TYPE_FOOTER = 1002;
|
private int TYPE_WELCOME = 1003;
|
private int TYPE_ACTIVITY = 1004;
|
private int TYPE_MESSAGE = 1005;
|
|
private DisplayImageOptions options;
|
|
private IDeleteMessageListener deleteMessageListener;
|
|
public MainMessageAdapter(Context context, List<MainMessage> list, IDeleteMessageListener deleteMessageListener) {
|
this.mList = list;
|
this.mContext = context;
|
this.options = new DisplayImageOptions.Builder()
|
.showImageForEmptyUri(R.drawable.ic_goods_default)
|
.showImageOnFail(R.drawable.ic_goods_default)
|
.showImageOnLoading(R.drawable.ic_goods_default)
|
.resetViewBeforeLoading(true).cacheInMemory(true)
|
.cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY)
|
.considerExifParams(true)
|
.displayer(new FadeInBitmapDisplayer(300)).build();
|
this.deleteMessageListener = deleteMessageListener;
|
}
|
|
@Override
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
if (viewType == TYPE_FOOTER) {
|
return new MessageHolder(VIEW_FOOTER, TYPE_WELCOME);
|
} else if (viewType == TYPE_HEADER) {
|
return new MessageHolder(VIEW_HEADER, TYPE_WELCOME);
|
} else if (viewType == TYPE_WELCOME) {
|
View view = LayoutInflater.from(mContext).inflate(R.layout.item_message_welcome, parent, false);
|
MessageHolder holder = new MessageHolder(view, TYPE_WELCOME);
|
return holder;
|
} else if (viewType == TYPE_ACTIVITY) {
|
View view = LayoutInflater.from(mContext).inflate(R.layout.item_message_simple_img, parent, false);
|
MessageHolder holder = new MessageHolder(view, TYPE_ACTIVITY);
|
return holder;
|
} else if (viewType == TYPE_MESSAGE) {
|
View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_message, parent, false);
|
MessageHolder holder = new MessageHolder(view, TYPE_MESSAGE);
|
return holder;
|
} else {
|
View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_message, parent, false);
|
MessageHolder holder = new MessageHolder(view, TYPE_MESSAGE);
|
return holder;
|
}
|
}
|
|
/**
|
* 计算内容占用屏幕的宽度
|
*
|
* @param content
|
* @return
|
*/
|
private static Paint pFont = null;
|
|
private int computeTextWidth(float textSize, String content) {
|
if (pFont == null)
|
pFont = new Paint();
|
pFont.setTextSize(textSize);
|
Rect rect = new Rect();
|
//返回包围整个字符串的最小的一个Rect区域
|
content = content + "-";//多算一个字的宽度
|
pFont.getTextBounds(content, 0, content.length(), rect);
|
return rect.width();
|
}
|
|
@Override
|
public void onBindViewHolder(RecyclerView.ViewHolder cHolder, final int position) {
|
MessageHolder holder = (MessageHolder) cHolder;
|
if (!isHeaderView(position) && !isFooterView(position)) {
|
final MainMessage info;
|
if (haveHeaderView()) {
|
info = mList.get(position - 1);
|
} else if (haveFooterView() && ((haveHeaderView() && position == mList.size() + 1) ||
|
!haveHeaderView() && position == mList.size())) {
|
return;
|
} else {
|
info = mList.get(position);
|
}
|
|
if (info.getWelcomeMsg() != null) {
|
holder.tv_title.setText(info.getWelcomeMsg().getTitle());
|
holder.tv_welcome_content.setText(info.getWelcomeMsg().getContent());
|
final SharedPreferences sp = mContext.getSharedPreferences("user", Context.MODE_PRIVATE);
|
holder.ll_novice.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
RecordImageTextVideo textVideo = new RecordImageTextVideo(mContext);
|
String url = textVideo.getvideoMoreUrl();
|
if (StringUtils.isEmpty(url)) {
|
url = textVideo.gettxtImgUtl();
|
}
|
Intent intent = new Intent(mContext, ShareBrowserActivity.class);
|
intent.putExtra("url", url);
|
mContext.startActivity(intent);
|
}
|
});
|
holder.ll_help_center.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
Intent intent = new Intent(mContext, ShareBrowserActivity.class);
|
intent.putExtra("title", "帮助中心");
|
intent.putExtra("url", sp.getString("help", ""));
|
mContext.startActivity(intent);
|
}
|
});
|
} else if (info.getSpecial() != null) {
|
Glide.with(mContext).load(info.getSpecial().getSubPicture()).into(holder.iv_type);
|
holder.tv_type.setText(info.getSpecial().getName());
|
holder.tv_time.setText(info.getSpecial().getCreatetime());
|
Glide.with(mContext).load(info.getSpecial().getPicture())
|
.transform(new GlideRoundTransform(mContext, 5)).into(holder.iv_goods_img1);
|
// holder.tv_coupon1.setText(info.getActivityInfo().get(0).getCouponAmount() + "元券");
|
holder.tv_goods_des.setText(info.getSpecial().getRemark());
|
holder.tv_goods_des.setVisibility(StringUtils.isEmpty(info.getSpecial().getRemark()) ? View.GONE : View.VISIBLE);
|
// holder.tv_coupon1.setVisibility(Integer.parseInt(info.getGoodsList().get(0).getCouponAmount())
|
// == 0 ? View.GONE : View.VISIBLE);
|
final MainMessage info1 = info;
|
holder.iv_goods_img1.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
MessageCustomEvent.msgGuessLike(mContext);
|
Intent intent = null;
|
if ((!StringUtils.isEmpty(info1.getSpecial().getJumpDetail().getActivity()))
|
&& info1.getSpecial().getParams() != null) {
|
try {
|
intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(info1.getSpecial().getJumpDetail().getActivity())));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
if (info1.getSpecial().getParams() != null) {
|
@SuppressWarnings("unchecked")
|
Iterator<String> its = info1.getSpecial().getParams().keys();
|
while (its.hasNext()) {
|
String key = its.next();
|
String value = info1.getSpecial().getParams().optString(key);
|
intent.putExtra(key, value);
|
}
|
}
|
mContext.startActivity(intent);
|
} else if (StringUtils.isEmpty(info1.getSpecial().getJumpDetail().getActivity())
|
&& info1.getSpecial().getParams() != null) {
|
intent = new Intent(mContext, ShareBrowserActivity.class);
|
|
if (info1.getSpecial().getParams() != null) {
|
@SuppressWarnings("unchecked")
|
Iterator<String> its = info1.getSpecial().getParams().keys();
|
while (its.hasNext()) {
|
String key = its.next();
|
String value = info1.getSpecial().getParams().optString(key);
|
intent.putExtra(key, value);
|
}
|
}
|
|
mContext.startActivity(intent);
|
}
|
}
|
});
|
} else {
|
UserMessage infoMsg;
|
if (haveHeaderView()) {
|
if (haveFooterView() && position == mList.size() + 1) {
|
return;
|
}
|
infoMsg = mList.get(position - 1).getUserMsg();
|
} else if (haveFooterView() && position == mList.size()) {
|
return;
|
} else {
|
infoMsg = mList.get(position).getUserMsg();
|
}
|
Glide.with(mContext).load(infoMsg.getIcon()).into(holder.iv_msg_type);
|
holder.tv_msg_type.setText(infoMsg.getTitle());
|
holder.tv_msg_time.setText(infoMsg.getTime());
|
holder.ll_content.removeAllViews();
|
//计算文字宽度
|
int titleLength = 0;
|
//获取标题字体大小
|
View item1 = LayoutInflater.from(mContext).inflate(R.layout.item_message_single_row, null);
|
TextView tvTitle = item1.findViewById(R.id.tv_title);
|
float textSize = tvTitle.getTextSize();
|
|
for (int i = 0; i < infoMsg.getContentItems().size(); i++) {
|
// int temp = info.getContentItems().get(i).getTitle().getContent().length();
|
// if (temp > titleLength) {
|
// titleLength = temp;
|
// }
|
int tempTitleLength = computeTextWidth(textSize, infoMsg.getContentItems().get(i).getTitle().getContent());
|
if (tempTitleLength > titleLength)
|
titleLength = tempTitleLength;
|
}
|
|
for (int i = 0; i < infoMsg.getContentItems().size(); i++) {
|
View item = LayoutInflater.from(mContext).inflate(R.layout.item_message_single_row, null);
|
TextView tv_title = item.findViewById(R.id.tv_title);
|
ImageView iv_portrait = item.findViewById(R.id.iv_portrait);
|
ImageView iv_input = item.findViewById(R.id.iv_input);
|
TextView tv_content = item.findViewById(R.id.tv_content);
|
|
// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DimenUtils.spToPixels(
|
// titleLength * 15, mContext), ViewGroup.LayoutParams.WRAP_CONTENT);
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(titleLength, ViewGroup.LayoutParams.WRAP_CONTENT);
|
params.leftMargin = DimenUtils.dip2px(mContext, 15);
|
tv_title.setLayoutParams(params);
|
|
MessageContent mContent = infoMsg.getContentItems().get(i);
|
tv_title.setTextColor(Color.parseColor(mContent.getTitle().getColor()));
|
tv_title.setText(mContent.getTitle().getContent());
|
|
if (mContent.getContent().size() > 0 && !StringUtils.isEmpty(mContent.getContent().get(0).getImg())) {
|
Glide.with(mContext).load(mContent.getContent().get(0).getImg())
|
.transform(new GlideCircleTransform(mContext)).into(iv_portrait);
|
iv_portrait.setVisibility(View.VISIBLE);
|
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
params1.leftMargin = 0;
|
tv_content.setLayoutParams(params1);
|
} else {
|
iv_portrait.setVisibility(View.GONE);
|
}
|
String content = "";
|
for (int j = 0; j < mContent.getContent().size(); j++) {
|
content += mContent.getContent().get(j).getContent();
|
}
|
Spannable span = new SpannableString(content);
|
String temp = "";
|
for (int k = 0; k < mContent.getContent().size(); k++) {
|
span.setSpan(new ForegroundColorSpan(Color.parseColor(mContent.getContent().get(k).getColor()))
|
, temp.length(), (temp + mContent.getContent().get(k).getContent()).length(),
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
temp += mContent.getContent().get(k).getContent();
|
}
|
tv_content.setText(span);
|
iv_input.setVisibility(mContent.isClick() ? View.VISIBLE : View.GONE);
|
holder.ll_content.addView(item);
|
}
|
|
final UserMessage cInfo = infoMsg;
|
holder.ll_item.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (cInfo.getJumpDetail() != null) {
|
Intent intent = null;
|
if ((!StringUtils.isEmpty(cInfo.getJumpDetail().getActivity()))
|
&& cInfo.getParams() != null) {
|
try {
|
intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(cInfo.getJumpDetail().getActivity())));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
if (cInfo.getParams() != null) {
|
@SuppressWarnings("unchecked")
|
Iterator<String> its = cInfo.getParams().keySet().iterator();
|
while (its.hasNext()) {
|
String key = its.next();
|
String value = cInfo.getParams().getString(key);
|
intent.putExtra(key, value);
|
}
|
}
|
mContext.startActivity(intent);
|
} else if (StringUtils.isEmpty(cInfo.getJumpDetail().getActivity())
|
&& cInfo.getParams() != null) {
|
intent = new Intent(mContext, ShareBrowserActivity.class);
|
|
if (cInfo.getParams() != null) {
|
@SuppressWarnings("unchecked")
|
Iterator<String> its = cInfo.getParams().keySet().iterator();
|
while (its.hasNext()) {
|
String key = its.next();
|
String value = cInfo.getParams().getString(key);
|
intent.putExtra(key, value);
|
}
|
}
|
|
mContext.startActivity(intent);
|
}
|
}
|
}
|
});
|
|
holder.ll_item.setOnLongClickListener(new View.OnLongClickListener() {
|
@Override
|
public boolean onLongClick(View v) {
|
if (!StringUtil.isEmpty(cInfo.getId())) {
|
AddZFBInfoDialog2.Builder builder = new AddZFBInfoDialog2.Builder(mContext);
|
builder.setTitle("删除提醒").setMessage("确定要删除本条消息?").setMessageTextAligin(AddZFBInfoDialog2.Builder.TEXT_ALIGIN_MIDDLE)
|
.setPositiveButton("确定删除", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialog, int which) {
|
if (deleteMessageListener != null)
|
deleteMessageListener.delete(cInfo);
|
dialog.dismiss();
|
}
|
}).create().show();
|
}
|
return false;
|
}
|
});
|
|
}
|
}
|
}
|
|
@Override
|
public int getItemCount() {
|
int count = (mList == null ? 0 : mList.size());
|
if (VIEW_FOOTER != null) {
|
count++;
|
}
|
|
if (VIEW_HEADER != null) {
|
count++;
|
}
|
return count;
|
}
|
|
@Override
|
public int getItemViewType(int position) {
|
if (isHeaderView(position)) {
|
return TYPE_HEADER;
|
} else if (isFooterView(position)) {
|
return TYPE_FOOTER;
|
} else {
|
if (mList.get(position - 1) == null)
|
return TYPE_HEADER;
|
if (mList.get(position - 1).getWelcomeMsg() != null) {
|
return TYPE_WELCOME;
|
} else if (mList.get(position - 1).getSpecial() != null) {
|
return TYPE_ACTIVITY;
|
} else if (mList.get(position - 1).getUserMsg() != null) {
|
return TYPE_MESSAGE;
|
} else {
|
return TYPE_MESSAGE;
|
}
|
}
|
}
|
|
@Override
|
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
|
try {
|
if (mRecyclerView == null && mRecyclerView != recyclerView) {
|
mRecyclerView = recyclerView;
|
}
|
ifGridLayoutManager();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
private View getLayout(int layoutId) {
|
return LayoutInflater.from(mContext).inflate(layoutId, null);
|
}
|
|
public void addHeaderView(View headerView) {
|
if (haveHeaderView()) {
|
throw new IllegalStateException("hearview has already exists!");
|
} else {
|
//避免出现宽度自适应
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
headerView.setLayoutParams(params);
|
VIEW_HEADER = headerView;
|
ifGridLayoutManager();
|
notifyItemInserted(0);
|
}
|
}
|
|
public void addFooterView(View footerView) {
|
if (haveFooterView()) {
|
throw new IllegalStateException("footerView has already exists!");
|
} else {
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
footerView.setLayoutParams(params);
|
VIEW_FOOTER = footerView;
|
ifGridLayoutManager();
|
notifyItemInserted(getItemCount() - 1);
|
}
|
}
|
|
private void ifGridLayoutManager() {
|
if (mRecyclerView == null) return;
|
final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
|
if (layoutManager instanceof GridLayoutManager) {
|
((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
@Override
|
public int getSpanSize(int position) {
|
if (isHeaderView(position) || isFooterView(position)) {
|
return ((GridLayoutManager) layoutManager).getSpanCount();
|
} else {
|
return 1;
|
}
|
}
|
});
|
}
|
}
|
|
private boolean haveHeaderView() {
|
return VIEW_HEADER != null;
|
}
|
|
public boolean haveFooterView() {
|
return VIEW_FOOTER != null;
|
}
|
|
private boolean isHeaderView(int position) {
|
return haveHeaderView() && position == 0;
|
}
|
|
private boolean isFooterView(int position) {
|
return haveFooterView() && position >= getItemCount() - 1;
|
}
|
|
|
public class MessageHolder extends RecyclerView.ViewHolder {
|
/*
|
新人欢迎
|
*/
|
TextView tv_title;
|
TextView tv_welcome_content;
|
LinearLayout ll_novice;
|
LinearLayout ll_help_center;
|
|
/*
|
推荐内容
|
*/
|
ImageView iv_type;
|
ResizableImageView iv_goods_img1;
|
TextView tv_type;
|
TextView tv_time;
|
// TextView tv_coupon1;
|
TextView tv_goods_des;
|
|
//消息详情
|
LinearLayout ll_content;
|
ImageView iv_msg_type;
|
TextView tv_msg_type;
|
TextView tv_msg_time;
|
LinearLayout ll_item;
|
|
public MessageHolder(View view, int type) {
|
super(view);
|
if (type == TYPE_WELCOME) {
|
tv_title = view.findViewById(R.id.tv_title);
|
tv_welcome_content = view.findViewById(R.id.tv_welcome_content);
|
ll_novice = view.findViewById(R.id.ll_novice);
|
ll_help_center = view.findViewById(R.id.ll_help_center);
|
} else if (type == TYPE_ACTIVITY) {
|
iv_type = view.findViewById(R.id.iv_type);
|
iv_goods_img1 = view.findViewById(R.id.iv_goods_img1);
|
tv_type = view.findViewById(R.id.tv_type);
|
tv_time = view.findViewById(R.id.tv_time);
|
// tv_coupon1 = view.findViewById(R.id.tv_coupon1);
|
tv_goods_des = view.findViewById(R.id.tv_goods_des);
|
} else {
|
ll_content = view.findViewById(R.id.ll_content);
|
iv_msg_type = view.findViewById(R.id.iv_msg_type);
|
tv_msg_type = view.findViewById(R.id.tv_msg_type);
|
tv_msg_time = view.findViewById(R.id.tv_msg_time);
|
ll_item = view.findViewById(R.id.ll_item);
|
}
|
}
|
}
|
}
|