安卓AndroidScrollView嵌套ListView的问题及其优化

来源:互联网 发布:网络打字员兼职招聘网 编辑:程序博客网 时间:2024/06/16 09:42

在写项目的过程中通常会用到ScrollView嵌套ListView的情况,此时我们使用原生的ListView会出现只显示一行的情况,并且可能会出现scrollview与listview的滑动出现冲突,前者是因为在scrollView中ListView在OnMeasure阶段无法测出实际的高度,后者是因为事件冲突。

第一个问题的解决方案有三种。

1.写死listview的高度,前提是在scrollview内嵌套一个linearlayout,并且使其包裹listview,xml布局如下:

    <ScrollView        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content">            <ListView                android:id="@+id/main_lv"                android:layout_width="match_parent"                android:layout_height="300dp"/>        </LinearLayout>    </ScrollView>

这种方法弊端显而易见,体验感很差劲,所以不推荐用这种方法,这时候看第二种

2.动态设置listview高度,java代码如下:

public class MainActivity extends AppCompatActivity {    private ListView listView;    private ArrayAdapter<String > adapter;    private ArrayList<String> datas;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView) findViewById(R.id.main_lv);        datas = new ArrayList<>();        for (int i = 0; i < 50; i++) {            datas.add("这是第"+i+"条数据");        }        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datas);        listView.setAdapter(adapter);        setListViewHight(listView);    }    private void setListViewHight(ListView listView) {        // 获取ListView对应的Adapter        ListAdapter listAdapter = listView.getAdapter();        if(listAdapter == null){            return;        }        int totalHeight = 0;        for (int i = 0; i < listAdapter.getCount(); i++) {            // listAdapter.getCount()返回数据项的数目            View itemView = listAdapter.getView(i,null,listView);            // 计算子项View 的宽高            itemView.measure(0,0);            // 统计所有子项的总高度            totalHeight += itemView.getMeasuredHeight();        }        // listView.getDividerHeight()获取子项间分隔符占用的高度        // params.height最后得到整个ListView完整显示需要的高度        ViewGroup.LayoutParams params = listView.getLayoutParams();        params.height = totalHeight +(listView.getDividerHeight()*(listAdapter.getCount()-1));        listView.setLayoutParams(params);    }}
另外也有前提,在scrollview内嵌套一个linearlayout,并且使其包裹listview,不然动态设值的高度不起作用。这种方式较为复杂,那么请看我们的第三种方法:

3.自定义ListView,并重写其中的onMeasure()方法。

public class MyListView extends ListView {    public MyListView(Context context) {        super(context);    }    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int mySpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, mySpec);    }}
解释一下,从上边我们可以看出,我们没有改变widthMeasureSpec,仅仅是调用了makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST)方法,该方法会返回一个带有模式和大小信息的int值的,第一个参数Integer.MAX_VALUE >> 2,我们知道我们的控件的大小的最大值是用30位表示的(int占32位,其中前两位用来表示文章开头所说的三种模式)。那么Integer.MAX_VALUE来获取int值的最大值,然后右移2位,就得到这个最大值了 。因为是要最大值所以只能选择AT_MOST模式。最后 super.onMeasure()方法将我们的高度值传进去就可以使ListView内容都展示出来了。 最后我们在xml布局文件中添加这个自定义的控件就行了。





1 0
原创粉丝点击