无法刷新和滑动
添加notifyDatesetChanged()后滑动卡到一半,既不能上划也不能下滑。请教,谢谢!
package com.example.android_listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity implements OnItemClickListener,OnScrollListener{
private ListView listView;
private ArrayAdapter<String>arr_adapter;
private SimpleAdapter simp_adapter;
private List<Map<String,Object>>dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView)findViewById(R.id.listView);
String[]arr_data={"imook1","imook2","imook3","imook4"};
dataList=new ArrayList<Map<String,Object>>();
arr_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data);
simp_adapter=new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text});
listView.setAdapter(simp_adapter);
listView.setOnItemClickListener(this);
listView.setOnScrollListener(this);
}
private List<Map<String,Object>> getData()
{
for(int i=0;i<20;i++)
{
Map<String,Object>map=new HashMap<String, Object>();
map.put("pic",R.drawable.ic_launcher);
map.put("text","imook"+i);
dataList.add(map);
}
return dataList;
}
@Override
public void onScroll(AbsListView view, int scrollState, int arg2, int arg3) {
// TODO Auto-generated method stub
switch(scrollState)
{
case SCROLL_STATE_FLING:
Log.i("Main","用户在手指离开屏幕之前,由于用力滑了一下,视图仍能继续滑动");
Map<String,Object>map=new HashMap<String, Object>();
map.put("pic",R.drawable.ic_launcher);
map.put("text", "增加项");
dataList.add(map);
simp_adapter.notifyDataSetChanged();
break;
case SCROLL_STATE_IDLE:
Log.i("Main","视图已经停止滑动");
break;
case SCROLL_STATE_TOUCH_SCROLL:
Log.i("Main", "手指没有离开屏幕");
break;
}
}
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String text=listView.getItemIdAtPosition(position)+"";
Toast.makeText(this, "position="+position+" text="+text, Toast.LENGTH_SHORT).show();
}
}