Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating headers of a ListView giving ClassCastException

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.

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.

Or Is there a way to apply layout params that supports margin on a 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);
    }
}
}
like image 876
wasaig Avatar asked Dec 01 '11 09:12

wasaig


1 Answers

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.

like image 86
gianpi Avatar answered Nov 18 '22 05:11

gianpi