Android Scollowview嵌套ListView
来源:互联网 发布:淘宝 百雀羚漫网专卖店 编辑:程序博客网 时间:2024/06/06 05:33
Scollowview 嵌套ListView
缺点:
listView只会显示一行,滑动冲突
解决办法:
重新测量ListView的高度
代码如下:直接粘贴复制即可
这里面有两个测量listview高度的方法
一个是自定义listview
一个事动态设置listview的高度,如果用自定义的话,就是用那个自定义的MyListView,用的时候需要把布局的listview换成你的包名加你的类名,效果是一样的,喜欢那个就用那个吧。
xml布局
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.scollview_listview.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/mLv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</ScrollView>
</RelativeLayout>
主界面
MainActivity
public class MainActivity extends AppCompatActivity {
private List<String> list;
private ListView mLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
mLv= (ListView) findViewById(R.id.mLv);
// mLv.setSelection(MyListView.FOCUS_DOWN);
mLv.setFocusable(false);
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,list);
mLv.setAdapter(adapter);
initHeight(mLv);
}
private void initList() {
list=new ArrayList<>();
for (int i=0;i<50;i++){
list.add("这是第"+i+"数据");
}
}
private void initHeight(ListView listview){
int height=0;
View view=null;
ListAdapter listAdapter=listview.getAdapter();
if(listAdapter==null){
return;
}
for(int i=0;i<listview.getCount();i++){
view=listAdapter.getView(i,view,listview);
view.measure(0,0);
height+=view.getMeasuredHeight();
}
ViewGroup.LayoutParams params=listview.getLayoutParams();
params.height=height+(listview.getDividerHeight()*(listview.getCount()-1));
listview.setLayoutParams(params);
}
}
public class MyListView extends ListView {
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
//Integer.MAX_VALUE>>2 系统规定只能显示30个数值,而int类型是32个数值,所以需要再往后两位
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, height);
}
}
- Android Scollowview嵌套ListView
- ListView 和Scollowview嵌套滑动冲突的解决
- Android 中ListView 嵌套 ListView
- Android listview 中嵌套 listview
- android ScrollView嵌套ListView
- Android ExpandableListView嵌套ListView
- android ScrollView嵌套ListView
- Android ListView嵌套
- android ScrollView嵌套Listview
- android 嵌套 listview
- android scrollview 嵌套listview
- Android ScrollView嵌套ListView
- Android ExpandableListView嵌套ListView
- listView 嵌套gridView android
- Android之ScrollView嵌套ListView
- Android之ScrollView嵌套ListView
- android scrollView嵌套listview问题
- Android中ScrollView嵌套ListView
- android.mk 语法
- bzoj1455罗马游戏
- memcache redis
- android studio 解决app显示界面无标题
- 洛谷 P2055 [ZJOI2009]假期的宿舍
- Android Scollowview嵌套ListView
- 2017.4.6 公司练习题
- groovy 直接将给定的文本转换成Java中的Class<?>对象从而不需要再手动去写.java文件
- 台芯片大军遭前后夹击 今年转型成关键
- linux 下Valgrind泄漏检查工具
- groovy 直接将给定的文本转换成Java中的Class<?>对象从而不需要再手动去写.java文件
- 微信红包-腾讯校招
- java 中的类和对象
- BigInteger类的使用