码迷,mamicode.com
首页 > 其他好文 > 详细

关于ListView的HeaderView

时间:2014-07-02 16:51:40      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:android   listview   headerview   隐藏   可见   

1. 可点击/不可点击

	private void addHeaderView(){
		View headerView = this.mInflater.inflate(R.layout.XXX_header_info, this.listView, false);
		this.headerInfo = (TextView) headerView.findViewById(R.id.XXX_HeaderInfo);
		//this.listView.addHeaderView(headerView);
		//让HeaderView不可点击 
		this.listView.addHeaderView(headerView, null, false);
		
		this.headerInfo.setVisibility(View.GONE);
	}


2. HeaderView滑动到完全看不到之后隐藏

		this.listView.setOnScrollListener(new OnScrollListener() {			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if(scrollState == SCROLL_STATE_IDLE && headerInfo.getVisibility() == View.VISIBLE 
						&& listView.getFirstVisiblePosition() > 0) {					
					//listView.removeHeaderView(headerView);
					headerInfo.setVisibility(View.GONE);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {				
			}
		});
如果刷新后需要显示HeaderView,再次设置可见即可:

	private void setHeaderInfo(int num) {		
		String text = String.format("关于“%s”,共%d条评论。", XXXXData.name, num);
		this.headerInfo.setText(text);
		this.headerInfo.setVisibility(View.VISIBLE);
	}
而不必在removeHeaderView之后再次add:

		if(this.listView.getHeaderViewsCount() <= 0) {
			this.listView.addHeaderView(this.headerView, null, false);
		}

采用removeHeaderView()方法移除headerView的方法会出现显示问题:直接移除ListView的列表头会出现屏幕跳动,改为设置headerView的子视图不可见即可。


这里的headerInfo是headerView的子视图,直接设置headerView不可见的话,headerView是看不到了,但是仍然占用了listView视图的高度,只是显示为空白而已。下面是XXX_HeaderInfo.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:orientation="vertical" >

    <TextView
        android:id="@+id/XXX_HeaderInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="9dp"
        android:textColor="@color/text_color"
        android:textSize="@dimen/title_text_size" 
        android:background="#fefee2"/>

</LinearLayout>


这里没有给根视图添加背景,而是把背景设置到了子视图TextView的背景上了。


直接add/remove headerView在某些机型上可能会出现问题,也可以通过往headerView里添加移除子视图headerInfo的方式来实现。

其他要注意的地方就是在给listView增加页眉、页脚的时机要在setAdapter之前,这是add的代码:

    /**
     * Add a fixed view to appear at the top of the list. If addHeaderView is
     * called more than once, the views will appear in the order they were
     * added. Views added using this call can take focus if they want.
     * <p>
     * NOTE: Call this before calling setAdapter. This is so ListView can wrap
     * the supplied cursor with one that will also account for header and footer
     * views.
     *
     * @param v The view to add.
     * @param data Data to associate with this view
     * @param isSelectable whether the item is selectable
     */
    public void addHeaderView(View v, Object data, boolean isSelectable) {

        if (mAdapter != null) {
            throw new IllegalStateException(
                    "Cannot add header view to list -- setAdapter has already been called.");
        }

        FixedViewInfo info = new FixedViewInfo();
        info.view = v;
        info.data = data;
        info.isSelectable = isSelectable;
        mHeaderViewInfos.add(info);
    }

通过设置headerView不可见,并且remove页眉的方式,屏幕出现了跳动。刷新数据后设置可见、add页眉,也没有出错,如果只是通过原代码来解释似乎不通。

关于ListView的HeaderView,布布扣,bubuko.com

关于ListView的HeaderView

标签:android   listview   headerview   隐藏   可见   

原文地址:http://blog.csdn.net/xujunfeng000/article/details/36386147

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!