Simple message top, simple message top
First look
A message list, click item, and then refresh the time. After you click "TOP", the time is refreshed. The top setting rules are sorted by two fields.
If it is in the top state, top is 1, and then the time will be refreshed for each operation. time is the timestamp of storage. First, let's look at the object class.
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;/*** pin time **/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;}/*** compare by time **/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 ;} /** determine whether to pin to the top */int result = 0-(top-chatBean. getTop (); if (result = 0) {/** sort by time */result = 0-compareToTime (time, chatBean. getTime () ;}return result ;}}View Code
Then you need to write some auxiliary methods in the adapter to facilitate the ui Layer call. In fact, there are three methods. One function is to click the time to refresh the time field, one is the top setting method to set the top state and refresh time, and the other is sorting when adding data.
Then it is easy to call the interface to return data.
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 April /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) {if (bean. getTop () = 1) {holder. root. setBackgroundResource (R. color. color_EEEEEE); holder. top. setText ("unpin");} else {holder. root. setBackgroundResource (R. color. white); holder. top. setText ("pin");} holder. TV _title.setText ("00" + bean. getId () + "no."); holder. TV _content.setText ("Hello everyone, I am 00" + bean. getId () + ", my top is" + 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. aggregate () {@ Override public void aggregate (SwipeItemLayout swipeItemLayout) {}@ Override public void aggregate (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 );}}}View Code
Here I have added a control character, topPosition. Because the clicking time and sliding deletion listening are no longer the same, you need to record the operation you clicked, this will also prevent you from clicking the next button before the top is reached, leading to index disorder.
GitHub: https://github.com/1024477951/FragmentApp