安卓 解决方案

最最最最..简单的实现Android中Listview嵌套Lis

2017-01-07  本文已影响478人  决___则心可安

在一些初学者中很多的同学就会遇到这样的问题像标题所说的那样的问题。

什么废话我就不说了

/*
 * listview适应Scrollview
 */
public class ListViewForScrollView extends ListView {
    public ListViewForScrollView(Context context) {
        super(context);
    }

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

    public ListViewForScrollView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }
/**
     * 重写该方法,达到使ListView适应ScrollView的效果
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

(解释上面的代码)
写个自定义类继承listview写下三个构造方法这个我就不解释了,然后重写onMeasure方法。可以直接把上面的代码复制过去。

xml里面这样写:其中的xxx.xxxx 是你重写的这个类的包命 自定义类

  <xxx.xxxx.ListViewForScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@color/community_list_gray"
                android:dividerHeight="1dp" >
            </xxx.xxxx.ListViewForScrollView>

接下来的代码就是跟写listview一样的写就是了,注意的是如果是要做listview嵌套listview,那么就把这个类当成listview的条目。GridView嵌套也是这个原理只不过是把继承的类改成GridView,下面的方法都不用变。

贴上一张效果图


Screenshot_2017-01-07-12-46-49-1533014422.png

额上传了发现似乎有点大,不过不用在意这些细节。

下面的就是下载地址(说明一下 我只是把这两个类的代码文件放在下面而不是工程,因为我懒不想写。但是只要你不是傻子一般都是能明白)
http://pan.baidu.com/s/1dFuYSCD

上一篇下一篇

猜你喜欢

热点阅读