下拉刷新加ScrollView嵌套List,加暂无数据和底部 the-end布局写法

 写之前注意实现:

        ,首先,这种嵌套方式容易造成高度丢失.为了解决这个问题,需要做以下准备:

               1.ScrollView 自定义,写法后面代码区

                2.ListView 自定义, 重写onMeasure.

     3.listView的Item写法必须采用

       LayoutInflater.from(mContext).inflate(R.layout.item_store_list, parent,false);
       写法,借鉴Item父控件的高度,不要采用inflate(R.layout.item_store_list,null);
     4.ScrollView里面只能有一个子控件,采用线性布局包裹,不要采用ReletiveLayout进行包裹.
     5.暂无数据必须给固定高度,否则,这个高度会丢失.
喜欢的话,点个赞
    本人Github地址:https://github.com/wolfking0608
    csdn 博客收藏地址:http://blog.csdn.net/wolfking0608
    微信:18665996821 加我可加入本人的微信大群.全是安卓工程师,欢迎来沟通和交流
    以下为源代码:
     自定义的MyScrollview
          
public class MyScrollview extends ScrollView {

	private OnScrollListener onScrollListener;
	/**
	 * 主要是用在用户手指离开本view,本view还在继续滑动,我们用来保存Y的距离,然后做比较
	 */
	private int lastScrollY;

	public MyScrollview(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 设置滚动接口
	 * 
	 * @param onScrollListener
	 */
	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}

	/**
	 * 用于用户手指离开MyScrollView的时候获取MyScrollView滚动的Y距离,然后回调给onScroll方法中
	 */
	private Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			int scrollY = MyScrollview.this.getScrollY();

			// 此时的距离和记录下的距离不相等,在隔6毫秒给handler发送消息?
			if (lastScrollY != scrollY) {
				lastScrollY = scrollY;
				handler.sendMessageDelayed(handler.obtainMessage(), 6);
			}
			if (onScrollListener != null) {
				onScrollListener.onScroll(scrollY);
			}

		};

	};

	/**
	 * 重写onTouchEvent, 当用户的手在HoveringScrollview上面的时候,
	 * 直接将HoveringScrollview滑动的Y方向距离回调给onScroll方法中,当用户抬起手的时候,
	 * HoveringScrollview可能还在滑动,所以当用户抬起手我们隔6毫秒给handler发送消息,在handler处理
	 * HoveringScrollview滑动的距离
	 */
	public boolean onTouchEvent(MotionEvent ev) {
		if (onScrollListener != null) {
			onScrollListener.onScroll(lastScrollY = this.getScrollY());
		}
		switch (ev.getAction()) {
		case MotionEvent.ACTION_UP:
			handler.sendMessageDelayed(handler.obtainMessage(), 20);
			break;
		}
		return super.onTouchEvent(ev);
	};

	/**
	 * 滚动的回调接口
	 */
	public interface OnScrollListener {
		/**
		 * 回调方法, 返回本view滑动的Y方向距离
		 */
		public void onScroll(int scrollY);
	}

}
2.自定义的ListView
    
public class CustomListView extends ListView {
    public CustomListView(Context context) {
        super(context);
    }

    public CustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomListView(Context context, AttributeSet attrs,
                          int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    /**
     * 重写该方法、达到使ListView适应ScrollView的效果
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
3. 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:background="@color/line_color"
    android:orientation="vertical">
       <include
           android:id="@+id/title_layout"
           layout="@layout/home_search_title_layout"
           android:layout_width="match_parent"
           android:layout_height="@dimen/title_height"/>
   
    <TextView
        android:id="@+id/search_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingLeft="@dimen/dp_14"
        android:paddingBottom="@dimen/dp_11"
        android:paddingTop="@dimen/dp_11"
        android:text="搜索结果(0条)"
        android:textColor="@color/hint_color"
        android:textSize="@dimen/size_14"/>
      <View
          android:layout_width="match_parent"
          android:layout_height=
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值