亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Lazy延時(shí)加載的ListView

系統(tǒng) 1681 0
使用的是第三方開發(fā)包CWAC-AdapterWrapper.jar

Lazy延時(shí)加載的ListView

    
package com.ql.app;


/***
Copyright (c) 2008-2009 CommonsWare, LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
	http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
/**
 * 這個(gè)庫(kù)不好之處,就是底部Loading的View無(wú)法定制
 * http://blog.sina.com.cn/s/blog_643e83860100q4vj.html
 * @author admin
 *
 */
public class EndlessAdapterDemo extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
	super.onCreate(icicle);
	setContentView(R.layout.main);
	
	ArrayList<Integer> items=new ArrayList<Integer>();
	
	for (int i=0;i<15;i++) { items.add(i); }
	
	setListAdapter(new DemoAdapter(items));
}

class DemoAdapter extends EndlessAdapter {
	private RotateAnimation rotate=null;
	
	DemoAdapter(ArrayList<Integer> list) {
		super(new ArrayAdapter<Integer>(EndlessAdapterDemo.this,
					R.layout.row,android.R.id.text1,list));
		
		rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
					0.5f, Animation.RELATIVE_TO_SELF,0.5f);
					
		rotate.setDuration(600);
		rotate.setRepeatMode(Animation.RESTART);
		rotate.setRepeatCount(Animation.INFINITE);
	}
	
	@Override
	protected View getPendingView(ViewGroup parent) {
		View row=getLayoutInflater().inflate(R.layout.row, null);
		
		View child=row.findViewById(android.R.id.text1);
		
		child.setVisibility(View.GONE);
		
		child=row.findViewById(R.id.throbber);
		child.setVisibility(View.VISIBLE);
		child.startAnimation(rotate);
		
		return(row);
	}
	
	@Override
	protected boolean cacheInBackground() {
		SystemClock.sleep(5000);				// pretend to do work
		
		return(getWrappedAdapter().getCount()<75);
	}
	
	@Override
	protected void appendCachedData() {
		if (getWrappedAdapter().getCount()<75) {
			@SuppressWarnings("unchecked")
			ArrayAdapter<Integer> a=(ArrayAdapter<Integer>)getWrappedAdapter();
			
			for (int i=0;i<15;i++) { a.add(a.getCount()); }
		}
	}
}
}

  

    
package com.ql.app;


/***
Copyright (c) 2008-2009 CommonsWare, LLC
Portions (c) 2009 Google, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
	http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/		

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.util.Log;
import android.widget.ListAdapter;
import java.util.concurrent.atomic.AtomicBoolean;
import com.commonsware.cwac.adapter.AdapterWrapper;

/**
* Adapter that assists another adapter in appearing endless.
* For example, this could be used for an adapter being
* filled by a set of Web service calls, where each call returns
* a "page" of data.
*
* Subclasses need to be able to return, via getPendingView()
* a row that can serve as both a placeholder while more data
* is being appended.
*
* The actual logic for loading new data should be done in
* appendInBackground(). This method, as the name suggests,
* is run in a background thread. It should return true if
* there might be more data, false otherwise.
*
* If your situation is such that you will not know if there
* is more data until you do some work (e.g., make another
* Web service call), it is up to you to do something useful
* with that row returned by getPendingView() to let the user
* know you are out of data, plus return false from that final
* call to appendInBackground().
*/
abstract public class EndlessAdapter extends AdapterWrapper {
abstract protected boolean cacheInBackground();
abstract protected void appendCachedData();

private View pendingView=null;
private AtomicBoolean keepOnAppending=new AtomicBoolean(true);
private Context context;
private int pendingResource=-1;

/**
	* Constructor wrapping a supplied ListAdapter
*/
public EndlessAdapter(ListAdapter wrapped) {
	super(wrapped);
}

/**
 * Constructor wrapping a supplied ListAdapter and providing a id for a pending view.
 * @param context
 * @param wrapped
 * @param pendingResource
 */
public EndlessAdapter(Context context, ListAdapter wrapped, int pendingResource) {
	super(wrapped);
	this.context=context;
	this.pendingResource=pendingResource;
}

/**
	* How many items are in the data set represented by this
	* Adapter.
*/
@Override
public int getCount() {
	if (keepOnAppending.get()) {
		return(super.getCount()+1);		// one more for "pending"
	}
	
	return(super.getCount());
}

/**
 * Masks ViewType so the AdapterView replaces the "Pending" row when new
 * data is loaded.
 */
public int getItemViewType(int position) {
	if (position==getWrappedAdapter().getCount()) {
		return(IGNORE_ITEM_VIEW_TYPE);
	}
	
	return(super.getItemViewType(position));
}

/**
 * Masks ViewType so the AdapterView replaces the "Pending" row when new
 * data is loaded.
 * 
 * @see #getItemViewType(int)
 */
public int getViewTypeCount() {
	return(super.getViewTypeCount()+1);
}

/**
	* Get a View that displays the data at the specified
	* position in the data set. In this case, if we are at
	* the end of the list and we are still in append mode,
	* we ask for a pending view and return it, plus kick
	* off the background task to append more data to the
	* wrapped adapter.
	* @param position Position of the item whose data we want
	* @param convertView View to recycle, if not null
	* @param parent ViewGroup containing the returned View
*/
@Override
public View getView(int position, View convertView,
										ViewGroup parent) {
	if (position==super.getCount() &&
			keepOnAppending.get()) {
		if (pendingView==null) {
			pendingView=getPendingView(parent);

			new AppendTask().execute();
		}

		return(pendingView);
	}
	
	return(super.getView(position, convertView, parent));
}

/**
	* Called if cacheInBackground() raises a runtime exception,
	* to allow the UI to deal with the exception on the
	* main application thread.
	* @param pendingView View representing the pending row
	* @param e Exception that was raised by cacheInBackground()
	* @return true if should allow retrying appending new data, false otherwise
*/
protected boolean onException(View pendingView, Exception e) {
	Log.e("EndlessAdapter", "Exception in cacheInBackground()", e);
	
	return(false);
}

/**
 * A background task that will be run when there is a need
 * to append more data. Mostly, this code delegates to the
 * subclass, to append the data in the background thread and
 * rebind the pending view once that is done.
 */
class AppendTask extends AsyncTask<Void, Void, Exception> {
	@Override
	protected Exception doInBackground(Void... params) {
		Exception result=null;
		
		try {
			keepOnAppending.set(cacheInBackground());
		}
		catch (Exception e) {
			result=e;
		}
		
		return(result);
	}

	@Override
	protected void onPostExecute(Exception e) {
		if (e==null) {
			appendCachedData();
		}
		else {
			keepOnAppending.set(onException(pendingView, e));
		}
		
		pendingView=null;
		notifyDataSetChanged();
	}
}

/**
 * Inflates pending view using the pendingResource ID passed into the constructor
 * @param parent
 * @return inflated pending view, or null if the context passed into the pending view constructor was null.
 */
protected View getPendingView(ViewGroup parent) {
	if(context != null) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		return inflater.inflate(pendingResource, parent, false);
	}
	
	throw new RuntimeException("You must either override getPendingView() or supply a pending View resource via the constructor");
}
}

  

row.xml
    
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
>
	<TextView android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="?android:attr/listPreferredItemHeight"
	/>
	<ImageView android:id="@+id/throbber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
	android:src="@drawable/android:ic_popup_sync"
	android:layout_gravity="center_horizontal"
	android:visibility="gone"
	/>
</FrameLayout>


  

main.xml
    
<?xml version="1.0" encoding="utf-8"?>
<ListView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/list"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent"
	android:drawSelectorOnTop="false"
/>

  

http://blog.sina.com.cn/s/blog_643e83860100q4vj.html

精確監(jiān)聽AbsListView滾動(dòng)至底部
http://blog.csdn.net/hellogv/article/details/6615487

Android ListView pull up to refresh
http://www.iteye.com/topic/1116292
https://github.com/johannilsson/android-pulltorefresh

完整工程:

Lazy延時(shí)加載的ListView


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产一区二区在线看 | 午夜色网| 国产福利在线免费 | 99re久久在热线播放最新地址 | 四虎影在线永久免费观看 | 国产成人久久久精品一区二区三区 | 久久99久久| 四虎成人免费观看在线网址 | 狠狠地射 | 国产精品免费福利 | 激情欧美 | 久久精品视频免费 | 欧美成人免费mv在线播放 | 日本视频中文字幕 | 91视频你懂的 | 天天躁日日躁成人字幕aⅴ 天天躁日日躁狠狠躁黑人躁 | 四虎在线视频观看大全影视 | 99爱视频99爱在线观看免费 | 欧美在线一区视频 | 亚洲精品国产v片在线观看 亚洲精品国产啊女成拍色拍 | 69国产成人综合久久精品91 | 国产精品视频一区二区三区不卡 | 久草在线国产 | 伊人久热这里只精品视频 | 日本一区二区三区中文字幕 | 日本1区| 一区二区三区在线视频播放 | 深夜影院老司机69影院 | 国产91精品久久久久久久 | 久久久精品波多野结衣 | 国产亚洲精品线观看77 | 91麻豆精品国产91久久久久 | 亚洲精品一区二区三区美女 | 日韩在线一区二区 | 国产梦呦精品 | 精品亚洲综合久久中文字幕 | 免费看一级a一片毛片 | 久久久精品午夜免费不卡 | 成人免费草草视频 | 亚洲综合一二三 | 国产精品天堂 |