android - 安卓如何讓View往屏幕外隱藏?
問題描述
如圖,讓和讓最新那條插入的數(shù)據(jù) 顯示在 頂部開始的地方,而舊的數(shù)據(jù)隱藏在頂部屏幕之外呢?
當(dāng)插入 問題14的時(shí)候,,前13條被隱藏在頂部之外的地方了。怎么做到這樣呢?模仿語(yǔ)言助手那種界面?如何做到


問題解答
回答1:以下兩種方案都親測(cè)可行:方案一:(無動(dòng)畫)
listView.setSelection(問題14的position);
方案二:(帶動(dòng)畫)
ListView lvBaseList添加問題14后調(diào)用
final int targetPosition = 2;//假設(shè)你要置頂?shù)腎temView的position = 2final long targetId = adapter.getItemId(targetPosition);View child = getTargetView(lvBaseList, targetId);final boolean isVisible = child != null;if (isVisible) {//問題14已顯示 Log.d(TAG, 'lvBaseList.onItemClick isVisible = true >> '); Log.d(TAG, 'lvBaseList.onItemClick lvBaseList.getY() = ' + (int) lvBaseList.getY()); Log.d(TAG, 'lvBaseList.onItemClick child.getY() = ' + (int) child.getY()); lvBaseList.smoothScrollBy((int) (child.getY() - lvBaseList.getY()), 200); return;}//問題14未顯示Log.d(TAG, 'lvBaseList.onItemClick isVisible = false >> ');lvBaseList.smoothScrollToPosition(targetPosition + lvBaseList.getHeaderViewsCount());lvBaseList.setOnScrollListener(new OnScrollListener() {@Override public void onScrollStateChanged(AbsListView view, int scrollState) {if (scrollState == SCROLL_STATE_IDLE) { lvBaseList.setOnScrollListener(null);//避免影響正常滾動(dòng) View child = getTargetView(lvBaseList, targetId); if (child == null) {Log.e(TAG, 'lvBaseList.onItemClick child == null >> return;');return; } Log.d(TAG, 'lvBaseList.onItemClick lvBaseList.getY() = ' + (int) lvBaseList.getY()); Log.d(TAG, 'lvBaseList.onItemClick child.getY() = ' + (int) child.getY());// lvBaseList.scrollListBy((int) (child.getY() - lvBaseList.getY()));//可行,無動(dòng)畫 lvBaseList.smoothScrollBy((int) (child.getY() - lvBaseList.getY()), 200);} }@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { }});
需要用到的函數(shù):
//根據(jù)targetId獲取childView private View getTargetView(ListView lvBaseList, long targetId) {View child = null;MomentView itemView;//MomentView相當(dāng)于ViewHolder,里面存放item數(shù)據(jù)或者itemIdMomentItem item;for (int i = 0; i < lvBaseList.getChildCount(); i++) { child = lvBaseList.getChildAt(i); itemView = child == null ? null : (MomentView) child.getTag(); item = itemView == null ? null : itemView.getData(); Log.d(TAG, 'lvBaseList.onItemClick item.getId() = ' + (item == null ? 0 : item.getId()) + '; targetId = ' + targetId); if (item != null && item.getId() == targetId) {Log.d(TAG, 'lvBaseList.onItemClick item != null && item.getId() == targetId >> break;');break; } child = null;}return child; }
相關(guān)文章:
1. python - sqlalchemy更新數(shù)據(jù)報(bào)錯(cuò)2. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?3. javascript - h5微信中怎么禁止橫屏4. macos - 無法source activate python275. empty比isset更嚴(yán)格一點(diǎn)6. thinkphp6使用驗(yàn)證器 信息如何輸出到前端頁(yè)面7. 我在導(dǎo)入模板資源時(shí)遇到無法顯示的問題,請(qǐng)老師解答下8. PHPExcel表格導(dǎo)入數(shù)據(jù)庫(kù)怎么導(dǎo)入9. javascript - 微信網(wǎng)頁(yè)開發(fā)從菜單進(jìn)入頁(yè)面后,按返回鍵沒有關(guān)閉瀏覽器而是刷新當(dāng)前頁(yè)面,求解決?10. 預(yù)訂金和尾款分別支付

網(wǎng)公網(wǎng)安備