Android中l(wèi)istview嵌套scrollveiw沖突的解決方法
一.使用網(wǎng)上用的動(dòng)態(tài)改變listview高度的方法
該方法只適用于item布局是LinearLayout布局的情況,不能是其他的,因?yàn)槠渌腖ayout(如RelativeLayout)沒有重寫onMeasure(),所以會(huì)在onMeasure()時(shí)拋出異常。所以使用限制較大。
public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { //獲取ListView對(duì)應(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()返回?cái)?shù)據(jù)項(xiàng)的數(shù)目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); //計(jì)算子項(xiàng)View 的寬高 totalHeight += listItem.getMeasuredHeight(); //統(tǒng)計(jì)所有子項(xiàng)的總高度 } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); //listView.getDividerHeight()獲取子項(xiàng)間分隔符占用的高度 //params.height最后得到整個(gè)ListView完整顯示需要的高度 listView.setLayoutParams(params); } }
二.網(wǎng)上有帖子說在ScrollView中添加一屬性 android:fillViewport="true" ,這樣就可以讓ListView全屏顯示了。在我機(jī)器上測(cè)試失敗了。
三.重寫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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中實(shí)現(xiàn)淘寶購(gòu)物車RecyclerView或LIstView的嵌套選擇的邏輯
- ScrollView嵌套ListView滑動(dòng)沖突的解決方法
- Android ListView的item中嵌套ScrollView的解決辦法
- 關(guān)于Android中ListView嵌套GridView的問題
- Android listview多視圖嵌套多視圖
- Android之ScrollView嵌套ListView和GridView沖突的解決方法
- ListView嵌套GridView使用詳解及注意事項(xiàng)
- 探討:如何在ScrollView中嵌套ListView
- Android筆記之:在ScrollView中嵌套ListView的方法
- Android滑動(dòng)沖突的完美解決
相關(guān)文章
Android使用ViewPager實(shí)現(xiàn)屏幕滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)屏幕滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android原生TabLayout使用的超全解析(看這篇就夠了)
現(xiàn)在很多app都有頂部可左右切換的導(dǎo)航欄,并且還帶動(dòng)畫效果,要實(shí)現(xiàn)這種導(dǎo)航欄,可以使用Android原生的Tablayout也可以借助第三方框架實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于Android原生TabLayout使用的相關(guān)資料,需要的朋友可以參考下2022-09-09Android開發(fā)中一個(gè)簡(jiǎn)單實(shí)用的調(diào)試應(yīng)用技巧分享
這篇文章主要跟大家分享了一個(gè)簡(jiǎn)單實(shí)用的Android調(diào)試應(yīng)用技巧,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧。2017-05-05Android實(shí)現(xiàn)驗(yàn)證碼登錄
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)驗(yàn)證碼登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03Android里實(shí)現(xiàn)退出主程序的提示代碼
當(dāng)用戶選擇"確定",就退出當(dāng)前的對(duì)話框。其中,有個(gè)很重要的函數(shù),Activity.finish(),通過調(diào)用這個(gè)函數(shù),退出當(dāng)前運(yùn)行的整個(gè)Android程序2013-06-06Android事件分發(fā)機(jī)制深入刨析原理及源碼
Android?的事件分發(fā)機(jī)制大體可以分為三部分:事件生產(chǎn)、事件分發(fā)?、事件消費(fèi)。事件的生產(chǎn)是由用戶點(diǎn)擊屏幕產(chǎn)生,我們這次著重分析事件的分發(fā)和消費(fèi),因?yàn)槭录职l(fā)和處理聯(lián)系的過于緊密,這篇文章將把事件的分發(fā)和消費(fèi)放在一起分析2023-04-04Android 開發(fā)使用Activity實(shí)現(xiàn)加載等待界面功能示例
這篇文章主要介紹了Android 開發(fā)使用Activity實(shí)現(xiàn)加載等待界面功能,結(jié)合實(shí)例形式詳細(xì)分析了Android基于Activity實(shí)現(xiàn)加載等待界面布局與功能操作技巧,需要的朋友可以參考下2020-05-05