一、ListView
1、自定义滚动条
(1)、
设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可:
3、记录和恢复listView滚动位置
<ListView android:id=”@+id/listView” android:layout_width=”fill_parent”
android:layout_height=”fill_parent” android:fastScrollEnabled=”true”
android:focusable=”true” />
(2)、自定义图片
try {
Field f = AbsListView.class.getDeclaredField(“mFastScroller”);
f.setAccessible(true);
Object o=f.get(listView);
f=f.getType().getDeclaredField(“mThumbDrawable”);
f.setAccessible(true);
Drawable drawable=(Drawable) f.get(o);
drawable=getResources().getDrawable(R.drawable.icon);
f.set(o,drawable);
Toast.makeText(this, f.getType().getName(), 1000).show();
} catch (Exception e) {
throw new RuntimeException(e);
}
2、当listview为空时通过setEmptyView(View emptyView)方法提示
l_v = (ListView) findViewById(R.id.l_v);
TextView tv = new TextView(this);
// 设置水平和垂直都居中
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setText(R.string.no_data);
// 布局样式,水平填充父控件,垂直匹配内容
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)l_v.getLayoutParams();
params.addRule(RelativeLayout.BELOW, 1);
addContentView(tv, params);
l_v.setEmptyView(tv);
4、其它常用listView.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE: //滚动停止 /** * 第一种:通过位置坐标来恢复 * 在恢复位置时调用: listView.scrollTo(scrolledX, scrolledY); */ scrollX = listView.getScrollX(); scrollY = listView.getScrollY(); /** * 第二种:通过显示在屏幕上的第一个item的位置来恢复 * 在恢复位置时调用: listView.setSelection(position); */ listpos = listView.getFirstVisiblePosition(); break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: //滚动状态 break; case OnScrollListener.SCROLL_STATE_FLING: //手指离开状态 break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } });
(1)、ListView设置了divider及dividerHeight,但当数据不够一页时最下一条无下划线,可设置ListView为 android:layout_height="fill_parent"即可。
5、java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.
是不是你在用 adapter.notifyDataSetChanged() 刷新listview的时候,如果当前listview还未来得及刷新,你就去触摸listview,就会导致跳出出错信息呢?
解决办法是,通过设置listview.setVisibility(View.VISIBLE/View.GONE)。
在 adapter.notifyDataSetChanged() 之前调用listview.setVisibility(View.GONE);
在adapter.notifyDataSetChanged() 之后调用listview.setVisibility(View.VISIBLE);
6、必须在设置数据前,添加头部与尾部