首页 > 代码库 > Android ListView

Android ListView

  1. 仿Ios listview具有弹力拉伸效果

    重写listview中overScrollBy()方法,修改maxOverScrollY为自己当前设定的值。

    /** *  * Description:仿ios ListView有弹力拉伸效果 *  * @author YangDODO *  */public class FlexibleListView extends ListView {    private static int mMaxOverDistance = 50;// 拉伸距离    public FlexibleListView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        initView(context);    }    public FlexibleListView(Context context, AttributeSet attrs) {        super(context, attrs);        initView(context);    }    public FlexibleListView(Context context) {        super(context);        initView(context);    }    private void initView(Context context) {        DisplayMetrics metrics = context.getResources().getDisplayMetrics();        float density = metrics.density;        mMaxOverDistance = (int) (density * mMaxOverDistance);    }    @Override    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,            int scrollY, int scrollRangeX, int scrollRangeY,            int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY,                scrollRangeX, scrollRangeY, maxOverScrollX, mMaxOverDistance,                isTouchEvent);    }

     

  2. 解决scrollview嵌套ListView问题
           @Override        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {                int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                                MeasureSpec.AT_MOST);                super.onMeasure(widthMeasureSpec, expandSpec);        }

     

  3. 如何让多个android listview同时使用一个滚动条?设置listview高度
      /**     * 设置listview高度,注意listview子项必须为LinearLayout才能调用该方法     * @param listview listview     *      */    public static void  setListViewHeight(ListView listview){          int totalHeight = 0;           ListAdapter adapter= listview.getAdapter();       if(null != adapter){        for (int i = 0; i <adapter.getCount(); i++) {            View listItem = adapter.getView(i, null, listview);            if (null != listItem) {            listItem.measure(0, 0);//注意listview子项必须为LinearLayout才能调用该方法            totalHeight += listItem.getMeasuredHeight();            }        }                        ViewGroup.LayoutParams params = listview.getLayoutParams();            params.height = totalHeight + (listview.getDividerHeight() * (listview.getCount() - 1));            listview.setLayoutParams(params);         }    } 

  

 

Android ListView