我有listview存儲一個人的通信歷史。在listview中有一個頭文件,充當帶編輯文本和發送按鈕的消息編輯器。 當用戶鍵入內容並按下發送按鈕時,消息將添加到通信列表中,並且編輯器將變爲空。動畫ListView的標題給ClassCastException
我想要的是,當用戶按下發送按鈕,編輯器應該變得不可見,項目應該被添加到列表視圖。在那之後,編輯應該從頂部逐漸開始,感受到它在移動下面的項目。
我在頁眉上實現了一個翻譯動畫,但它所做的是通過將項目向下推,然後逐漸填充我不想要的空間來爲它創建空間。
我使用了在this question中解釋的負邊限技巧,但它對我不起作用。因爲我們不能使用其他AbsListView.LayoutParam的佈局參數作爲標題。我嘗試設置其他參數,但動畫時它給我ClassCastException。我追蹤了這個異常,並且由於在ListView中編寫的代碼,他們試圖在clearRecycledState()方法中使用AbsListView.LayoutParams轉換這些參數。
或者是否有一種方法來應用佈局參數,支持列表視圖標題上的邊距。
public class PageListView extends ListView {
private Application app;
private CommListAdapter listAdapter;
private MessageEditorHeader messageEditorHeader;
private MessageItemLongClick mInterface;
private Handler handler;
public ProfilePageListView(Application app, MessageItemLongClick mInterface) {
super(app);
this.app = app;
this.mInterface = mInterface;
this.handler = new Handler();
setupView();
}
public void applyData(ProfileData data){
listAdapter.applyData(data.getUser());
// some other business logic
}
private void setupView() {
messageEditorHeader = new MessageEditorHeader(app);
addHeaderView(messageEditorHeader);
listAdapter = new CommListAdapter(app, mInterface);
setAdapter(listAdapter);
setDivider(null);
setScrollingCacheEnabled(false);
tAnimation = new TranslateAnimation(0.0f, 0.0f, -90.0f, 0.0f);
tAnimation.setZAdjustment(-1);
tAnimation.setDuration(1500);
}
// this gets called whenever the communication gets added to the listview.
public void onNewCommunication(Communication lc) {
listAdapter.onNewCommunication();
if(lc != null && lc.isOutgoing() && !lc.getType().isCall()){
getMessageEditor().startNewMessage();
messageEditorHeader.setVisibility(VISIBLE); // this is overriden method here I m toggling the height 1px and WRAP_CONTENT
messageEditorHeader.startAnimation(tAnimation);
}
}
// few more methods are there.
}
繼承人郵件編輯器的代碼
public class MessageEditorHeader extends RelativeLayout {
private MessageEditor msgEditor;
public MessageEditorHeader(AppteraApplication context) {
super(context);
msgEditor = new MessageEditor(context); // Its a relative layout containing edit text and the send button
addView(msgEditor);
}
public MessageEditor getMsgEditor() {
return msgEditor;
}
public void setProgress(int progress){
msgEditor.setProgress(progress);
}
@Override
public void setVisibility(int visibility) {
this.visibility = visibility;
if (visibility == View.VISIBLE) {
ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT);
setLayoutParams(params);
}
else {
ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, 1);
setLayoutParams(params);
}
}
}
你可以發佈你的翻譯動畫文件,所以我們可以想辦法讓它工作嗎? – DallaRosa
我編輯了這個問題,你可以看看代碼。它是我在這裏做的一個簡單的翻譯動畫。 – wasaig
你在代碼中使用listView.addHeaderView(headerlayout)嗎? ,如果是,那麼你可以根據你的發送按鈕事件 – bindal