ListView优化相关

一、ListView与ScrollView合用的冲突

    /**
     * 重新计算ListView的高度,解决ScrollView和ListView两个View都有滚动的效果,在嵌套使用时起冲突的问题
     *
     * @param listView
     */
    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
       在对ListView设置Adapter后,调用当前方法,重新布局与绘制。将两个的滑动监听事件区分开来~


二、为ListView item设置CheckBox,标识选中未选中状态

1,在布局文件中,设置CheckBox不可用,Enable;

2,为对象添加标记,标注CheckBox的状态,额外变量;

3,为ListView添加OnItemClickListener监听器,修改额外变量的值;

4,刷新Adapter,修改当前展示状态;

<pre name="code" class="java">if (infos.get(position).isSelected()) {
    infos.get(position).setIsSelected(false);
   } else {
    infos.get(position).setIsSelected(true);
}

 
 
    // 刷新适配器
    public void initAdapter() {
        if (adapter == null && customInfos != null && customInfos.size() > 0) {
            adapter = new NewAdapter(CustomActivity.this, customInfos);
            customContenLV.setAdapter(adapter);
            //adapter数据适配器    addList选中的条目集合   customInfos原始数据集合
           } else {
            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }
        }
        if (customInfos != null) {
            int size = customInfos.size();
            addList.clear();
            for (int i = 0; i < size; i++) {
                if (customInfos.get(i).isSelected()) {
                    addList.add(customInfos.get(i).getUserid());
                }
            }
        }
    }
三、日期选择对话框

        new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        String dayDayOfWeek = getDayOfWeek(year, monthOfYear, dayOfMonth);
                        time.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth + " " + dayDayOfWeek);
                    }
                },     //初始化默认选择时间   
                        c.get(Calendar.YEAR),
                        c.get(Calendar.MONTH),
                        c.get(Calendar.DAY_OF_MONTH)).show();


//获取当前日历的实例对象
Calendar c = Calendar.getInstance();

四、年月日转换成为星期

    /**
     * 依据年月日获取到星期几  周三
     *
     * @param year
     * @param monthOfYear
     * @param dayOfMonth
     * @return
     */
    private String getDayOfWeek(int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, monthOfYear, dayOfMonth);
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            case 7:
                return "周六";
            default:
                return "周日";
        }
    }

人拥有此生此世是不够的 他还需要一个诗意的世界~~~

你可能感兴趣的:(ListView优化相关)