热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通信录分组并且分组标签悬停划入划出(包含错误信息及修改)第三方开源PinnedSectionListView...

PinnedSectionListView在github上的链接地址是:https:github.combeworkerpinned-section-listview

PinnedSectionListView在github上的链接地址是:https://github.com/beworker/pinned-section-listview 。

下载下来后直接将PinnedSectionListView.java(在一些SDK版本拉动的时候会异常崩溃,异常信息和修改后的文档在后面)复制粘贴在要用的包中:

activity_main.xml:

1 <RelativeLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"
2 xmlns:tools&#61;"http://schemas.android.com/tools"
3 android:layout_width&#61;"match_parent"
4 android:layout_height&#61;"match_parent"
5 tools:context&#61;"com.zzw.testpinnedsectionlistview.MainActivity" >
6
7 <com.zzw.testpinnedsectionlistview.PinnedSectionListView
8 android:id&#61;"&#64;&#43;id/listView"
9 android:layout_width&#61;"match_parent"
10 android:layout_height&#61;"match_parent" />
11
12 RelativeLayout>

item.xml:

1 xml version&#61;"1.0" encoding&#61;"utf-8"?>
2 <RelativeLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"
3 android:layout_width&#61;"match_parent"
4 android:layout_height&#61;"match_parent"
5 android:orientation&#61;"vertical" >
6
7 <ImageView
8 android:id&#61;"&#64;&#43;id/imageView"
9 android:layout_width&#61;"wrap_content"
10 android:layout_height&#61;"wrap_content"
11 android:layout_alignParentLeft&#61;"true"
12 android:layout_centerVertical&#61;"true"
13 android:src&#61;"&#64;drawable/ic_launcher" />
14
15 <TextView
16 android:id&#61;"&#64;&#43;id/textView"
17 android:layout_width&#61;"wrap_content"
18 android:layout_height&#61;"wrap_content"
19 android:layout_alignBottom&#61;"&#64;&#43;id/imageView1"
20 android:layout_alignParentRight&#61;"true"
21 android:gravity&#61;"center"
22 android:textSize&#61;"20sp"
23 android:layout_alignTop&#61;"&#64;&#43;id/imageView1"
24 android:layout_toRightOf&#61;"&#64;&#43;id/imageView1"
25 android:text&#61;"TextView" />
26
27 RelativeLayout>

item.xml

MainActivity.java&#xff1a;

1 package com.zzw.testpinnedsectionlistview;
2
3 import java.util.ArrayList;
4
5 import com.zzw.testpinnedsectionlistview.PinnedSectionListView.PinnedSectionListAdapter;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.graphics.Color;
10 import android.os.Bundle;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.view.ViewGroup;
14 import android.widget.ArrayAdapter;
15 import android.widget.TextView;
16
17 public class MainActivity extends Activity {
18
19 private ArrayList items &#61; null;
20
21 private final int VIEW_TYPE_COUNT &#61; 2;
22
23 &#64;Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27
28 items &#61;new ArrayList();
29 // 假设我们演示以A,B,C,,,F这样的字符串作为分组的标签。
30 // 每一组装载5个子数据。
31 String[] groups &#61; { "A", "B", "C", "D", "E" };
32 for (int i &#61; 0; i ) {
33 String s &#61; groups[i];
34
35 Item group &#61; new Item();
36 group.type &#61; Item.GROUP;
37 group.text &#61; s;
38 items.add(group);
39
40 for (int j &#61; 0; j <10; j&#43;&#43;) {
41 Item child &#61; new Item();
42 child.type &#61; Item.CHILD;
43 child.text &#61; s &#43; "组数据&#xff1a;" &#43; j;
44 items.add(child);
45 }
46 }
47
48 PinnedSectionListView listView &#61; (PinnedSectionListView) findViewById(R.id.listView);
49 listView.setAdapter(new MyAdapter(this, -1));
50
51 }
52
53 private class Item {
54 public static final int GROUP &#61; 0;
55 public static final int CHILD &#61; 1;
56
57 public int type;
58 public String text;
59 }
60
61 private class MyAdapter extends ArrayAdapter implements PinnedSectionListAdapter {
62
63 private LayoutInflater inflater;
64
65 public MyAdapter(Context context, int resource) {
66 super(context, resource);
67
68 inflater &#61; (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
69 }
70
71 &#64;Override
72 public int getItemViewType(int position) {
73
74 return items.get(position).type;
75 }
76
77 &#64;Override
78 public int getCount() {
79
80 return items.size();
81 }
82
83 &#64;Override
84 public int getViewTypeCount() {
85
86 return VIEW_TYPE_COUNT;
87 }
88
89 &#64;Override
90 public Item getItem(int position) {
91
92 return items.get(position);
93 }
94
95 &#64;Override
96 public View getView(int position, View convertView, ViewGroup parent) {
97 switch (getItemViewType(position)) {
98 case Item.GROUP:
99
100 if (convertView &#61;&#61; null) {
101 convertView &#61; inflater.inflate(android.R.layout.simple_list_item_1, null);
102 }
103
104 TextView textView_group &#61; (TextView) convertView.findViewById(android.R.id.text1);
105 textView_group.setText(getItem(position).text);
106 textView_group.setTextColor(Color.BLUE);
107 textView_group.setTextSize(30);
108 textView_group.setBackgroundColor(Color.GRAY);
109
110 break;
111
112 case Item.CHILD:
113 if (convertView &#61;&#61; null) {
114 convertView &#61; inflater.inflate(R.layout.item, null);
115 }
116
117 TextView textView_child &#61; (TextView) convertView.findViewById(R.id.textView);
118 textView_child.setText(getItem(position).text);
119 textView_child.setBackgroundColor(Color.YELLOW);
120
121 break;
122 }
123 return convertView;
124 }
125
126 /*
127 * 假设此方法返回皆为false。那么PinnedSectionListView将退化成为一个基础的ListView.
128 * 只不过退化后的ListView只是一个拥有两个View Type的ListView。
129 *
130 * 从某种角度上讲&#xff0c;此方法对于PinnedSectionListView至关重要
131 * 返回值true或false&#xff0c;将直接导致PinnedSectionListView是一个PinnedSectionListView&#xff0c;
132 * 还是一个普通的ListView
133 *
134 */
135 &#64;Override
136 public boolean isItemViewTypePinned(int viewType) {
137 boolean type &#61; false;
138 switch (viewType) {
139 case Item.GROUP:
140 type &#61; true;
141 break;
142
143 case Item.CHILD:
144 type &#61; false;
145 break;
146
147 default:
148 type &#61; false;
149 break;
150 }
151 return type;
152 }
153 }
154 }

程序运行拉动的时候有的sdk版本会出现程序崩溃&#xff0c;LogCat是这样情况&#xff1a;

 

报错的是原代码PinnedSectionListView.java中的&#xff08;200行左右&#xff09;&#xff1a;

// read layout parametersLayoutParams layoutParams &#61; (LayoutParams) pinnedView.getLayoutParams();if (layoutParams &#61;&#61; null) {layoutParams &#61; (LayoutParams) generateDefaultLayoutParams();pinnedView.setLayoutParams(layoutParams);}

主要是这句话&#xff1a;

layoutParams &#61; (LayoutParams) generateDefaultLayoutParams();

经由研究发现&#xff0c;此原因是在调用Android系统的generateDefaultLayoutParams()方法时候&#xff0c;发生异常&#xff0c;致使代码运行获得的结果layoutParams不正常&#xff0c;进而导致PinnedSectionListView崩溃。

解决方案&#xff1a;
自己动手重写Android系统的generateDefaultLayoutParams()方法&#xff0c;返回自己定制的LayoutParams值。

具体实现&#xff1a;
在PinnedSectionListView.java中增加自己重写的generateDefaultLayoutParams()方法&#xff1a;

1 &#64;Override
2 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
3
4 LayoutParams mLayoutParams&#61;new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
5
6 return mLayoutParams;
7 }

最终修复bug&#xff0c;改进后的PinnedSectionListView.java全部源代码为如下&#xff08;可直接复制粘贴使用&#xff09;&#xff1a;

1 /*
2 * Copyright (C) 2013 Sergej Shafarenka, halfbit.de
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file kt in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.zzw.testpinnedsectionlistview;
18
19
20 import android.content.Context;
21 import android.database.DataSetObserver;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.PointF;
25 import android.graphics.Rect;
26 import android.graphics.drawable.GradientDrawable;
27 import android.graphics.drawable.GradientDrawable.Orientation;
28 import android.os.Parcelable;
29 import android.util.AttributeSet;
30 import android.view.MotionEvent;
31 import android.view.SoundEffectConstants;
32 import android.view.View;
33 import android.view.ViewConfiguration;
34 import android.view.ViewGroup;
35 import android.view.accessibility.AccessibilityEvent;
36 import android.widget.AbsListView;
37 import android.widget.HeaderViewListAdapter;
38 import android.widget.ListAdapter;
39 import android.widget.ListView;
40 import android.widget.SectionIndexer;
41
42
43 /**
44 * ListView, which is capable to pin section views at its top while the rest is still scrolled.
45 */
46 public class PinnedSectionListView extends ListView {
47
48 //-- inner classes
49
50 /** List adapter to be implemented for being used with PinnedSectionListView adapter. */
51 public static interface PinnedSectionListAdapter extends ListAdapter {
52 /** This method shall return &#39;true&#39; if views of given type has to be pinned. */
53 boolean isItemViewTypePinned(int viewType);
54 }
55
56 /** Wrapper class for pinned section view and its position in the list. */
57 static class PinnedSection {
58 public View view;
59 public int position;
60 public long id;
61 }
62
63 //-- class fields
64
65 // fields used for handling touch events
66 private final Rect mTouchRect &#61; new Rect();
67 private final PointF mTouchPoint &#61; new PointF();
68 private int mTouchSlop;
69 private View mTouchTarget;
70 private MotionEvent mDownEvent;
71
72 // fields used for drawing shadow under a pinned section
73 private GradientDrawable mShadowDrawable;
74 private int mSectionsDistanceY;
75 private int mShadowHeight;
76
77 /** Delegating listener, can be null. */
78 OnScrollListener mDelegateOnScrollListener;
79
80 /** Shadow for being recycled, can be null. */
81 PinnedSection mRecycleSection;
82
83 /** shadow instance with a pinned view, can be null. */
84 PinnedSection mPinnedSection;
85
86 /** Pinned view Y-translation. We use it to stick pinned view to the next section. */
87 int mTranslateY;
88
89 /** Scroll listener which does the magic */
90 private final OnScrollListener mOnScrollListener &#61; new OnScrollListener() {
91
92 &#64;Override public void onScrollStateChanged(AbsListView view, int scrollState) {
93 if (mDelegateOnScrollListener !&#61; null) { // delegate
94 mDelegateOnScrollListener.onScrollStateChanged(view, scrollState);
95 }
96 }
97
98 &#64;Override
99 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
100
101 if (mDelegateOnScrollListener !&#61; null) { // delegate
102 mDelegateOnScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
103 }
104
105 // get expected adapter or fail fast
106 ListAdapter adapter &#61; getAdapter();
107 if (adapter &#61;&#61; null || visibleItemCount &#61;&#61; 0) return; // nothing to do
108
109 final boolean isFirstVisibleItemSection &#61;
110 isItemViewTypePinned(adapter, adapter.getItemViewType(firstVisibleItem));
111
112 if (isFirstVisibleItemSection) {
113 View sectionView &#61; getChildAt(0);
114 if (sectionView.getTop() &#61;&#61; getPaddingTop()) { // view sticks to the top, no need for pinned shadow
115 destroyPinnedShadow();
116 } else { // section doesn&#39;t stick to the top, make sure we have a pinned shadow
117 ensureShadowForPosition(firstVisibleItem, firstVisibleItem, visibleItemCount);
118 }
119
120 } else { // section is not at the first visible position
121 int sectionPosition &#61; findCurrentSectionPosition(firstVisibleItem);
122 if (sectionPosition > -1) { // we have section position
123 ensureShadowForPosition(sectionPosition, firstVisibleItem, visibleItemCount);
124 } else { // there is no section for the first visible item, destroy shadow
125 destroyPinnedShadow();
126 }
127 }
128 };
129
130 };
131
132 /** Default change observer. */
133 private final DataSetObserver mDataSetObserver &#61; new DataSetObserver() {
134 &#64;Override public void onChanged() {
135 recreatePinnedShadow();
136 };
137 &#64;Override public void onInvalidated() {
138 recreatePinnedShadow();
139 }
140 };
141
142 //-- constructors
143
144 public PinnedSectionListView(Context context, AttributeSet attrs) {
145 super(context, attrs);
146 initView();
147 }
148
149 public PinnedSectionListView(Context context, AttributeSet attrs, int defStyle) {
150 super(context, attrs, defStyle);
151 initView();
152 }
153
154 private void initView() {
155 setOnScrollListener(mOnScrollListener);
156 mTouchSlop &#61; ViewConfiguration.get(getContext()).getScaledTouchSlop();
157 initShadow(true);
158 }
159
160 //-- public API methods
161
162 public void setShadowVisible(boolean visible) {
163 initShadow(visible);
164 if (mPinnedSection !&#61; null) {
165 View v &#61; mPinnedSection.view;
166 invalidate(v.getLeft(), v.getTop(), v.getRight(), v.getBottom() &#43; mShadowHeight);
167 }
168 }
169
170 //-- pinned section drawing methods
171
172 public void initShadow(boolean visible) {
173 if (visible) {
174 if (mShadowDrawable &#61;&#61; null) {
175 mShadowDrawable &#61; new GradientDrawable(Orientation.TOP_BOTTOM,
176 new int[] { Color.parseColor("#ffa0a0a0"), Color.parseColor("#50a0a0a0"), Color.parseColor("#00a0a0a0")});
177 mShadowHeight &#61; (int) (8 * getResources().getDisplayMetrics().density);
178 }
179 } else {
180 if (mShadowDrawable !&#61; null) {
181 mShadowDrawable &#61; null;
182 mShadowHeight &#61; 0;
183 }
184 }
185 }
186
187 //*****添加*****//
188 &#64;Override
189 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
190 LayoutParams mLayoutParams&#61;new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
191 return mLayoutParams;
192 }
193 //*****添加*****//
194
195 /** Create shadow wrapper with a pinned view for a view at given position */
196 void createPinnedShadow(int position) {
197
198 // try to recycle shadow
199 PinnedSection pinnedShadow &#61; mRecycleSection;
200 mRecycleSection &#61; null;
201
202 // create new shadow, if needed
203 if (pinnedShadow &#61;&#61; null) pinnedShadow &#61; new PinnedSection();
204 // request new view using recycled view, if such
205 View pinnedView &#61; getAdapter().getView(position, pinnedShadow.view, PinnedSectionListView.this);
206
207 // read layout parameters
208 LayoutParams layoutParams &#61; (LayoutParams) pinnedView.getLayoutParams();
209 if (layoutParams &#61;&#61; null) {
210 layoutParams &#61; (LayoutParams) generateDefaultLayoutParams();
211 pinnedView.setLayoutParams(layoutParams);
212 }
213
214 int heightMode &#61; MeasureSpec.getMode(layoutParams.height);
215 int heightSize &#61; MeasureSpec.getSize(layoutParams.height);
216
217 if (heightMode &#61;&#61; MeasureSpec.UNSPECIFIED) heightMode &#61; MeasureSpec.EXACTLY;
218
219 int maxHeight &#61; getHeight() - getListPaddingTop() - getListPaddingBottom();
220 if (heightSize > maxHeight) heightSize &#61; maxHeight;
221
222 // measure & layout
223 int ws &#61; MeasureSpec.makeMeasureSpec(getWidth() - getListPaddingLeft() - getListPaddingRight(), MeasureSpec.EXACTLY);
224 int hs &#61; MeasureSpec.makeMeasureSpec(heightSize, heightMode);
225 pinnedView.measure(ws, hs);
226 pinnedView.layout(0, 0, pinnedView.getMeasuredWidth(), pinnedView.getMeasuredHeight());
227 mTranslateY &#61; 0;
228
229 // initialize pinned shadow
230 pinnedShadow.view &#61; pinnedView;
231 pinnedShadow.position &#61; position;
232 pinnedShadow.id &#61; getAdapter().getItemId(position);
233
234 // store pinned shadow
235 mPinnedSection &#61; pinnedShadow;
236 }
237
238 /** Destroy shadow wrapper for currently pinned view */
239 void destroyPinnedShadow() {
240 if (mPinnedSection !&#61; null) {
241 // keep shadow for being recycled later
242 mRecycleSection &#61; mPinnedSection;
243 mPinnedSection &#61; null;
244 }
245 }
246
247 /** Makes sure we have an actual pinned shadow for given position. */
248 void ensureShadowForPosition(int sectionPosition, int firstVisibleItem, int visibleItemCount) {
249 if (visibleItemCount <2) { // no need for creating shadow at all, we have a single visible item
250 destroyPinnedShadow();
251 return;
252 }
253
254 if (mPinnedSection !&#61; null
255 && mPinnedSection.position !&#61; sectionPosition) { // invalidate shadow, if required
256 destroyPinnedShadow();
257 }
258
259 if (mPinnedSection &#61;&#61; null) { // create shadow, if empty
260 createPinnedShadow(sectionPosition);
261 }
262
263 // align shadow according to next section position, if needed
264 int nextPosition &#61; sectionPosition &#43; 1;
265 if (nextPosition < getCount()) {
266 int nextSectionPosition &#61; findFirstVisibleSectionPosition(nextPosition,
267 visibleItemCount - (nextPosition - firstVisibleItem));
268 if (nextSectionPosition > -1) {
269 View nextSectionView &#61; getChildAt(nextSectionPosition - firstVisibleItem);
270 final int bottom &#61; mPinnedSection.view.getBottom() &#43; getPaddingTop();
271 mSectionsDistanceY &#61; nextSectionView.getTop() - bottom;
272 if (mSectionsDistanceY <0) {
273 // next section overlaps pinned shadow, move it up
274 mTranslateY &#61; mSectionsDistanceY;
275 } else {
276 // next section does not overlap with pinned, stick to top
277 mTranslateY &#61; 0;
278 }
279 } else {
280 // no other sections are visible, stick to top
281 mTranslateY &#61; 0;
282 mSectionsDistanceY &#61; Integer.MAX_VALUE;
283 }
284 }
285
286 }
287
288 int findFirstVisibleSectionPosition(int firstVisibleItem, int visibleItemCount) {
289 ListAdapter adapter &#61; getAdapter();
290
291 int adapterDataCount &#61; adapter.getCount();
292 if (getLastVisiblePosition() >&#61; adapterDataCount) return -1; // dataset has changed, no candidate
293
294 if (firstVisibleItem&#43;visibleItemCount >&#61; adapterDataCount){//added to prevent index Outofbound (in case)
295 visibleItemCount &#61; adapterDataCount-firstVisibleItem;
296 }
297
298 for (int childIndex &#61; 0; childIndex ) {
299 int position &#61; firstVisibleItem &#43; childIndex;
300 int viewType &#61; adapter.getItemViewType(position);
301 if (isItemViewTypePinned(adapter, viewType)) return position;
302 }
303 return -1;
304 }
305
306 int findCurrentSectionPosition(int fromPosition) {
307 ListAdapter adapter &#61; getAdapter();
308
309 if (fromPosition >&#61; adapter.getCount()) return -1; // dataset has changed, no candidate
310
311 if (adapter instanceof SectionIndexer) {
312 // try fast way by asking section indexer
313 SectionIndexer indexer &#61; (SectionIndexer) adapter;
314 int sectionPosition &#61; indexer.getSectionForPosition(fromPosition);
315 int itemPosition &#61; indexer.getPositionForSection(sectionPosition);
316 int typeView &#61; adapter.getItemViewType(itemPosition);
317 if (isItemViewTypePinned(adapter, typeView)) {
318 return itemPosition;
319 } // else, no luck
320 }
321
322 // try slow way by looking through to the next section item above
323 for (int position&#61;fromPosition; position>&#61;0; position--) {
324 int viewType &#61; adapter.getItemViewType(position);
325 if (isItemViewTypePinned(adapter, viewType)) return position;
326 }
327 return -1; // no candidate found
328 }
329
330 void recreatePinnedShadow() {
331 destroyPinnedShadow();
332 ListAdapter adapter &#61; getAdapter();
333 if (adapter !&#61; null && adapter.getCount() > 0) {
334 int firstVisiblePosition &#61; getFirstVisiblePosition();
335 int sectionPosition &#61; findCurrentSectionPosition(firstVisiblePosition);
336 if (sectionPosition &#61;&#61; -1) return; // no views to pin, exit
337 ensureShadowForPosition(sectionPosition,
338 firstVisiblePosition, getLastVisiblePosition() - firstVisiblePosition);
339 }
340 }
341
342 &#64;Override
343 public void setOnScrollListener(OnScrollListener listener) {
344 if (listener &#61;&#61; mOnScrollListener) {
345 super.setOnScrollListener(listener);
346 } else {
347 mDelegateOnScrollListener &#61; listener;
348 }
349 }
350
351 &#64;Override
352 public void onRestoreInstanceState(Parcelable state) {
353 super.onRestoreInstanceState(state);
354 post(new Runnable() {
355 &#64;Override public void run() { // restore pinned view after configuration change
356 recreatePinnedShadow();
357 }
358 });
359 }
360
361 &#64;Override
362 public void setAdapter(ListAdapter adapter) {
363
364 // assert adapter in debug mode
365 if (BuildConfig.DEBUG && adapter !&#61; null) {
366 if (!(adapter instanceof PinnedSectionListAdapter))
367 throw new IllegalArgumentException("Does your adapter implement PinnedSectionListAdapter?");
368 if (adapter.getViewTypeCount() <2)
369 throw new IllegalArgumentException("Does your adapter handle at least two types" &#43;
370 " of views in getViewTypeCount() method: items and sections?");
371 }
372
373 // unregister observer at old adapter and register on new one
374 ListAdapter oldAdapter &#61; getAdapter();
375 if (oldAdapter !&#61; null) oldAdapter.unregisterDataSetObserver(mDataSetObserver);
376 if (adapter !&#61; null) adapter.registerDataSetObserver(mDataSetObserver);
377
378 // destroy pinned shadow, if new adapter is not same as old one
379 if (oldAdapter !&#61; adapter) destroyPinnedShadow();
380
381 super.setAdapter(adapter);
382 }
383
384 &#64;Override
385 protected void onLayout(boolean changed, int l, int t, int r, int b) {
386 super.onLayout(changed, l, t, r, b);
387 if (mPinnedSection !&#61; null) {
388 int parentWidth &#61; r - l - getPaddingLeft() - getPaddingRight();
389 int shadowWidth &#61; mPinnedSection.view.getWidth();
390 if (parentWidth !&#61; shadowWidth) {
391 recreatePinnedShadow();
392 }
393 }
394 }
395
396 &#64;Override
397 protected void dispatchDraw(Canvas canvas) {
398 super.dispatchDraw(canvas);
399
400 if (mPinnedSection !&#61; null) {
401
402 // prepare variables
403 int pLeft &#61; getListPaddingLeft();
404 int pTop &#61; getListPaddingTop();
405 View view &#61; mPinnedSection.view;
406
407 // draw child
408 canvas.save();
409
410 int clipHeight &#61; view.getHeight() &#43;
411 (mShadowDrawable &#61;&#61; null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY));
412 canvas.clipRect(pLeft, pTop, pLeft &#43; view.getWidth(), pTop &#43; clipHeight);
413
414 canvas.translate(pLeft, pTop &#43; mTranslateY);
415 drawChild(canvas, mPinnedSection.view, getDrawingTime());
416
417 if (mShadowDrawable !&#61; null && mSectionsDistanceY > 0) {
418 mShadowDrawable.setBounds(mPinnedSection.view.getLeft(),
419 mPinnedSection.view.getBottom(),
420 mPinnedSection.view.getRight(),
421 mPinnedSection.view.getBottom() &#43; mShadowHeight);
422 mShadowDrawable.draw(canvas);
423 }
424
425 canvas.restore();
426 }
427 }
428
429 //-- touch handling methods
430
431 &#64;Override
432 public boolean dispatchTouchEvent(MotionEvent ev) {
433
434 final float x &#61; ev.getX();
435 final float y &#61; ev.getY();
436 final int action &#61; ev.getAction();
437
438 if (action &#61;&#61; MotionEvent.ACTION_DOWN
439 && mTouchTarget &#61;&#61; null
440 && mPinnedSection !&#61; null
441 && isPinnedViewTouched(mPinnedSection.view, x, y)) { // create touch target
442
443 // user touched pinned view
444 mTouchTarget &#61; mPinnedSection.view;
445 mTouchPoint.x &#61; x;
446 mTouchPoint.y &#61; y;
447
448 // copy down event for eventually be used later
449 mDownEvent &#61; MotionEvent.obtain(ev);
450 }
451
452 if (mTouchTarget !&#61; null) {
453 if (isPinnedViewTouched(mTouchTarget, x, y)) { // forward event to pinned view
454 mTouchTarget.dispatchTouchEvent(ev);
455 }
456
457 if (action &#61;&#61; MotionEvent.ACTION_UP) { // perform onClick on pinned view
458 super.dispatchTouchEvent(ev);
459 performPinnedItemClick();
460 clearTouchTarget();
461
462 } else if (action &#61;&#61; MotionEvent.ACTION_CANCEL) { // cancel
463 clearTouchTarget();
464
465 } else if (action &#61;&#61; MotionEvent.ACTION_MOVE) {
466 if (Math.abs(y - mTouchPoint.y) > mTouchSlop) {
467
468 // cancel sequence on touch target
469 MotionEvent event &#61; MotionEvent.obtain(ev);
470 event.setAction(MotionEvent.ACTION_CANCEL);
471 mTouchTarget.dispatchTouchEvent(event);
472 event.recycle();
473
474 // provide correct sequence to super class for further handling
475 super.dispatchTouchEvent(mDownEvent);
476 super.dispatchTouchEvent(ev);
477 clearTouchTarget();
478
479 }
480 }
481
482 return true;
483 }
484
485 // call super if this was not our pinned view
486 return super.dispatchTouchEvent(ev);
487 }
488
489 private boolean isPinnedViewTouched(View view, float x, float y) {
490 view.getHitRect(mTouchRect);
491
492 // by taping top or bottom padding, the list performs on click on a border item.
493 // we don&#39;t add top padding here to keep behavior consistent.
494 mTouchRect.top &#43;&#61; mTranslateY;
495
496 mTouchRect.bottom &#43;&#61; mTranslateY &#43; getPaddingTop();
497 mTouchRect.left &#43;&#61; getPaddingLeft();
498 mTouchRect.right -&#61; getPaddingRight();
499 return mTouchRect.contains((int)x, (int)y);
500 }
501
502 private void clearTouchTarget() {
503 mTouchTarget &#61; null;
504 if (mDownEvent !&#61; null) {
505 mDownEvent.recycle();
506 mDownEvent &#61; null;
507 }
508 }
509
510 private boolean performPinnedItemClick() {
511 if (mPinnedSection &#61;&#61; null) return false;
512
513 OnItemClickListener listener &#61; getOnItemClickListener();
514 if (listener !&#61; null && getAdapter().isEnabled(mPinnedSection.position)) {
515 View view &#61; mPinnedSection.view;
516 playSoundEffect(SoundEffectConstants.CLICK);
517 if (view !&#61; null) {
518 view.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
519 }
520 listener.onItemClick(this, view, mPinnedSection.position, mPinnedSection.id);
521 return true;
522 }
523 return false;
524 }
525
526 public static boolean isItemViewTypePinned(ListAdapter adapter, int viewType) {
527 if (adapter instanceof HeaderViewListAdapter) {
528 adapter &#61; ((HeaderViewListAdapter)adapter).getWrappedAdapter();
529 }
530 return ((PinnedSectionListAdapter) adapter).isItemViewTypePinned(viewType);
531 }
532
533 }

PinnedSectionListView.java

 

转:https://www.cnblogs.com/zzw1994/p/4997601.html



推荐阅读
  • 深入解析 Android 中 EditText 的 getLayoutParams 方法及其代码应用实例 ... [详细]
  • 本文介绍了一种自定义的Android圆形进度条视图,支持在进度条上显示数字,并在圆心位置展示文字内容。通过自定义绘图和组件组合的方式实现,详细展示了自定义View的开发流程和关键技术点。示例代码和效果展示将在文章末尾提供。 ... [详细]
  • 使用 ListView 浏览安卓系统中的回收站文件 ... [详细]
  • 在处理 XML 数据时,如果需要解析 `` 标签的内容,可以采用 Pull 解析方法。Pull 解析是一种高效的 XML 解析方式,适用于流式数据处理。具体实现中,可以通过 Java 的 `XmlPullParser` 或其他类似的库来逐步读取和解析 XML 文档中的 `` 元素。这样不仅能够提高解析效率,还能减少内存占用。本文将详细介绍如何使用 Pull 解析方法来提取 `` 标签的内容,并提供一个示例代码,帮助开发者快速解决问题。 ... [详细]
  • ButterKnife 是一款用于 Android 开发的注解库,主要用于简化视图和事件绑定。本文详细介绍了 ButterKnife 的基础用法,包括如何通过注解实现字段和方法的绑定,以及在实际项目中的应用示例。此外,文章还提到了截至 2016 年 4 月 29 日,ButterKnife 的最新版本为 8.0.1,为开发者提供了最新的功能和性能优化。 ... [详细]
  • 本文探讨了资源访问的学习路径与方法,旨在帮助学习者更高效地获取和利用各类资源。通过分析不同资源的特点和应用场景,提出了多种实用的学习策略和技术手段,为学习者提供了系统的指导和建议。 ... [详细]
  • 设计实战 | 10个Kotlin项目深度解析:首页模块开发详解
    设计实战 | 10个Kotlin项目深度解析:首页模块开发详解 ... [详细]
  • 【问题】在Android开发中,当为EditText添加TextWatcher并实现onTextChanged方法时,会遇到一个问题:即使只对EditText进行一次修改(例如使用删除键删除一个字符),该方法也会被频繁触发。这不仅影响性能,还可能导致逻辑错误。本文将探讨这一问题的原因,并提供有效的解决方案,包括使用Handler或计时器来限制方法的调用频率,以及通过自定义TextWatcher来优化事件处理,从而提高应用的稳定性和用户体验。 ... [详细]
  • 深入剖析Java中SimpleDateFormat在多线程环境下的潜在风险与解决方案
    深入剖析Java中SimpleDateFormat在多线程环境下的潜在风险与解决方案 ... [详细]
  • 在Android开发中,当TextView的高度固定且内容超出时,可以通过设置其内置的滚动条属性来实现垂直滚动功能。具体来说,可以通过配置`android:scrollbars="vertical"`来启用垂直滚动,确保用户能够查看完整的内容。此外,为了优化用户体验,建议结合`setMovementMethod(ScrollerMovementMethod.getInstance())`方法,使滚动操作更加流畅和自然。 ... [详细]
  • 本文详细介绍了一种利用 ESP8266 01S 模块构建 Web 服务器的成功实践方案。通过具体的代码示例和详细的步骤说明,帮助读者快速掌握该模块的使用方法。在疫情期间,作者重新审视并研究了这一未被充分利用的模块,最终成功实现了 Web 服务器的功能。本文不仅提供了完整的代码实现,还涵盖了调试过程中遇到的常见问题及其解决方法,为初学者提供了宝贵的参考。 ... [详细]
  • 在Android开发中,实现多点触控功能需要使用`OnTouchListener`监听器来捕获触摸事件,并在`onTouch`方法中进行详细的事件处理。为了优化多点触控的交互体验,开发者可以通过识别不同的触摸手势(如缩放、旋转等)并进行相应的逻辑处理。此外,还可以结合`MotionEvent`类提供的方法,如`getPointerCount()`和`getPointerId()`,来精确控制每个触点的行为,从而提升用户操作的流畅性和响应性。 ... [详细]
  • 在Cisco IOS XR系统中,存在提供服务的服务器和使用这些服务的客户端。本文深入探讨了进程与线程状态转换机制,分析了其在系统性能优化中的关键作用,并提出了改进措施,以提高系统的响应速度和资源利用率。通过详细研究状态转换的各个环节,本文为开发人员和系统管理员提供了实用的指导,旨在提升整体系统效率和稳定性。 ... [详细]
  • Web开发框架概览:Java与JavaScript技术及框架综述
    Web开发涉及服务器端和客户端的协同工作。在服务器端,Java是一种优秀的编程语言,适用于构建各种功能模块,如通过Servlet实现特定服务。客户端则主要依赖HTML进行内容展示,同时借助JavaScript增强交互性和动态效果。此外,现代Web开发还广泛使用各种框架和库,如Spring Boot、React和Vue.js,以提高开发效率和应用性能。 ... [详细]
  • 尽管我们尽最大努力,任何软件开发过程中都难免会出现缺陷。为了更有效地提升对支持部门的协助与支撑,本文探讨了多种策略和最佳实践,旨在通过改进沟通、增强培训和支持流程来减少这些缺陷的影响,并提高整体服务质量和客户满意度。 ... [详细]
author-avatar
shadowsuyan3
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有