6

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);
    }
}
}
Community
  • 1
  • 1
wasaig
  • 666
  • 5
  • 18
  • Can you post your translation animation file so we can think of a way to make it work? – DallaRosa Dec 05 '11 at 04:45
  • I have edited the question, you can take a look at the code. Its a simple translate animation I am doing here. – wasaig Dec 05 '11 at 07:22
  • are you using listView.addHeaderView(headerlayout) in your code? ,if yeas then you can visible and visible gone your header layout based on your send button events – bindal Dec 05 '11 at 08:10
  • having no problem setting visibility of the header. What I want is when i change visibility from gone to visible. The header should gradually come down pushing all the list items. Instead It is pushing all the items making space equal to its height and then coming down. Gives kinda jumpy look and feel. – wasaig Dec 05 '11 at 10:08

1 Answers1

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.

gianpi
  • 3,110
  • 1
  • 17
  • 13