ListView的item设置置顶
1.activity
public class TopListViewActivity extends Activity {
private static int ON_TOP = 1;
private static int CANCEL_TOP = 0;
public static String TOP_STATES = "TOP";
private ListView mListView;
private List
private SessionItemAdapter itemAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_listview);
initView();
registerListener();
}
private void registerListener() {
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> parent, View view, final int position, long id) {
final Session session = (Session) parent.getItemAtPosition(position);
Bundle bundle = new Bundle();
bundle.putInt(TOP_STATES, session.getTop());
PopupDialogFragment popupDialog = new PopupDialogFragment();
popupDialog.setArguments(bundle);
popupDialog.setItemOnClickListener(new PopupDialogFragment.DialogItemOnClickListener() {
@Override
public void onTop() {
//置顶
session.setTop(ON_TOP);
session.setTime(System.currentTimeMillis());
refreshView();
}
@Override
public void onCancel() {
//取消
session.setTop(CANCEL_TOP);
refreshView();
}
});
popupDialog.show(getFragmentManager(), "POPUP");
return true;
}
});
}
private void initView() {
mListView = (ListView) findViewById(R.id.lv);
sessionList = new ArrayList<>();
//简单实现Item数据
TypedArray iconArray = getResources().obtainTypedArray(R.array.icon_array);
for (int i = 0; i < 8; i++) {
Session session = new Session();
session.setAvatar(iconArray.getResourceId(i,R.mipmap.ic_launcher));
sessionList.add(session);
}
itemAdapter = new SessionItemAdapter(this);
mListView.setAdapter(itemAdapter);
refreshView();
iconArray.recycle();
}
private void refreshView() {
//如果不调用sort方法,是不会进行排序的,也就不会调用compareTo
Collections.sort(sessionList);
itemAdapter.updateData(sessionList);
}
}
2.fragment
public class PopupDialogFragment extends DialogFragment {
private DialogItemOnClickListener itemOnClickListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.popview, null);
TextView onTopTv = (TextView) view.findViewById(R.id.on_top_tv);
TextView cancelTv = (TextView) view.findViewById(R.id.cancel_top_tv);
Bundle bundle = getArguments();
int isTop = bundle.getInt(TopListViewActivity.TOP_STATES);
//判断是否已经置顶
if (isTop == 1) {
onTopTv.setVisibility(View.GONE);
cancelTv.setVisibility(View.VISIBLE);
} else if (isTop == 0) {
onTopTv.setVisibility(View.VISIBLE);
cancelTv.setVisibility(View.GONE);
}
onTopTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
itemOnClickListener.onTop();
}
});
cancelTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
itemOnClickListener.onCancel();
}
});
getDialog().getWindow().requestFeature(STYLE_NO_TITLE);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Light);
setCancelable(true);
getDialog().getWindow().setBackgroundDrawableResource(R.color.write_bg);
return view;
}
public void setItemOnClickListener(DialogItemOnClickListener itemOnClickListener) {
this.itemOnClickListener = itemOnClickListener;
}
public interface DialogItemOnClickListener {
void onTop();
void onCancel();
}
}
3.itemview.xml
android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bg" android:orientation="horizontal"> android:id="@+id/avatar_img" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center_vertical" /> android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="30dp" android:text="hello word" android:textSize="20sp" />
4.attrs
5.popview
android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@color/write_bg" > android:id="@+id/on_top_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:padding="15dp" android:text="选择置顶" android:textSize="20sp" android:layout_gravity="center_horizontal" /> android:layout_width="match_parent" android:layout_height="0.4dp" android:background="@color/common_line_color"/> android:id="@+id/cancel_top_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:padding="15dp" android:text="取消置顶" android:textSize="20sp" android:layout_gravity="center_horizontal" />
6.session
package com.antquenn.demos.bean;
import java.io.Serializable;
import java.util.Calendar;
/**
* Created by ant_quenn on 2020/11/10.
*/
public class Session implements Serializable, Comparable {
/**
* 是否置顶
*/
public int top;
/**
* 置顶时间
**/
public long time;
/**
* 头像
*/
public int avatar;
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public int getTop() {
return top;
}
public void setTop(int top) {
this.top = top;
}
public int getAvatar() {
return avatar;
}
public void setAvatar(int avatar) {
this.avatar = avatar;
}
@Override
public int compareTo(Object another) {
if (another == null || !(another instanceof Session)) {
return -1;
}
Session otherSession = (Session) another;
/**置顶判断 ArrayAdapter是按照升序从上到下排序的,就是默认的自然排序
* 如果是相等的情况下返回0,包括都置顶或者都不置顶,返回0的情况下要
* 再做判断,拿它们置顶时间进行判断
* 如果是不相等的情况下,otherSession是置顶的,则当前session是非置顶的,
* 应该在otherSession下面,所以返回1
* 同样,session是置顶的,则当前otherSession是非置顶的,
* 应该在otherSession上面,所以返回-1
* */
int result = 0 - (top - otherSession.getTop());
if (result == 0) {
result = 0 - compareToTime(time, otherSession.getTime());
}
return result;
}
/**
* 根据时间对比
* */
public static int compareToTime(long lhs, long rhs) {
Calendar cLhs = Calendar.getInstance();
Calendar cRhs = Calendar.getInstance();
cLhs.setTimeInMillis(lhs);
cRhs.setTimeInMillis(rhs);
return cLhs.compareTo(cRhs);
}
}