比方說,你有一個指針到列表的對象。然後運行如下:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myListViewArrayOfItems.remove(index);
myListViewAdapter.notifyDataSetChanged();
}
}, numberOfSeconds);
而且你可以在適配器removeItem(final int index)
您運行
myListViewArrayOfItems.remove(index);
notifyDataSetChanged();
聲明的方法如果numberOfSeconds是隨機的和刪除操作需要可用於取消:
public class RemoveRunnable implements Runnable {
public interface RemoveListener {
void onItemRemoved(int index);
}
private BaseAdapter mAdapter;
private int mIndexToRemove;
private RemoveListener mListener;
public RemoveRunnable(BaseAdapter adapter, int index, RemoveListener listener) {
mAdapter = adapter;
mIndexToRemove = index;
mListener = listener;
}
public void run() {
mAdapter.removeItem(mIndexToRemove);
mListener.onItemRemoved(mIndexToRemove);
}
public void updateIndex(int removedIndex) {
if (removedIndex < mIndexToRemove) {
mIndexToRemove--;
}
}
}
與用法:
private Handler mHandler = new Handler();
private List<RemoveRunnable> mRunnables = new ArrayList<RemoveRunnable>();
private void removeItem(final int index, final int delay) {
Runnable runnable = new RemoveRunnable(myAdapter, index, this);
mRunnables.add(runnable);
mHandler.postDelayed, delay);
}
private void cancelAllRemovals() {
mHandler.removeCallbacksAndMessages(null);
}
@Override
public void onItemRemoved(final int index) {
mRunnables.remove(index);
// Update rest indexes
for (int i=0; i < mRunnables.size(); i++) {
mRunnables.get(i).updateIndex(index);
}
}
默認情況下,listview只加載可以在移動設備屏幕上容納的數據。 – amalBit
感謝評論amalBit。可能是我無法提供我的任務,所以我已經更新了這個問題。 – user98239820
您的問題現在已清楚。 – amalBit