adb shell pm setInstallLocation 2 SD卡
adb shell pm setInstallLocation 0 内存
在滑动的过程中,只有在视野中的item才会加载数据,其他的其实显示的都是”loading…” 这样的好处就是如果加载的是图片的话,那么始终加载的只是几张图片而非是所有的图片,这样就能很好的避免出现内存溢出的问题,当然其他的图片要及时的回收才行,否则图片对象依然在内存里(这样的话图片最好先保存在本地,这样就不会频繁的发送请求,就不会产生网络流量),关于加载大量图片的问题会在以后的文章中出现。下面就看代码,非常简单。
public class ListViewTest extends Activity implements ListView.OnScrollListener {
private boolean mBusy = false;
ListView l;
SlowAdapter slowAdapter;
int totalPage = 10;// 共有页数
int nowPage = 1;// 当前页
int lastItem = 0;
List<String> mStrings= new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
l = (ListView) findViewById(R.id.mainList);
getListByPage(nowPage);
slowAdapter=new SlowAdapter(this,mStrings);
l.setAdapter(slowAdapter);
l.setOnScrollListener(this);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
lastItem = firstVisibleItem + visibleItemCount - 1;
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { //滑动停止
mBusy = false;
if(lastItem==mStrings.size()-1){
nowPage++;
if (nowPage <= totalPage) {
getListByPage(nowPage);
}
}
slowAdapter.notifyDataSetChanged();
}
else if(scrollState == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL){//滑动手未松开
mBusy = true;
}else if(scrollState == OnScrollListener.SCROLL_STATE_FLING){//滑动中手已松开
mBusy = true;
}
}
private class SlowAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<String> list;
public SlowAdapter(Context context,List<String> list) {
this.list=list;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
if (convertView == null) {
text = (TextView)inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
} else {
text = (TextView)convertView;
}
if (!mBusy) {
text.setText(list.get(position));
} else {
text.setText("Loading...");
}
return text;
}
}
public void getListByPage(int page){
int start = (page-1)*20;
int size=page*20;
for(int i=start;i<size;i++){
mStrings.add("条目"+i);
}
}
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
}
2、加载图片的线程
class MyThread extends Thread{
int index;
public MyThread(int index) {
super();
this.index = index;
}
@Override
public void run() {
if (imageList.get(index).getIsNull().equals(ISTRUE)) { imageList.get(index).setImage(ImageManager.getRefImage(imageList.get(index)
.getPath()));
imageList.get(index).setIsNull(ISFALSE);
}
refreshAdapter();
}
}
3、gallery的滑动事件
private Gallery.OnItemSelectedListener itemSelected_listener = new Gallery.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
toShowIndex = position;
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (showingIndex != toShowIndex) {
showingIndex = toShowIndex;
// 业务逻辑处理
if (toShowIndex < imageList.size()) {
addImage(toShowIndex);
}
}
}
};
Thread checkChange = new Thread() {
@Override
public void run() {
int myIndex = toShowIndex;
try {
sleep(TIME_OUT_DISPLAY);
if (myIndex == toShowIndex) {
handler.sendEmptyMessage(0);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
checkChange.start();
TextView gallery_total = (TextView) findViewById(R.id.gallery_total);
gallery_total.setText((toShowIndex + 1) + "/"+ imageList.size()); }
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
};
4、更新ImageAdapter
private void refreshAdapter() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
adpter.notifyDataSetChanged();
}
});
}
5、从sd卡里获取图片后封装对象
public List<ImageMessage> getAllImageMessage() {
List<ImageMessage> imageList = new ArrayList<ImageMessage>();
getSD(Environment.getExternalStorageDirectory().toString()+ "/");
for (int i = 0; i < pathList.size(); i++) {
ImageMessage im = new ImageMessage();
im.setPath(pathList.get(i)[0]);
im.setName(pathList.get(i)[1]);
im.setIsNull(ISTRUE);
imageList.add(im);
}
return imageList;
}