zoukankan      html  css  js  c++  java
  • Android之ScrollView嵌套ListView冲突

    在ScrollView中嵌套使用ListView,ListView只会显示一行多一点。两者进行嵌套,即会发生冲突。
    由于ListView本身都继承于ScrollView,一旦在ScrollView中嵌套ScrollView,
    那么里面的ScrollView高度计算就会出现问题。
    我们也就无法得到想要的效果。
    下面进入正题,我们将讨论ScrollView中嵌套ListView情况。
    核心解决方案: 重写ListView或者GridView的OnMesure 方法。对GridView同样适用。

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
    MeasureSpec.AT_MOST);
    
    super.onMeasure(widthMeasureSpec, expandSpec);
    }

    ScrollView中嵌套ListView:

    package com.android.xiaomolongstudio.example.scrollviewlistview;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    /**
    * 
    * @author 小尛龙
    *
    */
    public class MainActivity extends Activity {
            ListView listView;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    listView = (ListView) findViewById(R.id.listView);
                    listView.setAdapter(new ArrayAdapter<String>(this,
                                    android.R.layout.simple_expandable_list_item_1, getData()));
            }
    
            private List<String> getData() {
                    List<String> data = new ArrayList<String>();
                    for (int i = 0; i < 30; i++) {
                            data.add("测试" + i);
                    }
    
                    return data;
    
            }
    
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.activity_main, menu);
                    return true;
            }
    
    }

    自定义ListView:

    package com.android.xiaomolongstudio.example.scrollviewlistview;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ListView;
    
    public class MyListView extends ListView {
    
            public MyListView(Context context) {
                    super(context);
            }
    
            public MyListView(Context context, AttributeSet attrs) {
                    super(context, attrs);
            }
    
            public MyListView(Context context, AttributeSet attrs, int defStyle) {
                    super(context, attrs, defStyle);
            }
    
            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                                    MeasureSpec.AT_MOST);
    
                    super.onMeasure(widthMeasureSpec, expandSpec);
            }
    
    }

    这样出来的效果是这样的:

    这样出来的效果是这样的:

    没有上面的按钮,一进页面直接显示的是ListView内容,怎么一开始就显示头部。
    ScrollView有个属性mScrollView.scrollTo(x, y)可以显示位置。
    但是实际却没有达到效果,查了说mScrollView.scrollTo(x, y)首次初始化时无效果。
    最后我用了mScrollView.smoothScrollTo(0,0);

  • 相关阅读:
    springboot整合mybatisplus
    在layui中使用treetable.js插件
    shiro系列9:基于前端的权限控制和基于后端的权限控制
    springboot文件上传案例
    springboot配置虚拟路径访问上传到磁盘的文件
    springboot文件下载案例
    springboot跨域的处理方式
    springboot上传文件过大的解决方案
    Django与Ajax
    Django查询数据库性能优化
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5749601.html
Copyright © 2011-2022 走看看