ListView 与GridView

一、ListView

1、自定义滚动条
  (1)、 设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可:

                 <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);

3、记录和恢复listView滚动位置
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
				
			}
		});

4、其它常用
(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、必须在设置数据前,添加头部与尾部



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值