列表视图中的LoadMore在底部android下滚动时清除上一项?

时间:2016-01-13 06:09:16

标签: android android-listview android-adapter android-scrollview

我正在使用loadmore listview来获取项目。最初我得到20个项目并显示。但问题是当我滚动底部它取出下20个项目但前20个项目被清除。如何解决这个问题?这是代码。

 public class ActivityChatView extends ActivityBase implements
    OnLoadMoreListener {
private LoadMoreListView chatList;
private Intent intent;
private String expertName, chatId;
private int offset = 0;
ArrayList<History> chatHistoryList;
private AdapterChatView mAdapter;
private ProgressBar progressBar;
private TextView progressText;
private boolean isProgressNeeded = true;
private String rowId;

protected void setContentToLayout() {
    setContentView(R.layout.activity_chat_view);
    initChatListView();
    offset = 0;

}

private void initChatListView() {
    intent = getIntent();
    chatList = (LoadMoreListView) findViewById(R.id.list_chat_data);
    progressBar = (ProgressBar) findViewById(R.id.probar);
    progressText = (TextView) findViewById(R.id.loading_text);
    progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    chatList.setEmptyView(findViewById(android.R.id.empty));
    chatList.setOnLoadMoreListener(this);
    expertName = intent.getStringExtra(Constants.EXPERT_NAME);
    chatId = intent.getStringExtra(Constants.CHAT_ID);
    if (isProgressNeeded)
        progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    serviceCall(offset);
}
private void serviceCall(int offset) {
    startService(new Intent(ActivityChatView.this, ChatService.class)
            .setAction(Constants.CHAT_HISTORY)
            .putExtra(Constants.USER_ID, expertName)
            .putExtra(Constants.CHAT_ID, chatId)
            .putExtra(Constants.OFFSET, String.valueOf(offset)));
}

protected void onChatHistory(ArrayList<? extends Parcelable> arrayList) {
    ArrayList<History> listHistory = (ArrayList<History>) arrayList;
    chatHistoryList = new ArrayList<History>();
    for (int i = 0; i < listHistory.size(); i++) {
        History historyData = new History();
        historyData.setFromUser(listHistory.get(i).getFromUser());
        historyData.setMsgBody(listHistory.get(i).getMsgBody());
        historyData.setMsgTime(listHistory.get(i).getMsgTime());
        historyData.setRowId(listHistory.get(i).getRowId());
        historyData.setMsgId(listHistory.get(i).getMsgId());
        historyData.setToUser(listHistory.get(i).getToUser());
        chatHistoryList.add(historyData);
    }
    progressBar.setVisibility(View.GONE);
    progressText.setVisibility(View.GONE);
    setValuesInAdapter();
}
@Override
public void onLoadMore() {
    LogMessage.e("number", "number"+offset);
    serviceCall(offset);
}
private void setValuesInAdapter() {
    if (mAdapter == null) {
        mAdapter = new AdapterChatView(this, chatHistoryList);
        chatList.setAdapter(mAdapter);
    }
    mAdapter.setExpertData(chatHistoryList);
    mAdapter.notifyDataSetChanged();
    chatList.onLoadMoreComplete();
    History e = chatHistoryList.get(chatHistoryList.size() - 1);
    rowId= e.getRowId();
    LogMessage.e("rowId", rowId);
    offset = Integer.valueOf(rowId);
    if (chatHistoryList != null && !chatHistoryList.isEmpty()) {
        chatList.setVisibility(View.VISIBLE);
        chatList.getEmptyView().setVisibility(View.GONE);
    } else {
        chatList.getEmptyView().setVisibility(View.VISIBLE);
    }
}

}

  in adapter..
 //setExpertData.
 public void setExpertData(List<History> chatHistoryList) {
    this.mTempData = chatHistoryList;
}

1 个答案:

答案 0 :(得分:2)

protected void onChatHistory(ArrayList<? extends Parcelable> arrayList) {
    ArrayList<History> listHistory = (ArrayList<History>) arrayList;
    //chatHistoryList = new ArrayList<History>();// (Remove this line from here)
    for (int i = 0; i < listHistory.size(); i++) {
        History historyData = new History();
        historyData.setFromUser(listHistory.get(i).getFromUser());
        historyData.setMsgBody(listHistory.get(i).getMsgBody());
        historyData.setMsgTime(listHistory.get(i).getMsgTime());
        historyData.setRowId(listHistory.get(i).getRowId());
        historyData.setMsgId(listHistory.get(i).getMsgId());
        historyData.setToUser(listHistory.get(i).getToUser());
        chatHistoryList.add(historyData);
    }
    progressBar.setVisibility(View.GONE);
    progressText.setVisibility(View.GONE);
    setValuesInAdapter();
}

并将其带入

private void initChatListView() {

chatHistoryList = new ArrayList<History>();
    intent = getIntent();
    chatList = (LoadMoreListView) findViewById(R.id.list_chat_data);
    progressBar = (ProgressBar) findViewById(R.id.probar);
    progressText = (TextView) findViewById(R.id.loading_text);
    progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    chatList.setEmptyView(findViewById(android.R.id.empty));
    chatList.setOnLoadMoreListener(this);
    expertName = intent.getStringExtra(Constants.EXPERT_NAME);
    chatId = intent.getStringExtra(Constants.CHAT_ID);
    if (isProgressNeeded)
        progressBar.setVisibility(View.VISIBLE);
    progressText.setVisibility(View.VISIBLE);
    serviceCall(offset);
}