解决ScrollView嵌套ListView以及ListView嵌套ListView冲突的问题

来源:互联网 发布:淘宝怎么更改身份认证 编辑:程序博客网 时间:2024/04/29 08:31

      首先解决的是ScrollView嵌套ListView冲突的问题: 如果我们直接ScroView嵌套ListView的时候,就会出现ListView中的item只会出现一条内容,在一个item上滑动,所以解决冲突的关键是重写一个类继承ListView,解决冲突问题。

     

<aye_com.aye_aye_paste_android.app.widget.YListView    android:id="@+id/lv"                   android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:background="@color/color_fa"       android:divider="@null"            android:scrollbars="none" />

public class YListView extends ListView {    public YListView(Context context) {        super(context);    }    public YListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public YListView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }}

     之后解决ListView嵌套ListView的问题,问题也是同样的:方法会跟到之后......

     

if (position == 1) {    holder.tv_top.setText("美读");    holder.tv_foot.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            context.startActivity(new Intent(context, ConsultActivity.class));        }    });    ReadAdapter readAdapter = new ReadAdapter(context);    holder.lv.setAdapter(readAdapter);    if (null != bean.getT3()) {        readAdapter.setData(bean.getT3());        readAdapter.notifyDataSetChanged();        //解决scrollview嵌套listview的问题  如果不写会只显示一行        mapHeight.put(position,ListViewHeighUtils.setListViewHeightBasedOnChildren(holder.lv));    }}


private HashMap<Integer,Integer> mapHeight = new HashMap<>();

/** * 解决ScrollView嵌套ListView时,会无法正确的计算ListView的大小 * * @param listView */public static int setListViewHeightBasedOnChildren(ListView listView) {    // 获取ListView对应的Adapter    ListAdapter listAdapter = listView.getAdapter(); //item的高度    if (listAdapter == null) {        return 0 ;    }    int totalHeight = 0;    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {        // listAdapter.getCount()返回数据项的数目        View listItem = listAdapter.getView(i, null, listView);        // 计算子项View 的宽高        listItem.measure(0, 0);        // 统计所有子项的总高度        totalHeight += listItem.getMeasuredHeight();    }    ViewGroup.LayoutParams params = listView.getLayoutParams();    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));    // listView.getDividerHeight()获取子项间分隔符占用的高度    // params.height最后得到整个ListView完整显示需要的高度    listView.setLayoutParams(params);    // 返回总体高度    return params.height;}











阅读全文
1 0
原创粉丝点击