時間懸浮
帶時間戳的列表揭蜒,要求時間懸浮頂部横浑,動態(tài)替換頂部時間如下圖,如下圖屉更,
布局實現(xiàn)
多布局實現(xiàn)伪嫁,時間懸浮為一個布局,數(shù)據(jù)相關(guān)內(nèi)容為一個布局偶垮,這里推薦一個Adapter依賴庫BRVAH
public InformationAdapter(@Nullable List<Information> data) {
super(data);
addItemType(ITEM_STICK, R.layout.item_stock_sticky_head);
addItemType(ITEM_DATA, R.layout.item_information);
}
根據(jù)返回的ItemViewType來加載不同的布局张咳,動態(tài)設(shè)置時間布局的顯示
@Override
protected void convert(BaseViewHolder helper, Information item) {
switch (helper.getItemViewType()) {
case 1:
helper.setVisible(R.id.tv_stock_name, true);
helper.setText(R.id.tv_stock_name, item.pinnedHeaderName);
break;
case 2:
int timeInt = item.getArticle_time();
String s = String.valueOf(timeInt);
String time = AppUtils.getStrTime(s);
helper.setText(R.id.btn_time, time);
Log.d("haha", "convert: 時間 : " + time);
Log.d("haha", "convert: " + item.getArticle_title());
helper.setText(R.id.txt_title, item.getArticle_title()).addOnClickListener(R.id.txt_title);
helper.setText(R.id.txt_content, item.getArticle_content());
helper.setText(R.id.txt_zan, " " + item.getArticle_like_count()).addOnClickListener(R.id.txt_zan);
helper.setText(R.id.txt_kankong, " " + item.getArticle_dislike_count());
break;
}
}
加載數(shù)據(jù)時根據(jù)時間比較帝洪,判斷當(dāng)前數(shù)據(jù)是頭部數(shù)據(jù)還是普通數(shù)據(jù),添加進(jìn)集合中
case STATE_REFREH:
timeString = "月";
mInformationAdapter.getData().clear();
for (Information information : informationList) {
String s = transForDate(information.getArticle_time());
if (!s.equals(timeString)) {
newList.add(new Information(Information.ITEM_STICK, s));
timeString = s;
}
Log.d(TAG, "showData: " + s);
information.setItemType(Information.ITEM_DATA);
newList.add(information);
}
mInformationAdapter.addData(newList);
break;
注意數(shù)據(jù)的判斷脚猾,正確進(jìn)行頭部數(shù)據(jù)的添加葱峡。
頭部布局的創(chuàng)建
mHeaderItemDecoration = new PinnedHeaderItemDecoration.Builder(Information.ITEM_STICK).create();
mRecyclerCoinCircle.addItemDecoration(mHeaderItemDecoration);
時間戳轉(zhuǎn)換
public static String transForDate(Integer ms) {
String str = "";
if (ms != null) {
long msl = (long) ms * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if (ms != null) {
try {
str = sdf.format(msl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
String[] split = str.split("-");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(split[1] + "月").append(split[2] + "日");
return stringBuffer.toString();
}
將時間轉(zhuǎn)化為xx月xx日的格式
文本展開與收起
默認(rèn)情況下,最多只顯示四行文本如下
點擊之后龙助,展開所有文本
具體實現(xiàn)如下:
TextView textView = (TextView) adapter.getViewByPosition(mRecyclerCoinCircle, position, R.id.txt_content);
if (textView != null) {
if (isClick.containsKey(position)) {
if (isClick.get(position)) {
textView.setMaxLines(4);
isClick.put(position, false);
} else {
textView.setMaxLines(1000);
isClick.put(position, true);
}
} else {
textView.setMaxLines(1000);
isClick.put(position, true);
}
}
下拉刷新砰奕,上拉加載自定義布局
設(shè)置下拉刷新,下拉加載的布局
mEasyLayout.setRefreshHeadView(new RefreshHeaderView(this));
mEasyLayout.setLoadMoreView(new LoadMoreView(this));
具體實現(xiàn)可參考Demo提鸟,或者依賴庫文檔
刷新之后回調(diào)
mEasyLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
@Override
public void onRefreshing() {
page =1;
state = STATE_REFREH;
getReFreshData();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mEasyLayout.refreshComplete();
}
}, 1000);
}
@Override
public void onLoadMore() {
page++;
state = STATE_MORE;
getInformationData();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mEasyLayout.loadMoreComplete();
}
}, 1000);
}
});
下拉加載军援,上拉刷新布局如圖,具體可運(yùn)行demo查看
爬過的坑
在頭部懸浮称勋,結(jié)合上拉刷新下拉加載過程中胸哥,遇到過上拉加載,導(dǎo)致頭部懸浮布局錯位赡鲜,先前采用
SmartRefreshLayout空厌,由于SmartRefreshLayout上拉加載會將布局向上頂,導(dǎo)致懸浮布局錯誤银酬,調(diào)試無果后嘲更,采用EasyRefreshLayout。
總結(jié)
頭部懸浮的上拉加載揩瞪,下拉刷新的列表主要實現(xiàn)采用Adapter依賴庫BRVAH及其推薦相關(guān)庫EasyRefreshLayout和RecyclerView粘性標(biāo)簽庫赋朦。其他更多用法,可以自行訪問相關(guān)庫學(xué)習(xí)李破。
最后給出DEMO.