Android_ScrollView中嵌套ListView显示不全的解决方法

tips:本博客Github上的Demo链接:https://github.com/chengbiao1314/android_ListViewInListView.git


ScrollView、ListView、GridView之间的相互嵌套使用的时候,我们会发现滑动的是最外边的控件,而最里面的控件常常显示不全,面对显示不全的问题该怎么处理?大家在显示不全的时候,如果尝试给内部的ListView(这里以ListView为例)设置一个固定高度,它会显示我们设置的固定高度的内容,因此我们可以确定,之所以显示不全,是因为内部的ListView不知道它自己在父控件中应该显示的高度。
所以我们可以推测:如果我们最后给内部的ListView指定了一个高度,是不是问题就解决了?事实证明确实如此
新问题就是:我们该怎么来测量这个ListView的高度呢?解决了这个问题,我们的麻烦就得到解决了。

ps:有一点要值得注意的是:ListView的Item条目的每一个布局都必须要是LinearLayout,因为LinearLayout重写了OnMeasure方法,这样在测量的时候才能获取子Item的高度,不然其余的布局是不能达到这个目的的

解决这个问题的两步

1、测量内部ListView的实际高度

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

2、在给内部ListView设置完Adapter之后,给这个内部的ListView设置一个测量出来的高度
if(madapter == null){
            madapter = new mAdapter();
        }
        lv_listview.setAdapter(madapter);
        AdapterUtility.setListViewHeightBasedOnChildren(lv_listview);



完整代码如下:

布局文件:



    
        
            
            
            

            
            
        
    


代码如下:
测量ListView的工具类:
package com.example.ricky.listviewinlistview.utils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created by admin on 2016/4/12.
 */
public class AdapterUtility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}
Activity代码:
package com.example.ricky.listviewinlistview.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.ricky.listviewinlistview.R;
import com.example.ricky.listviewinlistview.utils.AdapterUtility;

/**
 * Created by admin on 2016/4/12.
 */
public class ListviewInScrollviewActivity extends Activity {
    private mAdapter madapter;
    private ListView lv_listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_in_scrollview);

        lv_listview = (ListView) findViewById(R.id.lv_listview);

        initListView();
    }

    private void initListView(){
        if(madapter == null){
            madapter = new mAdapter();
        }
        lv_listview.setAdapter(madapter);
        AdapterUtility.setListViewHeightBasedOnChildren(lv_listview);
    }

   public  class mAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 30;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            if (convertView == null) {
                convertView = View.inflate(ListviewInScrollviewActivity.this, R.layout.item_lv_listview_in_scrollview, null);
            }
            tv = (TextView) convertView.findViewById(R.id.tv_item);
            tv.setText("item"+position);
            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }


    }
}

你可能感兴趣的:(Android)