android listview item置頂,ListView的item置頂

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 sessionList;

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

@mipmap/ic_ac_unit_black

@mipmap/ic_airport_shuttle_black

@mipmap/ic_all_inclusive_black

@mipmap/ic_beach_access_black

@mipmap/ic_casino_black

@mipmap/ic_child_care_black

@mipmap/ic_child_friendly_black

@mipmap/ic_free_breakfast_black

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);

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281527.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281527.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281527.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

電信運營商x86服務器定制策略

近幾年&#xff0c;伴隨云計算、大數據、移動互聯網、物聯網等新技術從概念到實踐&#xff0c;業務、用戶數據海量式爆發增長&#xff0c;作為承載這些業務發展的服務器行業也面臨巨大的機遇和挑戰。一方面&#xff0c;需求的爆發式增長大幅提升了市場空間&#xff0c;尤其是大…

企業數字化轉型服務+方案

前言&#xff1a;本文的閱讀對象是公司老板、或企業高層管理者&#xff01;1【背景介紹】數字經濟與實體經濟深度融合是助推我國經濟高質量發展的重要環節。為加快數字中國建設&#xff0c;中央和地方政府都出臺各類政策扶植數字化轉型相關項目。馬云在接受采訪時也提到&#x…

在 MongoDB 上模擬事務操作來實現支付

我們的產品叫「學海密探」&#xff0c;屬于在線教育行業&#xff0c;產品需要有支付功能&#xff0c;然而支付最蛋疼是什么&#xff1f;有人會說是支付寶和微信等支付接口的接入開發&#xff01;沒錯&#xff0c;但支付接口的開發算是比較簡單的了&#xff0c;我覺得凡是跟錢有…

Zabbix server is not running

問題描述 Dashbord中提示Zabbix server is not running 分析過程 首先查看zabbix-server的運行狀態 systemctl status zabbix-server 確定是否是active(running)狀態&#xff0c;如果不是&#xff0c;重啟zabbix-server查看log tail -f /var/log/zabbix/zabbix_server.log 導致…

android 把異常寫入到文本里,嘗試在Android中將文件寫入sdcard時發生FileNotFoundException(權限被拒絕)...

從標題中可以看到&#xff0c;我在Android中將文件寫入sdcard時遇到問題.我想寫入將在sdcard上的公共空間中的文件&#xff0c;以便任何其他應用程序都可以讀取它。首先&#xff0c;我檢查sdcard是否已安裝&#xff1a;Environment.getExternalStorageState();然后&#xff0c;…

.NET性能優化-使用ValueStringBuilder拼接字符串

前言這一次要和大家分享的一個Tips是在字符串拼接場景使用的&#xff0c;我們經常會遇到有很多短小的字符串需要拼接的場景&#xff0c;在這種場景下及其的不推薦使用String.Concat也就是使用運算符。 目前來說官方最推薦的方案就是使用StringBuilder來構建這些字符串&#xff…

Buildroot 龍芯1C支持指南

本文轉載自&#xff1a;https://github.com/pengphei/smartloong-sphinx/blob/master/source/cn/loongson1c_buildroot_guide.rst Buildroot 龍芯1C支持指南 引子&#xff1a;從龍芯1C預訂拿到板子已經很長一段時間了&#xff0c;因為各種事情&#xff0c;一直讓它呆在角落的冷…

STOLUCK:經濟下行的當下 ,STO或將幫助中小企業度過寒冬

2018年被稱為創業陣亡率特別高的一年&#xff0c;相關報道稱有近20%的創業團隊面臨“后續融資跟不上&#xff0c;可能死在春天來臨之前”的窘境。經濟不景氣的當下&#xff0c;上下游資金不足&#xff0c;信貸機構沒錢&#xff0c;風投業捉襟見肘。實際今年3月份開始&#xff0…

[ 轉載 ] Java面試精選【Java基礎第一部分】

http://www.cnblogs.com/hnlshzx/p/3491587.html 轉載于:https://www.cnblogs.com/ILoke-Yang/p/8137326.html

html如何自動調整邊框大小,html – Chrome與大小調整:顯示中的邊框:表格

我正在使用display&#xff1a;table做一個小的2窗格布局.對于間距(也來自背景圖像),我使用填充.因為我需要孩子們有一個確切的寬度&#xff1a;50&#xff05;來自可用空間(考慮到父div的填充),我使用Box-sizing&#xff1a;border-Box.這在Opera中運行良好,但在Chrome中,框大…

淺析C# Dictionary實現原理

一、前言二、理論知識1、Hash 算法2、Hash 桶算法3、解決沖突算法三、Dictionary 實現1. Entry 結構體2. 其它關鍵私有變量3. Dictionary - Add 操作4. Dictionary - Find 操作5. Dictionary - Remove 操作6. Dictionary - Resize 操作(擴容)7. Dictionary - 再談 Add 操作8. C…

對特朗普獲勝感到意外? 那你是被社交媒體迷惑了

北京時間11月10日消息&#xff0c;據外媒報道&#xff0c;昨天曠日持久的美國總統選戰終于告一段落&#xff0c;特朗普的獲勝讓民調徹底成了一張廢紙&#xff0c;而早就在Facebook上提前歡慶希拉里勝利的人則徹底蒙圈了&#xff0c;就連萬里之外的中國吃瓜群眾們也開始追著許多…

貓晚流量再創記錄,阿里云直播方案護航優酷2500萬用戶體驗

2019獨角獸企業重金招聘Python工程師標準>>> 對“剁手黨而言&#xff0c;天貓雙11早已經超越了簡單的“買買買”&#xff0c;更是一場邊看邊玩的狂歡盛宴。今年的天貓雙11狂歡夜晚會&#xff08;簡稱“貓晚”&#xff09;在上海舉辦&#xff0c;這臺兼具年輕潮流與國…

python實現二叉樹和它的七種遍歷

介紹&#xff1a; 樹是數據結構中非常重要的一種&#xff0c;主要的用途是用來提高查找效率&#xff0c;對于要重復查找的情況效果更佳&#xff0c;如二叉排序樹、FP-樹。另外可以用來提高編碼效率&#xff0c;如哈弗曼樹。 代碼&#xff1a; 用python實現樹的構造和幾種遍歷算…

.NET性能系列文章二:Newtonsoft.Json vs System.Text.Json

微軟終于追上了&#xff1f;圖片來自 Glenn Carstens-Peters[1]Unsplash[2]歡迎來到.NET 性能系列的另一章。這個系列的特點是對.NET 世界中許多不同的主題進行研究、基準和比較。正如標題所說的那樣&#xff0c;重點在于使用最新的.NET7 的性能。你將看到哪種方法是實現特定主…

android gpu平板 推薦,性能強的不像話,最強安卓平板華為平板M6上手

原標題&#xff1a;性能強的不像話&#xff0c;最強安卓平板華為平板M6上手你為什么買平板電腦&#xff1f;當這一問題問出以后&#xff0c;許多朋友的表情都很微妙&#xff0c;隨后大概率的回答則相當統一&#xff1a;"我買平板干嘛&#xff1f;"。其實得到這樣一個…

【Python】HackBack(獲取暴力破解服務器密碼的IP來源)

1、前言 又在0x00sec上翻到好東東。 https://0x00sec.org/t/python-hackback-updated/882 帖子里的腳本會得到那些暴力服務器密碼失敗的IP和用戶名&#xff0c;并且使用shodan api做一個溯源定位。 #!/usr/bin/python3.4 import re import urllib.request import json log_path…

企業應用“數據優先”革命的下一個主戰場:安全與運營

根據IDC發布的2015年全球CIO日程預測&#xff0c;80%的CIO將提供一個實現創新和改善業務決策的新體系架構。 大數據時代&#xff0c;企業軟件市場正在經歷一次大遷移&#xff0c;數以十億計的企業IT支出預算將投向“數據優先”應用&#xff0c;而不是長久以來以業務流程和工作流…

給Web開發人員的以太坊入坑指南

以太坊現在各種學習資料數不勝數&#xff0c;但由于以太坊正處于飛速發展階段&#xff0c;有些學習資料很快就過時了。所以想找到有價值的資料無異于大海撈針。我費了很大功夫&#xff0c;才建立起對以太坊的整體認識&#xff0c;搞清楚它的工作機制。我相信很多躍躍欲試的開發…

和碩看重物聯網大勢 程建中:從擅長領域出發

物聯網(IoT)前景可期已是全球科技產業的共識&#xff0c;但是如何真正找出到位的商機&#xff0c;卻考驗產業鏈業者的智能。蘋果iPhone代工廠和碩聯合科技執行長程建中表示&#xff0c;物聯網與大數據相關應用商機看俏&#xff0c;物聯網筑的夢比網際網路還大&#xff0c;當年網…