动画ListView的标题给出ClassCastException

时间:2022-02-04 15:52:09

I have listview that stores the communication history of a person. I have one header inside a listview that acts as a message editor with a edit text and a send button. When a user types something and press send button the messages adds to the communication list and editor gets empty.

我有listview存储一个人的通信历史。我在listview中有一个标题,它充当带有编辑文本和发送按钮的消息编辑器。当用户键入内容并按发送按钮时,消息将添加到通信列表中,编辑器将变为空。

What I want is when user press the send button, the editor should become invisible and Item should be added to the listview. After that the editor should come gradually from the top giving the feel that its moving the items below.

我想要的是当用户按下发送按钮时,编辑器应该变为不可见,并且应该将项目添加到列表视图中。之后,编辑应该逐渐从顶部开始,让人觉得它会移动下面的项目。

I have implemented a translate animation on the header but what it does is it makes the space for it by pushing the items down and then gradually fills the space which I dont want.

我已经在标题上实现了一个翻译动画,但它的作用是通过向下推动项目然后逐渐填充我不想要的空间来为它创造空间。

I used the negative margin trick which is explained in this question but It didn't work for me. As we cant use layout params other that AbsListView.LayoutParam for the headers. I tried setting Other params but while animating It gives me ClassCastException. I tracked the exception and its due to code written inside ListView they are trying to cast these params with AbsListView.LayoutParams inside clearRecycledState() method.

我使用了这个问题中解释的负边距技巧,但它对我不起作用。因为我们不能使用布局参数,其他的AbsListView.LayoutParam用于标头。我尝试设置其他参数但是动画它给了我ClassCastException。我跟踪了异常,由于在ListView中编写的代码,他们试图在clearRecycledState()方法中使用AbsListView.LayoutParams来编译这些参数。

Or Is there a way to apply layout params that supports margin on a listview-header.

或者有没有办法在listview-header上应用支持边距的布局参数。

the code

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.
}

heres the code of message editor

继承了消息编辑器的代码

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

1 个解决方案

#1


1  

Have you thought about a different approach instead? Maybe you can just put the editor view at the top of the list, but outside the screen, and then use smoothScrollToPosition to transition in. So in reality you're just scrolling the list, but the effect could be what you're looking for.

你有没有考虑过不同的方法?也许你可以把编辑器视图放在列表的顶部,但是在屏幕之外,然后使用smoothScrollToPosition来转换。所以实际上你只是滚动列表,但效果可能是你正在寻找的。

#1


1  

Have you thought about a different approach instead? Maybe you can just put the editor view at the top of the list, but outside the screen, and then use smoothScrollToPosition to transition in. So in reality you're just scrolling the list, but the effect could be what you're looking for.

你有没有考虑过不同的方法?也许你可以把编辑器视图放在列表的顶部,但是在屏幕之外,然后使用smoothScrollToPosition来转换。所以实际上你只是滚动列表,但效果可能是你正在寻找的。