您的位置:首页 > 移动开发 > Android开发

android:ScrollView嵌套ListView的问题

2013-10-21 12:21 429 查看
在ScrollView中嵌套使用ListView,看起来ListView只会显示一行多一点,不能滑动。ListView的高度怎么改都有问题,与预期不符合。搜索了一些解决方案,我觉得最好不要用这样的设计,因为默认情况下android
禁止在ScrollView中放入另外的
ScrollView,它的高度是无法计算的

在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);

}

本帖最后由 小尛龙 于 2013-9-6 10:53 编辑

效果图:



在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);OK

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐