【问题标题】:Animating headers of a ListView giving ClassCastExceptionListView 的动画标题给出 ClassCastException
【发布时间】:2011-12-10 19:06:45
【问题描述】:

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

我想要的是当用户按下发送按钮时,编辑器应该变得不可见并且应该将项目添加到列表视图中。之后,编辑器应该从顶部逐渐出现,给人一种正在移动下面的项目的感觉。

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

我使用了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);
    }
}
}

【问题讨论】:

  • 您能否发布您的翻译动画文件,以便我们想办法让它发挥作用?
  • 我已经编辑了问题,你可以看一下代码。这是我在这里做的一个简单的翻译动画。
  • 您在代码中使用 listView.addHeaderView(headerlayout) 吗? ,如果是,那么您可以根据您的发送按钮事件可见和可见您的标题布局
  • 设置标题的可见性没有问题。我想要的是当我将可见性从消失变为可见时。标题应该逐渐下降,推动所有列表项。相反,它正在推动所有物品,使其空间等于其高度,然后下降。给人一种跳跃的外观和感觉。

标签: android listview animation


【解决方案1】:

您是否考虑过另一种方法?也许您可以将编辑器视图放在列表顶部,但在屏幕之外,然后使用smoothScrollToPosition 过渡。所以实际上您只是滚动列表,但效果可能是您的正在寻找。

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 2015-01-31
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多