您的位置:首页 > 其它

关于Scrollview嵌套listview数据显示不全,上拉加载没效果问题

2017-11-22 15:53 459 查看
今天公司有一个需求就是在Scrollview中嵌套的listview实现上拉加载更多数据的效果,(数据显示不全只需要重写listview的onmeasure()方法就可以了),我开始想的是用recyclerview来代替listview实现,毕竟recyclerview比较好用,哈哈,但是出现了一个问题,就是recyclerview嵌套在Scrollview里面后完全没有数据显示,于是各种百度,发现用重写测量listview的方法确实对[b]recyclerview没有效果,必要重写[b]recyclerview的布局管理也就是[/b][/b]
linearLayoutManager这个方法,但是在recyclerview中我的这个封装了上拉和下拉刷新效果,我尝试着修改了下,
setLayoutManager这个不仅没有找到方法,而且上拉加载更多也没有走进去
于是又百度了一阵,网上有人提议嵌套的方法不好,建议利用头布局来代替,我想这个办法,不出,于是在listview中添加了headview头布局,这个方法确实可行
,但是有点不符合需求,就是这个headview的头布局是加载在listview第一个也就是listview里面的,而这个listview布局我有边框的效果,就会导致headview
头布局也会出现边框,我想着在后面的数据里面在修改,但是每次都要监听第二个数据和最后一个数据想着太麻烦了,我就又回到了使用Scrollview嵌套listview的问题上
,想着就扎心,-----
现在我先贴出自定义的Scrollview
public class MyScrollview extends ScrollView {private int downX;private int downY;private int mTouchSlop;private OnScrollToBottomListener onScrollToBottomListener;public MyScrollview(Context context) {super(context);mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();}public MyScrollview(Context context, AttributeSet attrs) {super(context, attrs);mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();}public MyScrollview(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();}@Overrideprotected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);if (scrollY!=0&&onScrollToBottomListener!=null){onScrollToBottomListener.onScrollBottomListener(clampedY);}}public void setOnScrollToBottomLintener(OnScrollToBottomListener listener){onScrollToBottomListener = listener;}public interface OnScrollToBottomListener {void onScrollBottomListener(boolean isBottom);}//    @Override//    public boolean onInterceptTouchEvent(MotionEvent e) {//        int action = e.getAction();//        switch (action) {//            case MotionEvent.ACTION_DOWN://                downX = (int) e.getRawX();//                downY = (int) e.getRawY();//                break;//            case MotionEvent.ACTION_MOVE://                int moveY = (int) e.getRawY();//                if (Math.abs(moveY - downY) > mTouchSlop) {//                    return true;//                }//        }//        return super.onInterceptTouchEvent(e);//    }}
这个是自定义的
Scrollview类,然后在你需要调用的位置写如下代码(也是一个大佬写的,我觉得不错,拿来了,哈哈--)
myscrview.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {switch (motionEvent.getAction()){case MotionEvent.ACTION_DOWN://手指按下调用一次break;case MotionEvent.ACTION_MOVE://手指按下移动会一直调用break;case MotionEvent.ACTION_UP://手指离开,表示上拉一次,就调用一次// 这样能保证每次手指点击只能调用一次int scrollY = view.getScrollY();int height = view.getHeight();int scrollViewMeasuredHeight = myscrview.getChildAt(0).getMeasuredHeight();if (scrollY == 0) {//                            Log.e("scroll", "滑动到了顶端 view.getScrollY()=" + scrollY);}if ((scrollY + height) == scrollViewMeasuredHeight) {// 到达底部,刷新数据}break;}return false;}});
好啦,希望这个可以解决部分小伙伴的问题,我就是这样解决的,哈哈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: