scrollview中嵌套listview 數據顯示不全


處理方法一:
1、當Item的佈局是LinerarLayout時:(此方法不推薦)
private void showAllListView()
{
if (lotteryStyleAdapter != null)
{


int totalHeight = 0;


for (int i = 0; i < lotteryStyleAdapter.getCount(); i++)
{


View listItem = lotteryStyleAdapter.getView(i, null, listview);


listItem.measure(0, 0);


totalHeight += listItem.getMeasuredHeight();


}


ViewGroup.LayoutParams params = listview.getLayoutParams();


params.height = totalHeight
+ (listview.getDividerHeight() * (lotteryStyleAdapter
.getCount() - 1));


listview.setLayoutParams(params);


}
}


2、當Item佈局是其他佈局(包含LinearLayout):(推薦使用)


2.1 重寫ListView:


package com.test.lottery.view;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;


/**
 * 自定義的ListView 用於ScrollView嵌套ListView時 ListView展示不全
 */
public class AllListView extends ListView
{


public AllListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}


public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, mExpandSpec);
}


}
2.2、書寫佈局文件是用自定義的AllListView代替ListView使用


方法二:

ScrollView中嵌套ListView,使得listview上下滑動卡頓現象

最近項目中發現會出現內存溢出展開listview導致 現在換了一種方式如下:

使用方式在adapter更新數據後設置

mBusinessMoreRecommendAdapter.updateBusinessMoreRecommentData(mHitilistEntities);
SetListviewItemHeigtUtil.setListViewHeightBasedOnChildren(lvBusinessRecommend);


/**ScrollView嵌套ListView只顯示一行
 * 動態改變ListView的高度
 * Created by jiangbing on 2016/3/9.
 */
public class SetListviewItemHeigtUtil {

    public static void setListViewHeightBasedOnChildren(ListView listView) {
          //獲取ListView對應的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()返回數據項的數目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); //計算子項View 的寬高
            totalHeight += listItem.getMeasuredHeight(); //統計所有子項的總高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            //listView.getDividerHeight()獲取子項間分隔符佔用的高度
            //params.height最後得到整個ListView完整顯示需要的高度
        listView.setLayoutParams(params);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章