简单的消息置顶
程序员文章站
2022-03-16 19:57:46
先看看效果图 一个消息列表,然后点击item后会刷新时间,点击置顶后也会刷新时间,置顶规则就是根据两个字段来排序 如果是置顶状态,top为1,然后每次操作都会刷新 time的时间,time是存的时间戳,先看看实体类 package com.fragmentapp.home.bean; import ......
先看看效果图
一个消息列表,然后点击item后会刷新时间,点击置顶后也会刷新时间,置顶规则就是根据两个字段来排序
如果是置顶状态,top为1,然后每次操作都会刷新 time的时间,time是存的时间戳,先看看实体类
package com.fragmentapp.home.bean; import android.support.annotation.NonNull; import java.util.Calendar; /** * Created by liuzhen on 2018/3/22. */ public class ChatBean implements Comparable<ChatBean> { private int id; private int top; /** * 置顶时间 **/ public long time; public int getTop() { return top; } public void setTop(int top) { this.top = top; } public long getTime() { return time; } public void setTime(long time) { this.time = time; } public int getId() { return id; } public void setId(int id) { this.id = id; } /** * 根据时间对比 * */ public int compareToTime(long lhs, long rhs) { Calendar cLhs = Calendar.getInstance(); Calendar cRhs = Calendar.getInstance(); cLhs.setTimeInMillis(lhs); cRhs.setTimeInMillis(rhs); return cLhs.compareTo(cRhs); } @Override public int compareTo(@NonNull ChatBean chatBean) { if (chatBean == null) { return -1; } /** 判断是否置顶 */ int result = 0 - (top - chatBean.getTop()); if (result == 0) { /** 按时间排序 */ result = 0 - compareToTime(time, chatBean.getTime()); } return result; } }
然后需要在adapter里面把一些辅助的方法写进去,方便ui层调用,其实就是三个方法,一个作用是点击时间刷新 time字段,一个是置顶方法 设置置顶状态和刷新时间,还有一个是添加数据的时候排序
然后就容易了,调接口返回数据
package com.fragmentapp.home.adapter; import android.view.View; import android.widget.TextView; import com.chad.library.adapter.base.BaseViewHolder; import com.fragmentapp.R; import com.fragmentapp.base.ArrayRecyclerAdapter; import com.fragmentapp.home.bean.ChatBean; import com.fragmentapp.view.remove.SwipeItemLayout; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Created by liuzhen on 2017/11/20. */ public class HomeAdapter extends ArrayRecyclerAdapter<ChatBean, HomeAdapter.ViewHolder> { private int topPosition = -1; public HomeAdapter(int layoutResId) { super(layoutResId); } @Override protected void convert(final ViewHolder holder, final ChatBean bean) { if (bean.getTop() == 1){ holder.root.setBackgroundResource(R.color.color_EEEEEE); holder.top.setText("取消置顶"); }else{ holder.root.setBackgroundResource(R.color.white); holder.top.setText("置顶"); } holder.tv_title.setText("00"+bean.getId()+"号"); holder.tv_content.setText("大家好,我是00"+bean.getId()+"号,我的top是"+bean.getTop()); holder.tv_time.setText(bean.getTime()+""); // GlideApp.with(AndroidApplication.getInstance().getApplicationContext()) // .load(item.path) // .skipMemoryCache(true) // .diskCacheStrategy(DiskCacheStrategy.NONE) // .centerCrop() // .transform(new RoundedCorners(10)) // .into(imageView); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { super.onBindViewHolder(holder, position); final ChatBean bean = getItem(position); holder.item_layout.setSwipeAble(true); holder.item_layout.setDelegate(new SwipeItemLayout.SwipeItemLayoutDelegate() { @Override public void onSwipeItemLayoutOpened(SwipeItemLayout swipeItemLayout) { } @Override public void onSwipeItemLayoutClosed(SwipeItemLayout swipeItemLayout) { if (topPosition >= 0) { if (bean.getTop() == 0){ bean.setTime(System.currentTimeMillis()); setTop(position,1); }else{ setTop(position,0); } topPosition = -1; } } @Override public void onSwipeItemLayoutStartOpen(SwipeItemLayout swipeItemLayout) { } }); holder.del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (topPosition == -1) { holder.item_layout.closeWithAnim(); } } }); holder.top.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (topPosition == -1) { holder.item_layout.closeWithAnim(); topPosition = position; } } }); } private void setTop(int position,int top){ mData.get(position).setTop(top); Collections.sort(mData); notifyDataSetChanged(); } public void click(int position){ mData.get(position).setTime(System.currentTimeMillis()); Collections.sort(mData); notifyDataSetChanged(); } public List<ChatBean> sortList(List<ChatBean> list){ Collections.sort(list); return list; } static class ViewHolder extends BaseViewHolder { TextView tv_title,tv_content,tv_time,tv_home,tv_read,del,top; SwipeItemLayout item_layout; View root; public ViewHolder(View view) { super(view); tv_title = getView(R.id.tv_title); tv_content = getView(R.id.tv_content); tv_time = getView(R.id.tv_time); tv_home = getView(R.id.tv_home); tv_read = getView(R.id.tv_read); item_layout = getView(R.id.item_layout); root = getView(R.id.root); del = getView(R.id.item_contact_delete); top = getView(R.id.item_contact_top); } } }
这里我给加了个控制符,topPosition,因为点击时间和滑动删除监听不再一起,所以要记录一下你点击的操作,这样也能防止你这个还没置顶就点下一个了,导致索引错乱