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ī)器上測試失敗了。
三.重寫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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中實(shí)現(xiàn)淘寶購物車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開發(fā)MQTT協(xié)議的模型及通信淺析
這篇文章主要W為大家介紹了Android開發(fā)MQTT協(xié)議的模型及通信淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測試可用)
Android帶數(shù)字或紅點(diǎn)的底部導(dǎo)航攔和聯(lián)網(wǎng)等待加載動(dòng)畫示例
Android實(shí)現(xiàn)讀取NFC卡的編號(hào)
Android項(xiàng)目開發(fā) 教你實(shí)現(xiàn)Periscope點(diǎn)贊效果

