ExtendedListView.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012 Bartek Przybylski
  3. * Copyright (C) 2012-2015 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package com.owncloud.android.ui;
  19. import android.content.Context;
  20. import android.graphics.Canvas;
  21. import android.util.AttributeSet;
  22. import android.widget.ListView;
  23. import com.owncloud.android.lib.common.utils.Log_OC;
  24. /**
  25. * ListView allowing to specify the position of an item that should be centered in the visible area, if possible.
  26. *
  27. * The cleanest way I found to overcome the problem due to getHeight() returns 0 until the view is really drawn.
  28. *
  29. * @author David A. Velasco
  30. */
  31. public class ExtendedListView extends ListView {
  32. private static final String TAG = ExtendedListView.class.getSimpleName();
  33. private int mPositionToSetAndCenter = 0;
  34. public ExtendedListView(Context context) {
  35. super(context);
  36. }
  37. public ExtendedListView(Context context, AttributeSet attrs) {
  38. super(context, attrs);
  39. }
  40. public ExtendedListView(Context context, AttributeSet attrs, int defStyle) {
  41. super(context, attrs, defStyle);
  42. }
  43. /**
  44. * {@inheritDoc}
  45. *
  46. *
  47. */
  48. @Override
  49. protected void onDraw (Canvas canvas) {
  50. super.onDraw(canvas);
  51. if (mPositionToSetAndCenter > 0) {
  52. Log_OC.v(TAG, "Centering around position " + mPositionToSetAndCenter);
  53. this.setSelectionFromTop(mPositionToSetAndCenter, getHeight() / 2);
  54. mPositionToSetAndCenter = 0;
  55. }
  56. }
  57. /**
  58. * Public method to set the position of the item that should be centered in the visible area of the view.
  59. *
  60. * The position is saved here and checked in onDraw().
  61. *
  62. * @param position Position (in the list of items) of the item to center in the visible area.
  63. */
  64. public void setAndCenterSelection(int position) {
  65. mPositionToSetAndCenter = position;
  66. }
  67. }