Scrollview 里 嵌套listview
来源:互联网 发布:淘宝买家套话精仿高仿 编辑:程序博客网 时间:2024/05/21 10:24
想给listview的上方加一个header,我们可以 在listview上方增加布局来实现 如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.testlistviewscrollview.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dip"
android:text="-----这里是标题--------"
android:textColor="@android:color/holo_red_dark" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="5dip"/>
</LinearLayout>
但是,需要注意的是,这中做法,listview的header是固定在listview上的,不能随着listview的滑动而滑动。
如果,我们的需求是:header随着listview的滑动而滑动呢?
这个时候,我们或许就会用到ScrollView,在ScrollView里嵌套一个listview来实现我们的需求
如”
xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.testlistviewscrollview.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"><!--这里为什么又嵌套了一层布局呢?不说了。如果不知道,自己去掉这层view再运行看看-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dip"
android:text="-----这里是标题--------"
android:textColor="@android:color/holo_red_dark" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="5dip" />
</LinearLayout>
</ScrollView>
</LinearLayout>
java类:
public class MainActivity extends Activity {
private ListView listView;
private TestAdapter adapter;
private List<String> data;
int n = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
data = getData();
adapter = new TestAdapter(this, data);
listView.setAdapter(adapter);
setListener();
}
private List<String> getData() {
List<String> data = new ArrayList<String>();
for (int i = 2; i < 20; i++) {
data.add("第" + n + "页 测试数据" + i);
}
n++; // 用来模拟分页加载
return data;
}
}
这样的话产生了一个问题:你在屏幕上看到的listview只显示一个条目,如何解决这个问题呢?
1.xml中 写死listview的高度。经过测设,这种方法也具有一定的可行性,但是它还有个弊端,就是listview的数据是可变动的,除非你能正确的写出listview的高度。
2. 在代码中动态计算listview的高度:在给listview设置完adapter之后,再动态的设置listview的高度。
在 listView.setAdapter(adapter);之后调用 Util.setListViewHeight(listView);
setListViewHeight:
public static void setListViewHeight(ListView listview) {
ListAdapter listAdapter = listview.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
int aa = listAdapter.getCount();
ViewGroup.LayoutParams params = listview.getLayoutParams();
params.height = totalHeight + (listview.getDividerHeight() * (aa - 1));
// ((MarginLayoutParams) params).setMargins(0,0,0, 0);
listview.setLayoutParams(params);
}
运行后发现,虽然目前是打到我们的需求了,但是还存在几个问题:
1》在刚打开页面的是时候,界面自动定位到listview的第0条目,listview的header是看不到的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.testlistviewscrollview.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dip"
android:text="-----这里是标题--------"
android:textColor="@android:color/holo_red_dark" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="5dip"/>
</LinearLayout>
但是,需要注意的是,这中做法,listview的header是固定在listview上的,不能随着listview的滑动而滑动。
如果,我们的需求是:header随着listview的滑动而滑动呢?
这个时候,我们或许就会用到ScrollView,在ScrollView里嵌套一个listview来实现我们的需求
如”
xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.testlistviewscrollview.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"><!--这里为什么又嵌套了一层布局呢?不说了。如果不知道,自己去掉这层view再运行看看-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dip"
android:text="-----这里是标题--------"
android:textColor="@android:color/holo_red_dark" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="5dip" />
</LinearLayout>
</ScrollView>
</LinearLayout>
java类:
public class MainActivity extends Activity {
private ListView listView;
private TestAdapter adapter;
private List<String> data;
int n = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
data = getData();
adapter = new TestAdapter(this, data);
listView.setAdapter(adapter);
setListener();
}
private List<String> getData() {
List<String> data = new ArrayList<String>();
for (int i = 2; i < 20; i++) {
data.add("第" + n + "页 测试数据" + i);
}
n++; // 用来模拟分页加载
return data;
}
}
这样的话产生了一个问题:你在屏幕上看到的listview只显示一个条目,如何解决这个问题呢?
1.xml中 写死listview的高度。经过测设,这种方法也具有一定的可行性,但是它还有个弊端,就是listview的数据是可变动的,除非你能正确的写出listview的高度。
2. 在代码中动态计算listview的高度:在给listview设置完adapter之后,再动态的设置listview的高度。
在 listView.setAdapter(adapter);之后调用 Util.setListViewHeight(listView);
setListViewHeight:
public static void setListViewHeight(ListView listview) {
ListAdapter listAdapter = listview.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
int aa = listAdapter.getCount();
ViewGroup.LayoutParams params = listview.getLayoutParams();
params.height = totalHeight + (listview.getDividerHeight() * (aa - 1));
// ((MarginLayoutParams) params).setMargins(0,0,0, 0);
listview.setLayoutParams(params);
}
运行后发现,虽然目前是打到我们的需求了,但是还存在几个问题:
1》在刚打开页面的是时候,界面自动定位到listview的第0条目,listview的header是看不到的
2》分页加载加载第二页的时候,listview的高度不更新
时间紧,这俩个问题后续-------
另外 提醒大家注意一下:之前我看到网上好多帖子说 用 动态设置listview的高度这种方法时,要求adapter的xml布局里必须要有LinearLayout,还说出了原因。之前我就年轻的相信了,。后来我写代码验证了一下,这种说法是不正确的,我用的是相对布局,也是可以的。也许,网上的那种说法只是在某些特定的情况下才成立。
所以,提醒大家,不要轻易相信别人:),一定要自己验证
0 0
- Scrollview里嵌套listview
- Scrollview 里 嵌套listview
- ScrollView里嵌套ListView解决方案
- android 中 ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- ScrollView里嵌套ListView的冲突问题
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- ScrollView里嵌套ListView的解决办法。
- ScrollView 里嵌套listView冲突问题
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView
- android之ScrollView里嵌套ListView,gridview,scrollview
- Android ScrollView里嵌套listview,listview显示不全的问题
- 电信光猫超级密码破解
- Navicat导入mysql大数据时出错解决方法
- iOS时间那点事--NSCalendar + NSDateComponents
- 生活中我们需要学习的各种“定律”
- [easyui]实际应用中遇到的问题
- Scrollview 里 嵌套listview
- 我的java DCL-based Singleton
- 4-1 UVA 1589Xiangqi中国象棋
- 失效的“private”修饰符
- 运维小知识---CentOS6.5下SVN启动问题
- 农资行业也迅速搭上了互联网这趟快车
- javaweb学习总结——HttpServletResponse对象(一)
- iOS 导航栏的手势返回
- java字符转义 字符串中的双引号