wangxiang 1 year ago
parent
commit
893ae902bf
  1. 103
      app/src/main/java/com/kanglai/push/ui/activity/InstantActivity.java
  2. 20
      app/src/main/java/com/kanglai/push/ui/adapter/MessageSwipeableRecyclerAdapter.java
  3. 9
      app/src/main/java/com/kanglai/push/ui/adapter/SoloChatRoomAdapter.java
  4. 10
      app/src/main/res/layout/item_swipeable_message.xml

103
app/src/main/java/com/kanglai/push/ui/activity/InstantActivity.java

@ -1,8 +1,11 @@
package com.kanglai.push.ui.activity; package com.kanglai.push.ui.activity;
import android.content.Context; import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.os.Bundle; import android.os.Bundle;
import android.text.InputFilter; import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import android.widget.EditText;
@ -15,6 +18,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.CacheDiskUtils; import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.StringUtils;
import com.dolphin.core.base.BaseActivity; import com.dolphin.core.base.BaseActivity;
import com.dolphin.core.bus.RxBus; import com.dolphin.core.bus.RxBus;
import com.dolphin.core.bus.RxSubscriptions; import com.dolphin.core.bus.RxSubscriptions;
@ -36,6 +40,8 @@ import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
@ -46,22 +52,17 @@ import io.reactivex.disposables.Disposable;
*/ */
public class InstantActivity extends BaseActivity<ActivityInstantBinding, InstantViewModel>{ public class InstantActivity extends BaseActivity<ActivityInstantBinding, InstantViewModel>{
private Disposable mSubscription;
public SoloChatRoomAdapter mAdapter; public SoloChatRoomAdapter mAdapter;
public RefreshLayout refreshLayout; public RefreshLayout refreshLayout;
public RecyclerView mRecyclerView; public RecyclerView mRecyclerView;
public TextView btn_send;
public EditText input_box; public EditText input_box;
public ImageView more_line; public ImageView more_line;
public ImageView more_fill; public ImageView more_fill;
public GridLayout bottom_grid; public GridLayout bottom_grid;
public LinearLayout layout_footer; public LinearLayout layout_footer;
public Boolean isShowBottom = false; // 底部功能模块显示开关,默认关闭 public Boolean isShowBottom = false;
public LocalPushChatMsg localPushChatMsg;
public List<PushChatMessage> list = new ArrayList<>(); // 聊天数据 private Disposable mSubscription;
public LocalPushChatMsg msgData; // 聊天对象数据
private User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User()); private User user = CacheDiskUtils.getInstance().getParcelable(CacheConstant.USER_INFO, User.CREATOR, new User());
@Override @Override
@ -77,17 +78,6 @@ public class InstantActivity extends BaseActivity<ActivityInstantBinding, Instan
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
/* 实时监听友盟传输的聊天数据 */
/* mSubscription = RxBus.getInstance().toObservableSticky(ChatMsg.class)
.compose(RxUtil.schedulersTransformer())
.compose(RxUtil.exceptionTransformer())
.subscribe(chat ->{
JSONObject jsonObject = new JSONObject(new Gson().toJson(chat));
// 收到友盟消息 查找最新的一条数据
if (jsonObject.getString("concernFanId").equals(mViewModel.adverseData.getId())) mViewModel.loadNewDate();
// LogUtils.d(jsonObject.getString("concernFanId"), mViewModel.adverseData.getId());
});
RxSubscriptions.add(mSubscription);*/
} }
@Override @Override
@ -95,35 +85,36 @@ public class InstantActivity extends BaseActivity<ActivityInstantBinding, Instan
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
super.mViewModel.mActivity = this; super.mViewModel.mActivity = this;
// 其他基础控件 // 其他基础控件
input_box = (EditText)findViewById(R.id.instant_input_box); input_box = findViewById(R.id.instant_input_box);
input_box.setFilters(new InputFilter[]{ViewFilterUtil.getNoSpecialCharactersFilter()}); // 去空格、换行符等特殊符号 input_box.setFilters(new InputFilter[]{ViewFilterUtil.getNoSpecialCharactersFilter()}); // 去空格、换行符等特殊符号
more_line = (ImageView)findViewById(R.id.iv_more_line); more_line = findViewById(R.id.iv_more_line);
more_fill = (ImageView)findViewById(R.id.iv_more_fill); more_fill = findViewById(R.id.iv_more_fill);
more_line.setOnClickListener(view -> changBottomType()); more_line.setOnClickListener(view -> changBottomType());
more_fill.setOnClickListener(view -> changBottomType()); more_fill.setOnClickListener(view -> changBottomType());
bottom_grid = (GridLayout) findViewById(R.id.instant_grid); // 隐藏的九宫格 bottom_grid = findViewById(R.id.instant_grid);
layout_footer = (LinearLayout) findViewById(R.id.layout_footer); // 底部消息输入框、更多、发送按钮 layout_footer = findViewById(R.id.layout_footer);
msgData = getIntent().getParcelableExtra("GOING_TO_INSTANT"); // 接收跳转过来的值 localPushChatMsg= getIntent().getParcelableExtra("GOING_TO_INSTANT");
if (msgData instanceof LocalPushChatMsg) { if (localPushChatMsg instanceof LocalPushChatMsg) {
mViewModel.adverseData = msgData; // 将对方基础数据存起来 mViewModel.adverseData = localPushChatMsg;
mViewModel.init(); // 加载一方聊天记录 mViewModel.init();
judgeViewType(msgData); judgeViewType(localPushChatMsg);
} }
// 一对一聊天室适配器初始化 mSubscription = RxBus.getInstance().toObservable(com.dolphin.umeng.entity.PushChatMessage.class)
// final SoloChatRoomAdapter soloChatRoomAdapter = new SoloChatRoomAdapter(CollectionUtils.newArrayList(), msgData); .compose(RxUtil.schedulersTransformer())
// mAdapter = soloChatRoomAdapter; .compose(RxUtil.exceptionTransformer())
// RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); // 初始化垂直适配器 .subscribe(msg -> {
// mRecyclerView = findViewById(R.id.instant_recycler); com.dolphin.umeng.entity.PushChatMessage pushChatMessage = (com.dolphin.umeng.entity.PushChatMessage) msg;
// mRecyclerView.setLayoutManager(mLayoutManager); PushChatMessage chatMessage = new PushChatMessage();
// mRecyclerView.setAdapter(mAdapter); chatMessage.setUserId(pushChatMessage.getFanUserId());
// chatMessage.setText(pushChatMessage.getText());
// refreshLayout = findViewById(R.id.instant_smart_refresh); // 刷新布局初始化 Calendar calendar = Calendar.getInstance();
// refreshLayout.setEnableRefresh(true); // 下拉刷新 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
// refreshLayout.setEnableLoadMore(false); // 上拉加载更多 String currentTime = sdf.format(calendar.getTime());
// refreshLayout.setEnableAutoLoadMore(true); // 开启使上拉加载具有弹性效果 chatMessage.setCreateTime(currentTime);
// refreshLayout.setEnableScrollContentWhenLoaded(true); // 加载完成时滚动列表显示新的内容 mAdapter.append(chatMessage);
// refreshLayout.setOnRefreshListener(v -> mViewModel.init()); });
RxSubscriptions.add(mSubscription);
} }
/** /**
@ -131,31 +122,30 @@ public class InstantActivity extends BaseActivity<ActivityInstantBinding, Instan
* @param requestList * @param requestList
*/ */
public void adapterInit(ArrayList<PushChatMessage> requestList) { public void adapterInit(ArrayList<PushChatMessage> requestList) {
final SoloChatRoomAdapter soloChatRoomAdapter = new SoloChatRoomAdapter(requestList, msgData); final SoloChatRoomAdapter soloChatRoomAdapter = new SoloChatRoomAdapter(requestList, localPushChatMsg);
mAdapter = soloChatRoomAdapter; mAdapter = soloChatRoomAdapter;
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); // 初始化垂直适配器 RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); // 初始化垂直适配器
mRecyclerView = findViewById(R.id.instant_recycler); mRecyclerView = findViewById(R.id.instant_recycler);
mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
refreshLayout = findViewById(R.id.instant_smart_refresh); // 刷新布局初始化 refreshLayout = findViewById(R.id.instant_smart_refresh);
refreshLayout.setEnableRefresh(true); // 下拉刷新 refreshLayout.setEnableRefresh(true);
refreshLayout.setEnableLoadMore(false); // 上拉加载更多 refreshLayout.setEnableLoadMore(false);
refreshLayout.setEnableAutoLoadMore(true); // 开启使上拉加载具有弹性效果 refreshLayout.setEnableAutoLoadMore(true);
refreshLayout.setEnableScrollContentWhenLoaded(true); // 加载完成时滚动列表显示新的内容 refreshLayout.setEnableScrollContentWhenLoaded(true);
refreshLayout.setOnRefreshListener(v -> mViewModel.loadMore()); refreshLayout.setOnRefreshListener(v -> mViewModel.loadMore());
refreshLayout.setDisableContentWhenRefresh(true); // 在刷新时候禁止操作内容视图 refreshLayout.setDisableContentWhenRefresh(true);
} }
/** /**
* 加载不同用户类型时的界面 * 加载不同用户类型时的界面
*
* app端不能给 系统/企业用户/企业内部/系统 用户发消息 * app端不能给 系统/企业用户/企业内部/系统 用户发消息
*/ */
private void judgeViewType(LocalPushChatMsg data) { private void judgeViewType(LocalPushChatMsg data) {
mViewModel.setTitleText(data.getUserName()); // 标题 mViewModel.setTitleText(data.getUserName()); // 标题
if ("2".equals(data.getUserType()) || "5".equals(data.getUserType()) || "0".equals(data.getUserType())){ if ("2".equals(data.getUserType()) || "5".equals(data.getUserType()) || "0".equals(data.getUserType())){
layout_footer.setVisibility(View.GONE); layout_footer.setVisibility(View.INVISIBLE);
} }
} }
@ -180,14 +170,13 @@ public class InstantActivity extends BaseActivity<ActivityInstantBinding, Instan
public void hideKeyboard() { public void hideKeyboard() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocusView = getCurrentFocus(); View currentFocusView = getCurrentFocus();
if (currentFocusView != null) imm.hideSoftInputFromWindow(currentFocusView.getWindowToken(), 0); imm.hideSoftInputFromWindow(Objects.requireNonNullElseGet(currentFocusView, () -> getWindow().getDecorView()).getWindowToken(), 0);
else imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
if (mAdapter != null) mAdapter.clear(); // 清空数据 RxSubscriptions.remove(mSubscription);
RxSubscriptions.remove(mSubscription); // 解除RX监听防止内存泄露 if (mAdapter != null) mAdapter.clear();
} }
} }

20
app/src/main/java/com/kanglai/push/ui/adapter/MessageSwipeableRecyclerAdapter.java

@ -68,11 +68,11 @@ public class MessageSwipeableRecyclerAdapter extends RecyclerView.Adapter<Messag
public ViewHolder(View v) { public ViewHolder(View v) {
super(v); super(v);
behindLayout = v.findViewById(R.id.message_layout_behind); behindLayout = v.findViewById(R.id.message_layout_behind);
hideBtn2 = v.findViewById(R.id.hide_btn_02); //hideBtn2 = v.findViewById(R.id.hide_btn_02);
hideBtn3 = v.findViewById(R.id.hide_btn_03); hideBtn3 = v.findViewById(R.id.hide_btn_03);
container = v.findViewById(R.id.message_layout_container); container = v.findViewById(R.id.message_layout_container);
image = v.findViewById(R.id.message_left_image); image = v.findViewById(R.id.message_left_image);
badge = v.findViewById(R.id.message_left_badge); //badge = v.findViewById(R.id.message_left_badge);
userName = v.findViewById(R.id.title_label01); userName = v.findViewById(R.id.title_label01);
days = v.findViewById(R.id.title_label02); days = v.findViewById(R.id.title_label02);
textContent = v.findViewById(R.id.title_label03); textContent = v.findViewById(R.id.title_label03);
@ -105,7 +105,7 @@ public class MessageSwipeableRecyclerAdapter extends RecyclerView.Adapter<Messag
private void setToRead(int position) { private void setToRead(int position) {
if (mEventListener != null) { if (mEventListener != null) {
mEventListener.setToRead(position); mEventListener.setToRead(position);
mItemList.get(position).setIsRead("0"); //mItemList.get(position).setIsRead("0");
mItemList.get(position).setPinned(false); mItemList.get(position).setPinned(false);
notifyItemChanged(position); notifyItemChanged(position);
} }
@ -131,16 +131,16 @@ public class MessageSwipeableRecyclerAdapter extends RecyclerView.Adapter<Messag
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final LocalPushChatMsg item = mItemList.get(position); final LocalPushChatMsg item = mItemList.get(position);
holder.setMaxLeftSwipeAmount(-0.44f); holder.setMaxLeftSwipeAmount(-0.22f);
holder.setMaxRightSwipeAmount(0); holder.setMaxRightSwipeAmount(0);
holder.setSwipeItemHorizontalSlideAmount(item.getPinned() ? -0.44f : 0); holder.setSwipeItemHorizontalSlideAmount(item.getPinned() ? -0.22f : 0);
/** 单项赋值 */ /** 单项赋值 */
holder.userName.setText(item.getUserName()); holder.userName.setText(item.getUserName());
holder.days.setText(item.getSendTime()); holder.days.setText(item.getSendTime());
holder.textContent.setText(item.getLastContent()); holder.textContent.setText(item.getLastContent());
String avatarUrl = item.getAvatar(); String avatarUrl = item.getAvatar();
if (!TextUtils.isEmpty(item.getIsRead())&&"1".equals(item.getIsRead())) holder.badge.setVisibility(View.VISIBLE); //if (!TextUtils.isEmpty(item.getIsRead())&&"1".equals(item.getIsRead())) holder.badge.setVisibility(View.VISIBLE);
/** 文字非空 以及 非url */ /** 文字非空 以及 非url */
if (!TextUtils.isEmpty(item.getAvatar()) && !Patterns.WEB_URL.matcher(avatarUrl).matches()) avatarUrl = BuildConfig.HOST_URL + avatarUrl.substring(1); if (!TextUtils.isEmpty(item.getAvatar()) && !Patterns.WEB_URL.matcher(avatarUrl).matches()) avatarUrl = BuildConfig.HOST_URL + avatarUrl.substring(1);
Glide.with(holder.itemView.getContext()) Glide.with(holder.itemView.getContext())
@ -156,8 +156,8 @@ public class MessageSwipeableRecyclerAdapter extends RecyclerView.Adapter<Messag
return false; return false;
}); });
holder.container.setOnClickListener(view -> onSwipeableViewContainerClick(view, item)); holder.container.setOnClickListener(view -> onSwipeableViewContainerClick(view, item));
// holder.hideBtn1.setOnClickListener(view -> onBtnSwipeable1Click(view)); //holder.hideBtn1.setOnClickListener(view -> onBtnSwipeable1Click(view));
holder.hideBtn2.setOnClickListener(view -> setToRead(position)); //holder.hideBtn2.setOnClickListener(view -> setToRead(position));
holder.hideBtn3.setOnClickListener(view -> setToRemove(item)); holder.hideBtn3.setOnClickListener(view -> setToRemove(item));
} }
@ -168,8 +168,8 @@ public class MessageSwipeableRecyclerAdapter extends RecyclerView.Adapter<Messag
@Override @Override
public int onGetSwipeReactionType(@NonNull ViewHolder holder, int position, int x, int y) { public int onGetSwipeReactionType(@NonNull ViewHolder holder, int position, int x, int y) {
final int tx = (int) (holder.getSwipeableContainerView().getTranslationX() + 0.44f); final int tx = (int) (holder.getSwipeableContainerView().getTranslationX() + 0.22f);
final int ty = (int) (holder.getSwipeableContainerView().getTranslationY() + 0.44f); final int ty = (int) (holder.getSwipeableContainerView().getTranslationY() + 0.22f);
final int left = holder.getSwipeableContainerView().getLeft() + tx; final int left = holder.getSwipeableContainerView().getLeft() + tx;
final int right = holder.getSwipeableContainerView().getRight() + tx; final int right = holder.getSwipeableContainerView().getRight() + tx;
final int top = holder.getSwipeableContainerView().getTop() + ty; final int top = holder.getSwipeableContainerView().getTop() + ty;

9
app/src/main/java/com/kanglai/push/ui/adapter/SoloChatRoomAdapter.java

@ -140,4 +140,13 @@ public class SoloChatRoomAdapter extends ChatRoomListRecyclerAdapter{
notifyDataSetChanged(); notifyDataSetChanged();
return this; return this;
} }
public SoloChatRoomAdapter append(PushChatMessage pushChatMessage){
mItemList.add(pushChatMessage);
notifyItemRangeInserted(mItemList.size() - 1, 1);
return this;
}
} }

10
app/src/main/res/layout/item_swipeable_message.xml

@ -7,13 +7,13 @@
<!--右侧隐藏按钮--> <!--右侧隐藏按钮-->
<LinearLayout <LinearLayout
android:id="@+id/message_layout_behind" android:id="@+id/message_layout_behind"
android:layout_width="@dimen/dp_160" android:layout_width="@dimen/dp_80"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_alignParentRight="true" android:layout_alignParentRight="true"
android:orientation="horizontal" android:orientation="horizontal"
android:visibility="gone" android:visibility="gone"
tools:visibility="visible"> tools:visibility="visible">
<TextView <!--<TextView
android:id="@+id/hide_btn_02" android:id="@+id/hide_btn_02"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -23,7 +23,7 @@
android:text="已 读" android:text="已 读"
android:textColor="@color/white" android:textColor="@color/white"
android:textSize="@dimen/sp_8" android:textSize="@dimen/sp_8"
android:textStyle="bold" /> android:textStyle="bold" />-->
<TextView <TextView
android:id="@+id/hide_btn_03" android:id="@+id/hide_btn_03"
android:layout_width="0dp" android:layout_width="0dp"
@ -60,7 +60,7 @@
android:layout_centerInParent="true" android:layout_centerInParent="true"
android:layout_marginStart="@dimen/dp_6" android:layout_marginStart="@dimen/dp_6"
android:src="@drawable/icon_links_avatar" /> android:src="@drawable/icon_links_avatar" />
<ImageView <!--<ImageView
android:id="@+id/message_left_badge" android:id="@+id/message_left_badge"
android:visibility="gone" android:visibility="gone"
android:layout_width="@dimen/dp_10" android:layout_width="@dimen/dp_10"
@ -68,7 +68,7 @@
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_alignTop="@id/message_left_image" android:layout_alignTop="@id/message_left_image"
android:layout_alignRight="@id/message_left_image" android:layout_alignRight="@id/message_left_image"
android:src="@drawable/icon_badge_red" /> android:src="@drawable/icon_badge_red" />-->
</RelativeLayout> </RelativeLayout>
<!--右侧用户名+文字+时间--> <!--右侧用户名+文字+时间-->
<LinearLayout <LinearLayout

Loading…
Cancel
Save