码迷,mamicode.com
首页 > 其他好文 > 详细

ListView和Gridview与滚动冲突解决

时间:2014-12-02 17:03:09      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   io   ar   color   sp   for   on   

   现在的界面是越做越长了,不知道你们会有什么绝招来对付这么长的界面,我还是利用listview或者GridView来结合Scrollview来合成。然后,问题来了,如果试这么开始的,总会有滑动冲突的现象产生。

那么,该何去何从呢,我也是见别人的参考做的。记下来,今后肯定用的多。

package com.ky.utills;

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

public class Utility {
    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);
    }

    public static void setGridViewHeightBasedOnChildren(GridView gridView,
            int num) {
        ListAdapter listAdapter = gridView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int count = 0;
        if (listAdapter.getCount() % num == 0) {
            count = listAdapter.getCount() / num;
        } else {
            count = listAdapter.getCount() / num + 1;

        }
        for (int i = 0; i < count; i++) {
            View listItem = listAdapter.getView(i, null, gridView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        params.height = totalHeight + (gridView.getHeight() * ((count) - 1));
        gridView.setLayoutParams(params);
    }
}

,然后在Viewsetadapter后,加上这个就okay:

        Utility.setListViewHeightBasedOnChildren(game_ListView);

完好,今天送给自己一句话:人活着一定要有意义

ListView和Gridview与滚动冲突解决

标签:android   style   blog   io   ar   color   sp   for   on   

原文地址:http://www.cnblogs.com/Catherine-Brain/p/4137916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!