ListView混合项目滚动

时间:2018-01-24 13:59:42

标签: android listview

我有一个listview,当我滚动它时混合它的项目。这是我的代码:

public class ListView_MessagesAdapter extends BaseAdapter {
private List<Message> messages;

public ListView_MessagesAdapter(List<Message> messages) {
    this.messages = messages;
}

@Override
public int getCount() {
    return this.messages != null ? this.messages.size() : 0;
}

@Override
public Message getItem(int i) {
    return this.messages != null ? this.messages.get(i) : null;
}

@Override
public long getItemId(int i) {
    return this.messages != null ? i : -1;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null)
        view = (RelativeLayout) LayoutInflater.from(this.context).inflate(R.layout.listview_messages, viewGroup, false);

    Message message = this.getItem(i);

    if (message != null) {
        switch (message.type) {
            case DEFAULT:
                this.manageAsDefault(view, message);
                break;
            case TEXT:
                this.manageAsText(view, message);
                break;
            case AUDIO:
                this.managerAsAudio(view, message);
                break;
        }
    }

    return view;
}

private void manageAsDefault(View view, Message message) {
    // Manage message...
}

private void manageAsText(View view, Message message) {
    // Manage message...
}

private void managerAsAudio(View view, Message message) {
    // Manage message...
}

}

这就是我绑定listview的方式:

ListView_MessagesAdapter adapter = new ListView_MessagesAdapter(messages); lstMessages.setAdapter(adapter);

我按照特定顺序将listview绑定了20条消息。它第一次正确加载,但是当我滚动时,项目混合没有任何意义。

我在网上找到的任何解决方案似乎都有效。任何解决方案?

1 个答案:

答案 0 :(得分:0)

view.removeAllChildren()中,您首先需要重置//here i did it first time //(part of addMultiFileField method you can see above, in first post) self.dynamicView.addMultiFileField(forProperty: property) let scrollView = self.dynamicView.subviews.first(where: { $0 is UIScrollView } ) as! UIScrollView let stackView = scrollView.subviews.first(where: { $0 is UIStackView }) as! UIStackView let addButton = stackView.subviews.first(where: { $0 is UIButton } ) as! UIButton addButton.isUserInteractionEnabled = true self.oneFileOnly = false let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.addFiles)) addButton.addGestureRecognizer(tapRecognizer) //and here i did it second time. this line stay here from previous realization and i forgot comment it self.dynamicView.addArrangedSubview(stackView) 变量的状态。这是因为在滚动时会重复使用它,例如,导致视图被处理为文本处理为音频。

尝试{{1}}(或类似的东西)。