ScrollView嵌套GridView,并实现GridView竖直分页加载

来源:互联网 发布:bcm4306 linux 编辑:程序博客网 时间:2024/06/09 14:20

最近在项目中遇到ScrollView嵌套GridView的需求,由于ScrollView和GridView的滑动方向都一样,所以会产生冲突。在网上找了好了好久,搜到的都是在setOnTouchListener(new View.OnTouchListener()里设置GridView的requestDisallowInterceptTouchEvent(true),可这并不能真正解决我的需求问题,思考测试一段时间后,想到了一个方法,但任然存在一些小bug。
具体的代码和过程如下:
首先要自定义一个ScrollView

package com.xiaosong.yuangou;import android.content.Context;import android.util.AttributeSet;import android.widget.ScrollView;/** * Created by user on 2015/8/31. */public class InforScrollView extends ScrollView {    private OnScrollListener onScrollListener;    public InforScrollView(Context context){        super(context);    }    public InforScrollView(Context context,AttributeSet attrs){        super(context, attrs);    }    public InforScrollView(Context context,AttributeSet attrs,int defStyle){        super(context, attrs, defStyle);    }    public void setOnScrollListener(OnScrollListener onScrollListener){        this.onScrollListener = onScrollListener;    }    @Override    protected int computeVerticalScrollRange() {        return super.computeVerticalScrollRange();    }    @Override    protected void onScrollChanged(int l, int t, int oldl, int oldt) {        super.onScrollChanged(l, t, oldl, oldt);        if(onScrollListener != null){            onScrollListener.onScroll(t);        }    }    public interface OnScrollListener{        public void onScroll(int ScrollY);    }}

接下来就是自定义的GridView,用来实现GridView控件的内容滑动到顶部和底部的监听,代码如下:

import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.widget.AbsListView;import android.widget.GridView;/** * Created by user on 2015/9/29. */public class MainHeadGrideView extends GridView implements AbsListView.OnScrollListener{    private int count = 15;    private int num = 1;    private OnScrollBottomListener Bottomlistener;    private OnScrollTopListener Toplistener;    public MainHeadGrideView(Context context) {        super(context);    }    public MainHeadGrideView(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    public void onScrollStateChanged(AbsListView view, int scrollState) {        switch (scrollState) {            case OnScrollListener.SCROLL_STATE_IDLE:                Log.v("onScrollStateChanged", "已经停止:SCROLL_STATE_IDLE");                if(view.getFirstVisiblePosition() == 0){                    Log.v("onScrollStateChanged", "到顶部啦。可以撤销权限");                    if (Toplistener != null) {                        Toplistener.onScrollTop();                    }                }                // 判断滚动到底部                if (view.getLastVisiblePosition() == (view.getCount() - 1)) {                    Log.v("onScrollStateChanged", "到底部啦。可以请求刷新");                    if (Bottomlistener != null) {                        Bottomlistener.onScrollBottom(view.getCount());                    }                }                break;            case OnScrollListener.SCROLL_STATE_FLING:                Log.v("onScrollStateChanged", "开始滚动:SCROLL_STATE_FLING");                break;            case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:                Log.v("onScrollStateChanged", "正在滚动:SCROLL_STATE_TOUCH_SCROLL");                break;        }    }    @Override    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {    }    public void setOnScrollBottomListener(OnScrollBottomListener Bottomlistener) {        this.setOnScrollListener(this);        this.Bottomlistener = Bottomlistener;    }    public void setOnScrollTopListener(OnScrollTopListener Toplistener) {        this.setOnScrollListener(this);        this.Toplistener = Toplistener;    }    public void removeOnScrollBottomListener() {        Bottomlistener = null;    }    public void removeOnScrollTopListener() {        Toplistener = null;    }    public interface OnScrollBottomListener {        public void onScrollBottom(int count);    }    public interface OnScrollTopListener{        public void onScrollTop();    }}

需要的控件都定义好了,接下来就是整体的布局和实现了。
需要注意的是GridView的长度需要定死,如果是match_parent或wrap_content都不能实现ScrollView下的滑动,GridView的长度要根据它所在的ScrollView的位置来确定,拿本次的项目来说,需求是当选择栏滑动到顶部时ScrollView就停止滑动,滑动手势就传递给GridView,那么GridView的长度就应该小于等于选择栏到界面底部的距离否则就会挡住GridView的内容。
MainActivity的部分代码如下:

 main_scrollview =(InforScrollView)findViewById(R.id.main_scrollview); main_scrollview.setOnScrollListener(this); @Override    public void onScroll(int ScrollY) {        int top = Math.max(ScrollY,main_classify_search_layout.getTop());        main_classify_search_layout_top.layout(0, top, main_classify_search_layout_top.getWidth(),                top + main_classify_search_layout_top.getHeight());        if(ScrollY==629){            synchronized (this){                switch(backpagelist.get(backpagelist.size()-1)){                    case PAGE1:                        headgridview.setOnTouchListener(new View.OnTouchListener() {                            @Override                            public boolean onTouch(View v, MotionEvent event) {                                synchronized (this) {                                    if (event.getAction() == MotionEvent.ACTION_DOWN ) {                                        headgridview.requestDisallowInterceptTouchEvent(true);                                    }                                }                                return false;                            }                        });                        break;                    case PAFE4:                        detailinfor_webview.setOnTouchListener(new View.OnTouchListener() {                            @Override                            public boolean onTouch(View v, MotionEvent event) {                                detailinfor_webview.requestDisallowInterceptTouchEvent(true);                                return false;                            }                        });                        break;                    case PAFE6:                        shop_gride.setOnTouchListener(new View.OnTouchListener() {                            @Override                            public boolean onTouch(View v, MotionEvent event) {                                synchronized (this) {                                    if (event.getAction() == MotionEvent.ACTION_DOWN ) {                                        shop_gride.requestDisallowInterceptTouchEvent(true);                                    }                                }                                return false;                            }                        });                        break;                    case PAGE5:                        goodsGridView.setOnTouchListener(new View.OnTouchListener() {                            @Override                            public boolean onTouch(View v, MotionEvent event) {                                synchronized (this) {                                    if (event.getAction() == MotionEvent.ACTION_DOWN ) {                                        goodsGridView.requestDisallowInterceptTouchEvent(true);                                    }                                }                                return false;                            }                        });                        break;                }            }        }    }

这是ScrollView滑动到指定位置的代码

public void yghead(String merchantid){        loadnum = 0;        goodsofyghead.removeAll(goodsofyghead);        Map<String,Object> shopcontent = new LinkedHashMap<>();        shopcontent.put("merchantid", merchantid);        shopcontent.put("goodsname", null);        shopcontent.put("sortby",null);        shopcontent.put("goodstype",null);        shopcontent.put("shopid", null);        shopcontent.put("rowsnum", loadnum);        shopcontent.put("isbargain",null);        shopcontent.put("ishot", null);        goodsofyghead = getgoodslist(shopcontent);        System.out.printf("首页获取商品数" + goodsofyghead.size());        headgridview = (MainHeadGrideView) main_lay1.findViewById(R.id.main_head_gridview);        headgridview.setOnScrollBottomListener(new MainHeadGrideView.OnScrollBottomListener() {            @Override            public void onScrollBottom(int count) {                System.out.println("滑动加载");                sharedPreferences = getSharedPreferences("yuangou", MainActivity.MODE_PRIVATE);                String merchantid = sharedPreferences.getString("merchantid", "");                loadnum += 15;                Map<String, Object> shopcontent = new LinkedHashMap<>();                shopcontent.put("merchantid", merchantid);                shopcontent.put("goodsname", null);                shopcontent.put("sortby", null);                shopcontent.put("goodstype", null);                shopcontent.put("shopid", null);                shopcontent.put("rowsnum", loadnum);                shopcontent.put("isbargain", null);                shopcontent.put("ishot", null);                List<GoodsType> goodslists = getgoodslist(shopcontent);                for (int i = 0; i < goodslists.size(); i++) {                    goodsofyghead.add(goodslists.get(i));                }                headGridViewAdapter.notifyDataSetChanged();                System.out.println("goodsofyghead:" + goodsofyghead.size());            }        });        headgridview.setOnScrollTopListener(new MainHeadGrideView.OnScrollTopListener() {            @Override            public void onScrollTop() {                headgridview.setOnTouchListener(new View.OnTouchListener() {                    @Override                    public boolean onTouch(View v, MotionEvent event) {                        synchronized (this) {                            if (event.getAction() == MotionEvent.ACTION_UP) {                                headgridview.requestDisallowInterceptTouchEvent(false);                            }                        }                        return false;                    }                });            }        });        headGridViewAdapter = new HeadGridViewAdapter(this, R.layout.head_list_image_item,                goodsofyghead);        headgridview.setAdapter(headGridViewAdapter);    }

这是GridView滑动的代码,当滑动到底部的时候就会实现竖直的分页,重点在headGridViewAdapter.notifyDataSetChanged();当滑动到顶部的时候,并且手势是向上的,GridView就不再响应手势。

以上就是全部的内容了,ScrollView嵌套WebView滑动的问题,解决方法和上面类似,可以参考。

0 0
原创粉丝点击