android中listview分页加载数据
来源:互联网 发布:淘宝不能买彩票了吗 编辑:程序博客网 时间:2024/04/29 22:46
<span style="font-family: Arial; background-color: rgb(255, 255, 255);">listview的优化方案,优化方案有三种:</span>
1.如果自定义适配器,那么在getView方法中判断contentView是否为空,如果为空创建contentView并返回,如果不为空直接返回contentView。这样 能尽可能少创建view。
2.给contentView设置tag,传入一个viewHoder对象,用于缓存要实现的数据。
3.如果listview中显示的item太多,就要考虑分页加载了。
下面就注意介绍一下分页加载数据。首先在layout下创建listview.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView></LinearLayout>
然后创建listview_item.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/list_item_text" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:textSize="20sp" android:paddingTop="10dp" android:paddingBottom="10dp"/> </LinearLayout>
再为跟多按钮添加一个xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/loadMoreButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="loadMore" android:text="加载更多" /> </LinearLayout>
代码部分:
public class ListViewAdapter extends BaseAdapter { private static Map<Integer,View> m=new HashMap<Integer,View>(); private List<String> items; private LayoutInflater inflater; public ListViewAdapter(List<String> items, Context context) { super(); this.items = items; this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return items.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return items.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View contentView, ViewGroup arg2) { // TODO Auto-generated method stub contentView=m.get(position); if(contentView==null){ contentView=inflater.inflate(R.layout.listview_item, null); TextView text=(TextView) contentView.findViewById(R.id.list_item_text); text.setText(items.get(position)); } m.put(position, contentView); return contentView; } public void addItem(String item) { items.add(item); } }
public class ListViewActivity extends Activity implements OnScrollListener { List<String> items = new ArrayList<String>(); private ListView listView; private int visibleLastIndex = 0; //最后的可视项索引 private int visibleItemCount; // 当前窗口可见项总数 private ListViewAdapter adapter; private View loadMoreView; private Button loadMoreButton; private Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null); loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton); loadMoreButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub loadMoreButton.setText("正在加载..."); //设置按钮文字loading handler.postDelayed(new Runnable() { @Override public void run() { loadData(); adapter.notifyDataSetChanged(); //数据集变化后,通知adapter listView.setSelection(visibleLastIndex - visibleItemCount + 1); //设置选中项 loadMoreButton.setText("加载更多"); //恢复按钮文字 } }, 1000); } }); listView = (ListView) this.findViewById(R.id.listView1); listView.addFooterView(loadMoreView); //设置列表底部视图 // listView.addHeaderView(v) //设置列表顶部视图 initAdapter(); listView.setAdapter(adapter); //自动为id是list的ListView设置适配器 listView.setOnScrollListener(this); //添加滑动监听 listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), items.get(position),Toast.LENGTH_SHORT).show(); } }); } /** * 初始化适配器 */ private void initAdapter() { for (int i = 0; i < 20; i++) { items.add(String.valueOf(i + 1)); } adapter = new ListViewAdapter(items,this); } /** * 滑动时被调用 */ @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.visibleItemCount = visibleItemCount; visibleLastIndex = firstVisibleItem + visibleItemCount - 1; } /** * 滑动状态改变时被调用 */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { int itemsLastIndex = adapter.getCount() - 1; //数据集最后一项的索引 int lastIndex = itemsLastIndex + 1; //加上底部的loadMoreView项 if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) { //如果是自动加载,可以在这里放置异步加载数据的代码 Log.i("LOADMORE", "loading..."); } } /** * 模拟加载数据 */ private void loadData() { int count = adapter.getCount(); for (int i = count; i < count + 20; i++) { adapter.addItem(String.valueOf(i + 1)); } } }
最后看看效果:
0 0
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- android中listview分页加载数据
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- android中listview分页加载数据
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- android中listview分页加载数据
- Android中ListView分页加载数据
- Android中ListView分页加载数据
- android中listview分页加载数据
- android中listview分页加载数据
- Android中ListView分页加载数据
- Android获取手机图片路径的办法
- Chrome 控制台console的用法
- Android UI开源组件库BottomView ,第三方自定义UI控件
- 重要的事
- 2015年大一下第6周项目3-分数类中的运算符重载(1)分数类的加减乘除
- android中listview分页加载数据
- 第8周项目1.3 定义一个完整的类 实现复数加实数 实数加复数
- 第八周上机实践项目3-分数类中的运算符重载(分数与分数、分数与整数)
- thinkphp open,Call to undefined function imagecreatefromjpeg()
- 第八周项目1-3:实现复数类中的运算符重载-完整的复数类
- 2008 R2網域控制器配置WEB修改密碼功能 (安裝筆記)
- java多线程开发(1)
- 第八周项目一--定义完整的类,实现复数中的运算符重载
- Android NDK:①开发环境的搭建(Windows下Eclipse无cygwin)