zoukankan      html  css  js  c++  java
  • Android ScrollView 和ListView 一起使用的问题汇总

    1.ScrollView 嵌套 ListView  ,touch事件的截获问题。

    参考 http://www.cnblogs.com/lqminn/archive/2013/03/02/2940194.html
    http://blog.csdn.net/chaihuasong/article/details/17499799

    _scrollView.requestDisallowInterceptTouchEvent(true);

    这句话的意思是告诉scrollView,滚动的事件交给我处理。用完以后记得还回去

    _scrollView.requestDisallowInterceptTouchEvent(false);

    如果不设置回去,ScrollView将无法滚动了。

    2.ScrollView 滚动时,ListView的第一个条目是否处于显示状态?

    参考 http://stackoverflow.com/questions/4628800/android-how-to-check-if-a-view-inside-of-scrollview-is-visible

    boolean checkNeedRefresh() {
            Rect scrollBounds = new Rect();
            View firstChild = listView.getChildAt(0);
            _scrollView.getHitRect(scrollBounds);
            if (firstChild.getLocalVisibleRect(scrollBounds)) {
                // Any portion of the firstChild, even a single pixel, is within the
                // visible window
                return true;
            } else {
                // NONE of the firstChild is within the visible window
                return false;
            }
        }

    3. listView不能显示完整

    参考 http://blog.csdn.net/hahashui123/article/details/39177057
    http://blog.csdn.net/solomonxiang/article/details/26507145

    public static void setListViewHeight(ListView listView) {
            try {
                int totalHeight = 0;
                ListAdapter adapter = listView.getAdapter();
                for (int i = 0, len = adapter.getCount(); i < len; i++) { // listAdapter.getCount()
                    View listItem = adapter.getView(i, null, listView);
                    listItem.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
                    listItem.measure(0, 0); 
                    totalHeight += listItem.getMeasuredHeight(); 
                }
    
                ViewGroup.LayoutParams params = listView.getLayoutParams();
                params.height = totalHeight
                        + (listView.getDividerHeight() * (listView.getCount() - 1));
                listView.setLayoutParams(params);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    顺带GridView

    public static void setGridViewHeight(GridView gridView, int numColumns) {
            try {
                ListAdapter adapter = gridView.getAdapter();
                int row = 3;
                View listItem = adapter.getView(0, null, gridView);
                if (listItem == null)
                    return;
                listItem.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                listItem.measure(0, 0); 
                int totalHeight = listItem.getMeasuredHeight() * row
                        + (gridView.getVerticalSpacing() * (row - 1));
                ViewGroup.LayoutParams params = gridView.getLayoutParams();
                params.height = totalHeight;
                gridView.setLayoutParams(params);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

     如果ListView 带有BottomView

    public static void setListViewHeight(ListView listView) {
            try {
                int totalHeight = 0;
                int bottomHeight = 0;
                ListAdapter dataAdapter = null;
                int totalItems = 0;
                ListAdapter adapter = listView.getAdapter();
                if (adapter instanceof HeaderViewListAdapter) {
                    HeaderViewListAdapter headerViewListAdapter = ((HeaderViewListAdapter) adapter);
                    dataAdapter = headerViewListAdapter.getWrappedAdapter();
                    totalItems = dataAdapter.getCount();
                    int allItems = headerViewListAdapter.getCount();
                    View bottomItem = headerViewListAdapter.getView(allItems - 1,
                            null, listView);
                    bottomItem.measure(0, 0);
                    bottomHeight = bottomItem.getMeasuredHeight();
    
                } else {
                    dataAdapter = adapter;
                }
    
                for (int i = 0, len = totalItems; i < len; i++) {
                    View listItem = dataAdapter.getView(i, null, listView);
                    listItem.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
                    listItem.measure(0, 0);
                    totalHeight += listItem.getMeasuredHeight();
                }
    
                int listviewCount = listView.getCount();
                int height = totalHeight
                        + (listView.getDividerHeight() * listviewCount + 1)
                        + bottomHeight;
    
                ViewGroup.LayoutParams params = listView.getLayoutParams();
                params.height = height;
                listView.setLayoutParams(params);
                listView.requestLayout();
                 
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    4. 其他,自定义控件实现ListView

    http://www.cnblogs.com/lesliefang/p/3587154.html 

     5. 发现 每次加载完成后,listview总是滚到 屏幕最上方,实际上listview上面还有东西被盖住了,解决办法如下

      <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="blocksDescendants"
                android:orientation="vertical" >

    最关键的一句, 找到srcollview的 内容控件,一般是 LinearLayout,加上属性  android:descendantFocusability="blocksDescendants"
    就可以了。

  • 相关阅读:
    16.普通用户、网格长、网格员,操作数据(3)newapp/components/putong.vue
    15.普通用户、网格长、网格员,操作数据(2)newapp/pages/center/center.vue
    14.普通用户、网格长、网格员,操作数据(1)后端部分
    13.需求:在后台可以控制网格长和网格员在小程序端对数据的编辑权限
    Asp.Net Core与携程阿波罗(Apollo)的第一次亲密接触
    Asp.Net Core 3.1 获取不到Post、Put请求的内容 System.NotSupportedException Specified method is not supported
    [Hei.Captcha] Asp.Net Core 跨平台图形验证码实现
    基于IdentityServer4的OIDC实现单点登录(SSO)原理简析
    ASP.NET Core Swagger接入使用IdentityServer4 的 WebApi
    ASP.NET Core 使用 Google 验证码(reCAPTCHA v3)代替传统验证码
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/4184617.html
Copyright © 2011-2022 走看看