面试的几个小问题?
来源:互联网 发布:js数组删除空元素 编辑:程序博客网 时间:2024/05/21 21:38
1:ScrollView和ListView的?
有时候项目里面需要ScrollView嵌套ListView,但是正常下ListView只会显示一行多一点,解决方法就是填充ListView数据后
重新计算ListView的高度,这里有两种方法来实现。
***(1)第一种方法:重写ListView
package com.jwzhangjie.test;
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);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
*****(2)第二种方法:添加完数据后计算ListView中所有Item的高度和间隔线的高度然后重新设置ListView的高度
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
if (listAdapter.getCount() <= 1)
return;
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, null, listView);
view.measure(0,0);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
设置完数据后,调用setListViewHeightBasedOnChildren(listview);
有时候项目里面需要ScrollView嵌套ListView,但是正常下ListView只会显示一行多一点,解决方法就是填充ListView数据后
重新计算ListView的高度,这里有两种方法来实现。
***(1)第一种方法:重写ListView
package com.jwzhangjie.test;
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);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
*****(2)第二种方法:添加完数据后计算ListView中所有Item的高度和间隔线的高度然后重新设置ListView的高度
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
if (listAdapter.getCount() <= 1)
return;
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, null, listView);
view.measure(0,0);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
设置完数据后,调用setListViewHeightBasedOnChildren(listview);
0 0
- 面试的几个小问题?
- 关于面试的几个小问题
- 面试中的几个小问题
- 几个经典的面试问题
- C++面试的几个小题目
- 几个小问题的整理
- PING的几个小问题!
- HQL的几个小问题
- jquery的几个小问题
- HQL的几个小问题 .
- 数据结构的几个小问题
- ibatis的几个小问题
- 内存的几个小问题
- 关于“构造函数”中的几个小问题,也许面试会问到哦~
- 关于“构造函数”中的几个小问题,也许面试会问到哦~
- JAVA面试的小问题
- 面试人员的几个方面的问题
- 关于linux内核的几个面试问题
- UltraEdit 多词搜索
- poj 3694Network 双联通分量+lca
- 线程同步:信号量Semaphore的使用
- FastJSON处理Map的问题
- 单例模式的七种写法
- 面试的几个小问题?
- 用单链表实现双向队列
- Android requestWindowFeature崩溃
- Ehcache详细解读
- Angularjs国际化
- 用Java处理大数据小技巧总结
- MKDirections绘制导航路线
- Problem A - Sum Kind Of Problem 2015 ACM/ICPC Greater New York Region
- eclipse clean后不能自动编译生成class文件的问题