首页 > 代码库 > 僵硬的ListView

僵硬的ListView

不可以滚动的ListView,解决在ScrollView中需要使用Listview的问题
标签: Android

[1].[文件] RigidListView.java ~ 4KB    下载(10) 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
packagecom.phodev.andtools.widget;
 
importandroid.content.Context;
importandroid.content.res.Resources.NotFoundException;
importandroid.database.DataSetObserver;
importandroid.graphics.drawable.Drawable;
importandroid.util.AttributeSet;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.BaseAdapter;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.ListAdapter;
 
/**
 * LinearLayout模仿ListView,解决ListView在ScrollView中的不兼容问题
 *
 * @author skg
 *
 */
publicclass RigidListView extendsLinearLayout implementsOnClickListener {
 
    privateBaseAdapter mAdapter;
    privateOnItemClickListener onItemClickListener;
    privateDrawable dividerDrawable;
    privateint dividerResId = android.R.drawable.divider_horizontal_dim_dark;
    privateint itemSelectorDrawableResId = android.R.drawable.list_selector_background;
 
    /**
     * 绑定布局
     */
    privatevoid bindLinearLayout() {
        removeAllChild();
        intcount = mAdapter.getCount();
        for(inti = 0; i < count; i++) {
            View v = mAdapter.getView(i, null,null);
            v.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));
            //
            LinearLayout itemContainer = newLinearLayout(getContext());
            itemContainer.setLayoutParams(newLayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            // 添加Item的View
            itemContainer.addView(v);
            // 设置Divider
            itemContainer.setBackgroundResource(itemSelectorDrawableResId);
 
            itemContainer.setOrientation(LinearLayout.VERTICAL);
            itemContainer.setId(i);
 
            itemContainer.setClickable(true);
            if(onItemClickListener != null) {
                itemContainer.setOnClickListener(this);
            }
 
            itemContainer.setFocusable(true);
 
            Drawable divider = getDivider();
            if(divider != null) {
                ImageView line = newImageView(getContext());
                line.setBackgroundDrawable(dividerDrawable);
                itemContainer.addView(line);
            }
 
            addView(itemContainer, i);
        }
    }
 
    publicRigidListView(Context context) {
        super(context);
        init(context);
    }
 
    publicRigidListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
 
    privatevoid init(Context context) {
        this.setOrientation(LinearLayout.VERTICAL);
    }
 
    /**
     * 不设置的话使用默认的,0表示不使用任何Divider
     *
     * @param resId
     */
    publicvoid setDivider(intresId) {
        dividerResId = resId;
    }
 
    publicvoid setItemSelector(intresId) {
        itemSelectorDrawableResId = resId;
    }
 
    privateDrawable getDivider() {
        if(dividerDrawable == null&& dividerResId != 0) {
            try{
                dividerDrawable = getContext().getResources().getDrawable(
                        dividerResId);
            }catch(NotFoundException e) {
                e.printStackTrace();
            }
        }
        returndividerDrawable;
    }
 
    /**
     * 获取Adapter
     *
     * @return adapter
     */
    publicListAdapter getAdpater() {
        returnmAdapter;
    }
 
    /**
     * 设置数据
     *
     * @param adpater
     */
    publicvoid setAdapter(BaseAdapter adpater) {
        mAdapter = adpater;
        mAdapter.registerDataSetObserver(dataSetObserver);
        bindLinearLayout();
    }
 
    /**
     * 设置点击事件,OnItemClickListener不同于ListView的监听,这个监听必须在setAdapter之前才有效
     *
     * @param onItemClickListener
     */
    publicvoid setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
 
    publicinterface OnItemClickListener {
        publicvoid onItemClick(View view, ListAdapter adpater, intposition);
    }
 
    @Override
    publicvoid onClick(View v) {
        if(onItemClickListener != null) {
            onItemClickListener.onItemClick(v, mAdapter, v.getId());
        }
    }
 
    privateDataSetObserver dataSetObserver = newRigidDataSetObserver();
 
    publicclass RigidDataSetObserver extendsDataSetObserver {
 
        @Override
        publicvoid onChanged() {
            refreshUI();
        }
 
        @Override
        publicvoid onInvalidated() {
            refreshUI();
        }
 
    }
 
    /**
     * 刷新UI
     */
    privatevoid refreshUI() {
        bindLinearLayout();
 
    }
 
    privatevoid removeAllChild() {
        if(this.getChildCount() > 0) {
            removeAllViews();
 
        }
    }
}

僵硬的ListView