Android中l(wèi)istview嵌套scrollveiw沖突的解決方法
一.使用網(wǎng)上用的動態(tài)改變listview高度的方法
該方法只適用于item布局是LinearLayout布局的情況,不能是其他的,因為其他的Layout(如RelativeLayout)沒有重寫onMeasure(),所以會在onMeasure()時拋出異常。所以使用限制較大。
public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { //獲取ListView對應(yīng)的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回數(shù)據(jù)項的數(shù)目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); //計算子項View 的寬高 totalHeight += listItem.getMeasuredHeight(); //統(tǒng)計所有子項的總高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); //listView.getDividerHeight()獲取子項間分隔符占用的高度 //params.height最后得到整個ListView完整顯示需要的高度 listView.setLayoutParams(params); } }
二.網(wǎng)上有帖子說在ScrollView中添加一屬性 android:fillViewport="true" ,這樣就可以讓ListView全屏顯示了。在我機器上測試失敗了。
三.重寫ListView、gridView(推薦)
重寫ListView:
public class MyListView extends ListView { public MyListView(Context context) { // TODO Auto-generated method stub super(context); } public MyListView(Context context, AttributeSet attrs) { // TODO Auto-generated method stub super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyle) { // TODO Auto-generated method stub super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
重寫GridView:
/** *自定義gridview,解決ScrollView中嵌套gridview顯示不正常的問題(1行) */ public class MyGridView extends GridView{ public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中實現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯
- ScrollView嵌套ListView滑動沖突的解決方法
- Android ListView的item中嵌套ScrollView的解決辦法
- 關(guān)于Android中ListView嵌套GridView的問題
- Android listview多視圖嵌套多視圖
- Android之ScrollView嵌套ListView和GridView沖突的解決方法
- ListView嵌套GridView使用詳解及注意事項
- 探討:如何在ScrollView中嵌套ListView
- Android筆記之:在ScrollView中嵌套ListView的方法
- Android滑動沖突的完美解決
相關(guān)文章
Android使用ViewPager實現(xiàn)屏幕滑動效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實現(xiàn)屏幕滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android原生TabLayout使用的超全解析(看這篇就夠了)
現(xiàn)在很多app都有頂部可左右切換的導(dǎo)航欄,并且還帶動畫效果,要實現(xiàn)這種導(dǎo)航欄,可以使用Android原生的Tablayout也可以借助第三方框架實現(xiàn),這篇文章主要給大家介紹了關(guān)于Android原生TabLayout使用的相關(guān)資料,需要的朋友可以參考下2022-09-09Android開發(fā)中一個簡單實用的調(diào)試應(yīng)用技巧分享
這篇文章主要跟大家分享了一個簡單實用的Android調(diào)試應(yīng)用技巧,文中介紹的非常詳細(xì),相信對大家具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧。2017-05-05Android 開發(fā)使用Activity實現(xiàn)加載等待界面功能示例
這篇文章主要介紹了Android 開發(fā)使用Activity實現(xiàn)加載等待界面功能,結(jié)合實例形式詳細(xì)分析了Android基于Activity實現(xiàn)加載等待界面布局與功能操作技巧,需要的朋友可以參考下2020-05-05